def load(self, test_set_name, fnc): """Load the TestSet and keep in memory constraints.""" if type(fnc) is not FunctionType: raise MalformedTestCase('assert_function must be of FunctionType.') self.__test_set_name = test_set_name self.__fnc = fnc self.__test_set = load_test_set(test_set_name)
def test_get_input_and_output_from_tests(self): test = load_test_set('temp_test') self.assertEqual(test[0].input, 2) self.assertEqual(test[0].output, 4) self.assertDictEqual(test[1].input, {'n': 6}) self.assertEqual(test[1].output, 12)
def test_performance(self): test = load_test_set('temp_test') performance = test.evaluate(double) self.assertEqual(len(performance), 4) performance = test.evaluate(lambda n: n * 2) self.assertEqual(len(performance), 4) performance = test.evaluate(lambda n: n ** 2) self.assertEqual(len(performance), 4)
def test_only_open_tests_in_iteration_and_indexation(self): test = load_test_set('temp_test') with self.assertRaises(IndexError): _ = test[3].input count = 0 for _ in test: count += 1 self.assertEqual(count, 3)
def test_evaluate(self): test = load_test_set('temp_test') evaluation1 = test.evaluate(double) self.assertEqual(len(evaluation1), 4) evaluation2 = test.evaluate(lambda n: n*2) self.assertEqual(len(evaluation2), 4) evaluation3 = test.evaluate(lambda n: n**2) self.assertEqual(len(evaluation3), 4) self.assertEqual(evaluation1, evaluation2) self.assertNotEqual(evaluation2, evaluation3)
def test_fail_to_get_closed_tests(self): test = load_test_set('temp_test') with self.assertRaises(AttributeError): _ = test.__closed_tests
def test_load(self): test = load_test_set('temp_test') self.assertEqual(len(test.test_cases), 3) self.assertEqual(test.closed_tests_count(), 1)
# Simple test test = TestSet() test.add_test(2, 4, assert_function=lambda x, y: 1.0 if x == y else 0.0) test.add_test({'n': 6}, 12, assert_function=equal) test.add_test({'n': 4}, 8, assert_function=equal) print('1:', test.evaluate(lambda n: 2**n), 'wrong') # wrong print('2:', test.evaluate(double), 'correct') print('3:', test.evaluate(lambda n: n**2), 'wrong') # wrong print() test.save('dobro.test') # Load test test2 = load_test_set('dobro.test') print('1:', test2.evaluate(lambda n: 2**n), 'wrong') # wrong print('2:', test2.evaluate(lambda n: 2 * n), 'correct') print('3:', test2.evaluate(lambda n: n**2), 'wrong') # wrong print() # 2 parameter input test test3 = TestSet() test3.add_test({'x': 2, 'y': 2}, 4, assert_function=equal) test3.add_test({'x': 2, 'y': 3}, 5, assert_function=equal) test3.add_test({'x': 10, 'y': 10}, 20, assert_function=equal) test3.add_test({'x': 100, 'y': 101}, 201, assert_function=equal) test3.save('teste3')