|
|
|
|
|
import os |
|
import random |
|
from pathlib import Path |
|
|
|
random.seed(42) |
|
import time |
|
import unittest |
|
|
|
from geco_data_generator import contdepfunct |
|
|
|
|
|
|
|
|
|
num_tests = 100000 |
|
|
|
|
|
|
|
test_argument_data_dict = { |
|
('contdepfunct','n/a','blood_pressure_depending_on_age'): \ |
|
{'age':[[[1],[2],[3],[77],[8],[9],[3.76],[99.9],[129.65],[42]], |
|
[[None],[''],['test'],[-65],[999],[-0.03],[130.01], |
|
[187.87],[{}],[[]]]]}, |
|
('contdepfunct','n/a','salary_depending_on_age'): \ |
|
{'age':[[[1],[2],[3],[77],[8],[9],[3.76],[99.9],[129.65],[42]], |
|
[[None],[''],['test'],[-65],[999],[-0.03],[130.01], |
|
[187.87],[{}],[[]]]]} |
|
} |
|
|
|
|
|
|
|
class TestCase(unittest.TestCase): |
|
|
|
|
|
|
|
def setUp(self): |
|
pass |
|
|
|
|
|
|
|
def tearDown(self): |
|
pass |
|
|
|
|
|
|
|
|
|
def testArguments(self, test_data): |
|
"""Test if a function or method can be called or initialised correctly with |
|
different values for their input arguments (parameters). |
|
|
|
The argument 'test_data' must be a dictionary with the following |
|
structure: |
|
|
|
- Keys are tuples consisting of three strings: |
|
(module_name, class_name, function_or_method_name) |
|
- Values are dictionaries where the keys are the names of the input |
|
argument that is being tested, and the values of these dictionaries |
|
are a list that contains two lists. The first list contains valid |
|
input arguments ('normal' argument tests) that should pass the test, |
|
while the second list contains illegal input arguments ('exception' |
|
argument tests) that should raise an exception. |
|
|
|
The lists of test cases are itself lists, each containing a number of |
|
input argument values, as many as are expected by the function or |
|
method that is being tested. |
|
|
|
This function returns a list containing the test results, where each |
|
list element is a string with comma separated values (CSV) which are to |
|
be written into the testing log file. |
|
""" |
|
|
|
test_res_list = [] |
|
|
|
|
|
for (test_method_names, test_method_data) in test_data.items(): |
|
test_method_name = test_method_names[2] |
|
print('Testing arguments for method/function:', test_method_name) |
|
|
|
for argument_name in test_method_data: |
|
print(' Testing input argument:', argument_name) |
|
|
|
norm_test_data = test_method_data[argument_name][0] |
|
exce_test_data = test_method_data[argument_name][1] |
|
print(' Normal test cases: ', norm_test_data) |
|
print(' Exception test cases:', exce_test_data) |
|
|
|
|
|
|
|
num_norm_test_cases = len(norm_test_data) |
|
num_norm_test_passed = 0 |
|
num_norm_test_failed = 0 |
|
norm_failed_desc_str = '' |
|
|
|
for test_input in norm_test_data: |
|
passed = True |
|
|
|
if (len(test_input) == 0): |
|
try: |
|
getattr(contdepfunct,test_method_name)() |
|
except: |
|
passed = False |
|
|
|
elif (len(test_input) == 1): |
|
try: |
|
getattr(contdepfunct,test_method_name)(test_input[0]) |
|
except: |
|
passed = False |
|
|
|
elif (len(test_input) == 2): |
|
try: |
|
getattr(contdepfunct,test_method_name)(test_input[0], |
|
test_input[1]) |
|
except: |
|
passed = False |
|
|
|
elif (len(test_input) == 3): |
|
try: |
|
getattr(contdepfunct,test_method_name)(test_input[0], |
|
test_input[1], |
|
test_input[2]) |
|
except: |
|
passed = False |
|
|
|
elif (len(test_input) == 4): |
|
try: |
|
getattr(contdepfunct,test_method_name)(test_input[0], |
|
test_input[1], |
|
test_input[2], |
|
test_input[3]) |
|
except: |
|
passed = False |
|
|
|
elif (len(test_input) == 5): |
|
try: |
|
getattr(contdepfunct,test_method_name)(test_input[0], |
|
test_input[1], |
|
test_input[2], |
|
test_input[3], |
|
test_input[4]) |
|
except: |
|
passed = False |
|
|
|
else: |
|
raise Exception('Illegal number of input arguments') |
|
|
|
|
|
|
|
if (passed == False): |
|
num_norm_test_failed += 1 |
|
norm_failed_desc_str += 'Failed test for input ' + \ |
|
"'%s'; " % (str(test_input)) |
|
else: |
|
num_norm_test_passed += 1 |
|
|
|
assert num_norm_test_failed+num_norm_test_passed == num_norm_test_cases |
|
|
|
norm_test_result_str = test_method_names[0] + ',' + \ |
|
test_method_names[1] + ',' + \ |
|
test_method_names[2] + ',' + \ |
|
argument_name + ',normal,' + \ |
|
'%d,' % (num_norm_test_cases) |
|
if (num_norm_test_failed == 0): |
|
norm_test_result_str += 'all tests passed' |
|
else: |
|
norm_test_result_str += '%d tests failed,' % (num_norm_test_failed) |
|
norm_test_result_str += '"'+norm_failed_desc_str[:-2]+'"' |
|
|
|
test_res_list.append(norm_test_result_str) |
|
|
|
|
|
|
|
num_exce_test_cases = len(exce_test_data) |
|
num_exce_test_passed = 0 |
|
num_exce_test_failed = 0 |
|
exce_failed_desc_str = '' |
|
|
|
for test_input in exce_test_data: |
|
passed = True |
|
|
|
if (len(test_input) == 0): |
|
try: |
|
self.assertRaises(Exception, |
|
getattr(contdepfunct,test_method_name)) |
|
except: |
|
passed = False |
|
|
|
elif (len(test_input) == 1): |
|
try: |
|
self.assertRaises(Exception, |
|
getattr(contdepfunct,test_method_name),test_input[0]) |
|
except: |
|
passed = False |
|
|
|
elif (len(test_input) == 2): |
|
try: |
|
self.assertRaises(Exception, |
|
getattr(contdepfunct,test_method_name),test_input[0], |
|
test_input[1]) |
|
except: |
|
passed = False |
|
|
|
elif (len(test_input) == 3): |
|
try: |
|
self.assertRaises(Exception, |
|
getattr(contdepfunct,test_method_name),test_input[0], |
|
test_input[1], |
|
test_input[2]) |
|
except: |
|
passed = False |
|
|
|
elif (len(test_input) == 4): |
|
try: |
|
self.assertRaises(Exception, |
|
getattr(contdepfunct,test_method_name),test_input[0], |
|
test_input[1], |
|
test_input[2], |
|
test_input[3]) |
|
except: |
|
passed = False |
|
|
|
elif (len(test_input) == 5): |
|
try: |
|
self.assertRaises(Exception, |
|
getattr(contdepfunct,test_method_name),test_input[0], |
|
test_input[1], |
|
test_input[2], |
|
test_input[3], |
|
test_input[4]) |
|
except: |
|
passed = False |
|
|
|
else: |
|
raise Exception('Illegal number of input arguments') |
|
|
|
|
|
|
|
if (passed == False): |
|
num_exce_test_failed += 1 |
|
exce_failed_desc_str += 'Failed test for input ' + \ |
|
"'%s'; " % (str(test_input)) |
|
else: |
|
num_exce_test_passed += 1 |
|
|
|
assert num_exce_test_failed+num_exce_test_passed == num_exce_test_cases |
|
|
|
exce_test_result_str = test_method_names[0] + ',' + \ |
|
test_method_names[1] + ',' + \ |
|
test_method_names[2] + ',' + \ |
|
argument_name + ',exception,' + \ |
|
'%d,' % (num_exce_test_cases) |
|
if (num_exce_test_failed == 0): |
|
exce_test_result_str += 'all tests passed' |
|
else: |
|
exce_test_result_str += '%d tests failed,' % (num_exce_test_failed) |
|
exce_test_result_str += '"'+exce_failed_desc_str[:-2]+'"' |
|
|
|
test_res_list.append(exce_test_result_str) |
|
|
|
test_res_list.append('') |
|
|
|
return test_res_list |
|
|
|
|
|
|
|
def testFunct_blood_pressure_depending_on_age(self, num_tests): |
|
"""Test the functionality of 'blood_pressure_depending_on_age', making |
|
sure this function returns a positive floating point value. |
|
""" |
|
|
|
print('Testing functionality of "blood_pressure_depending_on_age"') |
|
|
|
num_passed = 0 |
|
num_failed = 0 |
|
|
|
for i in range(num_tests): |
|
|
|
age = random.uniform(0.0, 120.0) |
|
|
|
try: |
|
assert (contdepfunct.blood_pressure_depending_on_age(age) >= 0.0) |
|
num_passed += 1 |
|
except: |
|
num_failed += 1 |
|
|
|
assert num_passed + num_failed == num_tests |
|
|
|
test_result_str = 'contdepfunct,n/a,blood_pressure_depending_on_age,' + \ |
|
'n/a,funct,%d,' % (num_tests) |
|
if (num_failed == 0): |
|
test_result_str += 'all tests passed' |
|
else: |
|
test_result_str += '%d tests failed' % (num_failed) |
|
|
|
return [test_result_str,''] |
|
|
|
|
|
|
|
def testFunct_salary_depending_on_age(self, num_tests): |
|
"""Test the functionality of 'salary_depending_on_age', making sure |
|
this function returns a positive floating point value. |
|
""" |
|
|
|
print('Testing functionality of "salary_depending_on_age"') |
|
|
|
num_passed = 0 |
|
num_failed = 0 |
|
|
|
for i in range(num_tests): |
|
|
|
age = random.uniform(0.0, 120.0) |
|
|
|
try: |
|
assert (contdepfunct.salary_depending_on_age(age) >= 0.0) |
|
num_passed += 1 |
|
except: |
|
num_failed += 1 |
|
|
|
assert num_passed + num_failed == num_tests |
|
|
|
test_result_str = 'contdepfunct,n/a,salary_depending_on_age,' + \ |
|
'n/a,funct,%d,' % (num_tests) |
|
if (num_failed == 0): |
|
test_result_str += 'all tests passed' |
|
else: |
|
test_result_str += '%d tests failed' % (num_failed) |
|
|
|
return [test_result_str,''] |
|
|
|
|
|
|
|
|
|
curr_time_tuple = time.localtime() |
|
curr_time_str = str(curr_time_tuple[0]) + str(curr_time_tuple[1]).zfill(2) + \ |
|
str(curr_time_tuple[2]).zfill(2) + '-' + \ |
|
str(curr_time_tuple[3]).zfill(2) + \ |
|
str(curr_time_tuple[4]).zfill(2) |
|
|
|
|
|
|
|
Path('./logs').mkdir(exist_ok=True) |
|
out_file_name = './logs/contdepfunctTest-%s.csv' % (curr_time_str) |
|
|
|
out_file = open(out_file_name, 'w') |
|
|
|
out_file.write('Test results generated by contdepfunctTest.py' + os.linesep) |
|
|
|
out_file.write('Test started: ' + curr_time_str + os.linesep) |
|
|
|
out_file.write(os.linesep) |
|
|
|
out_file.write('Module name,Class name,Method name,Arguments,Test_type,' + \ |
|
'Patterns tested,Summary,Failure description' + os.linesep) |
|
out_file.write(os.linesep) |
|
|
|
|
|
|
|
test_res_list = [] |
|
test_case_ins = TestCase('testArguments') |
|
test_res_list += test_case_ins.testArguments(test_argument_data_dict) |
|
|
|
test_case_ins = TestCase('testFunct_blood_pressure_depending_on_age') |
|
test_res_list += \ |
|
test_case_ins.testFunct_blood_pressure_depending_on_age(num_tests) |
|
|
|
test_case_ins = TestCase('testFunct_salary_depending_on_age') |
|
test_res_list += test_case_ins.testFunct_salary_depending_on_age(num_tests) |
|
|
|
|
|
|
|
for line in test_res_list: |
|
out_file.write(line + os.linesep) |
|
|
|
out_file.close() |
|
|
|
print('Test results are written to', out_file_name) |
|
|
|
for line in test_res_list: |
|
print(line) |
|
|
|
|
|
|