vendredi 21 août 2015

Python Unittest test for functions with random variables and unpredictable input

Function idea:

Random + Random = Output (right or wrong)

Function:

def input_gener():
    """Generates questions and prints and
    returns status result of user input as a string"""
    a = random.randrange(1,10)
    b = random.randrange(1,10)
    c = input("What is the sum of {0} and {1}? ".format(a,b))
    if c == a+b:
        print("{0} is right!.".format(c))
        status = "was right"
    else:
        print("{0} is wrong!.".format(c))
        status = "was wrong"            
    return status

I could create test like this:

def test_res_right(self):
    #Test right value
    right_status ="was right"
    self.assertEqual(right_status,input_gener())
def test_res_wrong(self):
    wrong_status = "was wrong"
    self.assertEqual(wrong_status,input_gener())

Results depend how I will enter data. Unless somehow I could retrieve values from the variables “a” and “b” and could modify each test to be right or wrong in a bit more automated way. What would better way to test such type functions where random values not known and input is unpredictable.




Aucun commentaire:

Enregistrer un commentaire