class UnittestTestRunnerTest(BaseTestCases.BaseTestRunnerTest): TEST_RUNNER_CLS = UnittestTestRunner TEST_SRC_SUCCESS = utils.f(""" import target from unittest import TestCase class MulTest(TestCase): def test_mul(self): self.assertEqual(target.mul(2), 4) """) TEST_SRC_FAIL = utils.f(""" import target from unittest import TestCase class MulTest(TestCase): def test_mul(self): self.assertEqual(target.mul(2), 5) """) TEST_SRC_SKIP = (utils.f(""" import target from unittest import TestCase, skip class MulTest(TestCase): @skip("test skipping") def test_skipped(self): pass """))
def test_replace_continue_by_break(self): self.assert_mutation(utils.f(""" for x in y: continue """), [utils.f(""" for x in y: break """)])
def test_deletion_with_arguments(self): self.assert_mutation(utils.f(""" @a(1) def f(): pass """), [utils.f(""" def f(): pass """)])
def test_single_decorator_deletion(self): self.assert_mutation(utils.f(""" @a def f(): pass """), [utils.f(""" def f(): pass """)])
def test_change_position_from_first_to_last(self): self.assert_mutation(utils.f(""" class A: def foo(self, x): super().foo(x) """), [utils.f(""" class A: def foo(self, x): pass """)])
def test_double_decorators_deletion(self): self.assert_mutation(utils.f(""" @a @b def f(): pass """), [utils.f(""" def f(): pass """)])
def test_delete_overriding_method_when_base_class_from_other_module(self): self.assert_mutation(utils.f(""" import ast class A(ast.NodeTransformer): def visit(self): pass """), [utils.f(""" import ast class A(ast.NodeTransformer): pass """)], with_exec=True)
def test_delete_variable(self): self.assert_mutation(utils.f(""" class B: x = 1 class A(B): x = 2 """), [utils.f(""" class B: x = 1 class A(B): pass """)], with_exec=True)
def test_delete_variable_if_one_hiding_in_three_targets(self): self.assert_mutation(utils.f(""" class B: x = 1 class A(B): (x, y, z) = (2, 3, 4) """), [utils.f(""" class B: x = 1 class A(B): (y, z) = (3, 4) """)], with_exec=True)
def test_delete_variable_if_two_hiding_in_two_targets(self): self.assert_mutation(utils.f(""" class B: (x, y) = (1, 2) class A(B): (x, y) = (3, 4) """), [utils.f(""" class B: (x, y) = (1, 2) class A(B): pass """)], with_exec=True)
def test_swallow_exception(self): self.assert_mutation(utils.f(""" try: pass except: raise """), [utils.f(""" try: pass except: pass """)])
def test_add_staticmethod_decorator(self): self.assert_mutation( utils.f(""" class X: def f(): pass """), utils.f(""" class X: @staticmethod def f(): pass """))
def test_add_staticmethod_decorator(self): self.assert_mutation( utils.f(""" class X: def f(): pass """), utils.f(""" class X: @staticmethod def f(): pass """) )
def test_delete_overriding_method(self): self.assert_mutation(utils.f(""" class A: def foo(self): pass class B(A): def foo(self): pass """), [utils.f(""" class A: def foo(self): pass class B(A): pass """)], with_exec=True)
def test_not_swallow_if_pass(self): self.assert_mutation(utils.f(""" try: pass except: pass """), [])
def test_not_delete_if_raise(self): self.assert_mutation(utils.f(""" try: pass except: raise """), [])
def test_not_add_if_already_has_staticmethod(self): self.assert_no_mutation(utils.f(""" class X: @staticmethod def f(): pass """))
def test_if_coverage(self): node = utils.create_ast( utils.f(""" if False: pass elif True: pass else: pass """)) self.coverage_injector.inject(node) if_node = node.body[0] first_if_test_node = if_node.test first_if_body_el = if_node.body[0] second_if_node = if_node.orelse[0] second_if_test_node = second_if_node.test second_if_body_el = second_if_node.body[0] else_body_el = second_if_node.orelse[0] self.assertTrue(self.coverage_injector.is_covered(first_if_test_node)) self.assertFalse(self.coverage_injector.is_covered(first_if_body_el)) self.assertTrue(self.coverage_injector.is_covered(second_if_test_node)) self.assertTrue(self.coverage_injector.is_covered(second_if_body_el)) self.assertFalse(self.coverage_injector.is_covered(else_body_el))
def test_not_change_position_if_single_statement(self): self.assert_mutation( utils.f(""" class A: def foo(self): super().foo() """), [])
class MutationControllerTest(unittest.TestCase): TARGET_SRC = 'def mul(x): return x * x' TEST_SRC = utils.f(""" import target from unittest import TestCase class MulTest(TestCase): def test_mul(self): self.assertEqual(target.mul(2), 4) def test_not_used(self): pass """) def setUp(self): target_loader = MockModulesLoader('target', self.TARGET_SRC) test_loader = MockModulesLoader('test', self.TEST_SRC) self.score_view = MutationScoreStoreView() mutator = controller.FirstOrderMutator( [operators.ArithmeticOperatorReplacement], percentage=100) self.mutation_controller = MockMutationController( target_loader=target_loader, test_loader=test_loader, views=[self.score_view], mutant_generator=mutator, mutate_covered=True, ) def test_run(self): self.mutation_controller.run() score = self.score_view.score self.assertEqual(score.all_mutants, 3) self.assertEqual(score.killed_mutants, 2) self.assertEqual(score.survived_mutants, 1)
def test_change_position_from_first_to_last(self): self.assert_mutation(utils.f(""" class B: def foo(self, x, y=1, *args, **kwargs): pass class A(B): def foo(self, x, y=1, *args, **kwargs): pass """), [utils.f(""" class B: def foo(self, x, y=1, *args, **kwargs): pass class A(B): def foo(self, x, y=1, *args, **kwargs): super().foo(x, y=1, *args, **kwargs) pass """)], with_exec=True)
class PytestTestRunnerTest(BaseTestCases.BaseTestRunnerTest): TEST_RUNNER_CLS = PytestTestRunner TEST_SRC_SUCCESS = utils.f(""" import target def test_mul(): assert target.mul(2) == 4 """) TEST_SRC_FAIL = utils.f(""" import target def test_mul(): assert target.mul(2) == 5 """) TEST_SRC_SKIP = utils.f(""" import target import pytest @pytest.mark.skip(reason="test skipping") def test_mul(): assert target.mul(2) == 4 """)
def test_double_mutation(self): self.assert_mutation(utils.f(""" while x: pass for x in y: pass """), [ utils.f(""" while x: pass break for x in y: pass """), utils.f(""" while x: pass for x in y: pass break """), ])
def test_func_def_coverage(self): node = utils.create_ast(utils.f(""" def foo(x): pass """)) self.coverage_injector.inject(node) func_node = node.body[0] func_body_el = func_node.body[0] arg_node = func_node.args.args[0] self.assertTrue(self.coverage_injector.is_covered(func_node)) self.assertTrue(self.coverage_injector.is_covered(arg_node)) self.assertFalse(self.coverage_injector.is_covered(func_body_el))
def test_func_def_coverage(self): node = utils.create_ast(utils.f(""" def foo(x): pass """)) self.coverage_injector.inject(node) func_node = node.body[0] func_body_el = func_node.body[0] arg_node = func_node.args.args[0] self.assert_covered([func_node, arg_node]) self.assert_not_covered([func_body_el])
def test_for_coverage(self): node = utils.create_ast(utils.f(""" for x in []: pass else: pass """)) self.coverage_injector.inject(node) for_node = node.body[0] for_body_el = for_node.body[0] else_body_el = for_node.orelse[0] self.assert_covered([for_node, for_node.target, for_node.iter, else_body_el]) self.assert_not_covered([for_body_el])
def test_except_coverage(self): node = utils.create_ast(utils.f(""" try: raise KeyError except KeyError: pass """)) self.coverage_injector.inject(node) try_node = node.body[0] try_body_el = try_node.body[0] except_node = try_node.handlers[0] except_body_el = except_node.body[0] self.assert_covered([try_node, try_body_el, except_node, except_body_el])
def test_class_def_coverage(self): node = utils.create_ast(utils.f(""" class X(object): def foo(x): pass """)) self.coverage_injector.inject(node) class_node = node.body[0] base_node = class_node.bases[0] func_body_el = class_node.body[0].body[0] self.assert_covered([class_node, base_node]) self.assert_not_covered([func_body_el])
def test_func_def_coverage(self): node = utils.create_ast( utils.f(""" def foo(x): pass """)) self.coverage_injector.inject(node) func_node = node.body[0] func_body_el = func_node.body[0] arg_node = func_node.args.args[0] self.assertTrue(self.coverage_injector.is_covered(func_node)) self.assertTrue(self.coverage_injector.is_covered(arg_node)) self.assertFalse(self.coverage_injector.is_covered(func_body_el))
def test_class_def_coverage(self): node = utils.create_ast(utils.f(""" class X(object): def foo(x): pass """)) self.coverage_injector.inject(node) class_node = node.body[0] base_node = class_node.bases[0] func_body_el = class_node.body[0].body[0] self.assertTrue(self.coverage_injector.is_covered(class_node)) self.assertTrue(self.coverage_injector.is_covered(base_node)) self.assertFalse(self.coverage_injector.is_covered(func_body_el))
def test_while__coverage(self): node = utils.create_ast(utils.f(""" while False: pass else: pass """)) self.coverage_injector.inject(node) while_node = node.body[0] while_test_node = while_node.test while_body_el = while_node.body[0] while_else_body_el = while_node.orelse[0] self.assertTrue(self.coverage_injector.is_covered(while_test_node)) self.assertFalse(self.coverage_injector.is_covered(while_body_el)) self.assertTrue(self.coverage_injector.is_covered(while_else_body_el))
def test_while__coverage(self): node = utils.create_ast(utils.f(""" while False: pass else: pass """)) self.coverage_injector.inject(node) while_node = node.body[0] while_test_node = while_node.test while_body_el = while_node.body[0] while_else_body_el = while_node.orelse[0] self.assert_covered([while_test_node, while_else_body_el]) self.assert_not_covered([while_body_el])
def test_class_def_coverage(self): node = utils.create_ast( utils.f(""" class X(object): def foo(x): pass """)) self.coverage_injector.inject(node) class_node = node.body[0] base_node = class_node.bases[0] func_body_el = class_node.body[0].body[0] self.assertTrue(self.coverage_injector.is_covered(class_node)) self.assertTrue(self.coverage_injector.is_covered(base_node)) self.assertFalse(self.coverage_injector.is_covered(func_body_el))
def test_for_coverage(self): node = utils.create_ast(utils.f(""" for x in []: pass else: pass """)) self.coverage_injector.inject(node) for_node = node.body[0] for_body_el = for_node.body[0] else_body_el = for_node.orelse[0] self.assertTrue(self.coverage_injector.is_covered(for_node)) self.assertTrue(self.coverage_injector.is_covered(for_node.target)) self.assertTrue(self.coverage_injector.is_covered(for_node.iter)) self.assertFalse(self.coverage_injector.is_covered(for_body_el)) self.assertTrue(self.coverage_injector.is_covered(else_body_el))
def test_except_coverage(self): node = utils.create_ast(utils.f(""" try: raise KeyError except KeyError: pass """)) self.coverage_injector.inject(node) try_node = node.body[0] try_body_el = try_node.body[0] except_node = try_node.handlers[0] except_body_el = except_node.body[0] self.assertTrue(self.coverage_injector.is_covered(try_node)) self.assertTrue(self.coverage_injector.is_covered(try_body_el)) self.assertTrue(self.coverage_injector.is_covered(except_node)) self.assertTrue(self.coverage_injector.is_covered(except_body_el))
def test_for_coverage(self): node = utils.create_ast( utils.f(""" for x in []: pass else: pass """)) self.coverage_injector.inject(node) for_node = node.body[0] for_body_el = for_node.body[0] else_body_el = for_node.orelse[0] self.assertTrue(self.coverage_injector.is_covered(for_node)) self.assertTrue(self.coverage_injector.is_covered(for_node.target)) self.assertTrue(self.coverage_injector.is_covered(for_node.iter)) self.assertFalse(self.coverage_injector.is_covered(for_body_el)) self.assertTrue(self.coverage_injector.is_covered(else_body_el))
def test_if_coverage(self): node = utils.create_ast(utils.f(""" if False: pass elif True: pass else: pass """)) self.coverage_injector.inject(node) if_node = node.body[0] first_if_test_node = if_node.test first_if_body_el = if_node.body[0] second_if_node = if_node.orelse[0] second_if_test_node = second_if_node.test second_if_body_el = second_if_node.body[0] else_body_el = second_if_node.orelse[0] self.assertTrue(self.coverage_injector.is_covered(first_if_test_node)) self.assertFalse(self.coverage_injector.is_covered(first_if_body_el)) self.assertTrue(self.coverage_injector.is_covered(second_if_test_node)) self.assertTrue(self.coverage_injector.is_covered(second_if_body_el)) self.assertFalse(self.coverage_injector.is_covered(else_body_el))
def test_not_change_position_if_single_statement(self): self.assert_mutation(utils.f(""" class A: def foo(self): super().foo() """), [])