Exemplo n.º 1
0
    def test_run(self):
        coverage_injector = coverage.CoverageInjector()

        class A:
            def x(self):
                coverage_injector.covered_nodes.add(1)

        class ATest(unittest.TestCase):
            def test_x(self):
                A().x()

            def test_y(self):
                pass

        result = coverage.CoverageTestResult(
            coverage_injector=coverage_injector)
        suite = unittest.TestSuite()
        test_x = ATest(methodName='test_x')
        suite.addTest(test_x)
        test_y = ATest(methodName='test_y')
        suite.addTest(test_y)

        suite.run(result)

        self.assertEqual(coverage_injector.covered_nodes, {1})
        self.assertEqual(result.test_covered_nodes[repr(test_x)], {1})
        self.assertFalse(result.test_covered_nodes[repr(test_y)])
Exemplo n.º 2
0
    def assert_mutation(self, original, mutants, lines=None, operator=None, with_coverage=False, with_exec=False):
        original_ast = utils.create_ast(original)
        if with_coverage:
            coverage_injector = coverage.CoverageInjector()
            coverage_injector.inject(original_ast)
        else:
            coverage_injector = None
        if not operator:
            operator = self.__class__.op
        if isinstance(mutants, str):
            mutants = [mutants]
        mutants = list(map(codegen.remove_extra_lines, mutants))
        module = None
        if with_exec:
            module = utils.create_module(original_ast)
        for mutation, mutatnt in operator.mutate(original_ast, coverage_injector=coverage_injector, module=module):
            mutant_code = codegen.remove_extra_lines(codegen.to_source(mutatnt))
            msg = '\n\nMutant:\n\n' + mutant_code + '\n\nNot found in:'
            for other_mutant in mutants:
                msg += '\n\n----------\n\n' + other_mutant
            self.assertIn(mutant_code, mutants, msg)
            mutants.remove(mutant_code)
            self.assert_location(mutatnt)
            if not lines is None:
                self.assert_mutation_lineo(mutation.node.lineno, lines)

        self.assertListEqual(mutants, [], 'did not generate all mutants')
Exemplo n.º 3
0
 def inject_coverage(self, target_ast, target_module):
     if not self.mutate_covered:
         return None, None
     coverage_injector = coverage.CoverageInjector()
     coverage_module = coverage_injector.inject(target_ast, target_module.__name__)
     suite = self.create_test_suite(coverage_module)
     with self.stdout_manager:
         coverage_result = suite.run_with_coverage(coverage_injector=coverage_injector)
     return coverage_injector, coverage_result
Exemplo n.º 4
0
 def setUp(self):
     self.coverage_injector = coverage.CoverageInjector()