mercredi 29 avril 2015

Unit Testing in Python for fileinput and commands.getstatusoutput

def find_repalce():
    name_file = '/tmp/name.txt'
    text_to_search = "find"
    text_to_replace = "replace"
    new_name_file = '/tmp/new_name.text'

    if os.path.isfile(name_file):

        move_ping_file = commands.getstatusoutput("sudo cp " + name_file + " " + new_name_file)

        if not move_ping_file[0]:
            return 0

        os.system("sudo chmod 666 " + new_name_file)

        for line in fileinput.input(new_name_file, inplace=True):
            sys.stdout.write(line.replace(text_to_search, text_to_replace))

        os.system("sudo chmod 644 " + new_name_file)

        replace_file = commands.getstatusoutput("sudo cp " + new_name_file + " " + name_file)

        if not replace_file[0]:
            return 0
        return 1

    else:
        return 0 

Hi I am new to unit testing in python. I have tried mocking but it is no use? Can anyone help me mock the file and handle the default calls in the python?

Any Suggesstion kindly comment it below

Thanks in Advance

Aucun commentaire:

Enregistrer un commentaire