def test_plugin1(self): self.make_file("simple.py", """\ import try_xyz a = 1 b = 2 """) self.make_file("try_xyz.py", """\ c = 3 d = 4 """) cov = coverage.Coverage() CheckUniqueFilenames.hook(cov, '_should_trace') CheckUniqueFilenames.hook(cov, '_check_include_omit_etc') cov.set_option("run:plugins", ["tests.plugin1"]) # Import the Python file, executing it. self.start_import_stop(cov, "simple") _, statements, missing, _ = cov.analysis("simple.py") self.assertEqual(statements, [1, 2, 3]) self.assertEqual(missing, []) zzfile = os.path.abspath(os.path.join("/src", "try_ABC.zz")) _, statements, _, _ = cov.analysis(zzfile) self.assertEqual(statements, [105, 106, 107, 205, 206, 207])
def test_plugin2(self): self.make_render_and_caller() cov = coverage.Coverage(omit=["*quux*"]) CheckUniqueFilenames.hook(cov, '_should_trace') CheckUniqueFilenames.hook(cov, '_check_include_omit_etc') cov.set_option("run:plugins", ["tests.plugin2"]) self.start_import_stop(cov, "caller") # The way plugin2 works, a file named foo_7.html will be claimed to # have 7 lines in it. If render() was called with line number 4, # then the plugin will claim that lines 4 and 5 were executed. _, statements, missing, _ = cov.analysis("foo_7.html") self.assertEqual(statements, [1, 2, 3, 4, 5, 6, 7]) self.assertEqual(missing, [1, 2, 3, 6, 7]) self.assertIn("foo_7.html", line_counts(cov.get_data())) _, statements, missing, _ = cov.analysis("bar_4.html") self.assertEqual(statements, [1, 2, 3, 4]) self.assertEqual(missing, [1, 4]) self.assertIn("bar_4.html", line_counts(cov.get_data())) self.assertNotIn("quux_5.html", line_counts(cov.get_data())) _, statements, missing, _ = cov.analysis("uni_3.html") self.assertEqual(statements, [1, 2, 3]) self.assertEqual(missing, [1]) self.assertIn("uni_3.html", line_counts(cov.get_data()))
def test_detect_duplicate(self): stub = self.Stub(23) CheckUniqueFilenames.hook(stub, "method") # Two method calls with different names are fine. assert stub.method("file1") == (23, "file1", 17, "hello") assert stub.method("file2", 1723, b="what") == (23, "file2", 1723, "what") # A duplicate file name trips an assertion. with pytest.raises(AssertionError): stub.method("file1")
def test_detect_duplicate(self): stub = self.Stub(23) CheckUniqueFilenames.hook(stub, "method") # Two method calls with different names are fine. assert stub.method("file1") == (23, "file1", 17, "hello") assert stub.method("file2", 1723, b="what") == (23, "file2", 1723, "what") # A duplicate file name trips an assertion. with self.assertRaises(AssertionError): stub.method("file1")
def test_should_trace_cache(self): # The tracers should only invoke should_trace once for each file name. # Make some files that invoke each other. self.make_file("f1.py", """\ def f1(x, f): return f(x) """) self.make_file("f2.py", """\ import f1 def func(x): return f1.f1(x, otherfunc) def otherfunc(x): return x*x for i in range(10): func(i) """) # Trace one file, but not the other. CheckUniqueFilenames will assert # that _should_trace hasn't been called twice for the same file. cov = coverage.Coverage(include=["f1.py"]) should_trace_hook = CheckUniqueFilenames.hook(cov, '_should_trace') # Import the Python file, executing it. self.start_import_stop(cov, "f2") # Double-check that our files were checked. abs_files = set(os.path.abspath(f) for f in should_trace_hook.filenames) self.assertIn(os.path.abspath("f1.py"), abs_files) self.assertIn(os.path.abspath("f2.py"), abs_files)
def test_plugin2_with_branch(self): self.make_render_and_caller() cov = coverage.Coverage(branch=True, omit=["*quux*"]) CheckUniqueFilenames.hook(cov, '_should_trace') CheckUniqueFilenames.hook(cov, '_check_include_omit_etc') cov.set_option("run:plugins", ["tests.plugin2"]) self.start_import_stop(cov, "caller") # The way plugin2 works, a file named foo_7.html will be claimed to # have 7 lines in it. If render() was called with line number 4, # then the plugin will claim that lines 4 and 5 were executed. analysis = cov._analyze("foo_7.html") self.assertEqual(analysis.statements, set([1, 2, 3, 4, 5, 6, 7])) # Plugins don't do branch coverage yet. self.assertEqual(analysis.has_arcs(), True) self.assertEqual(analysis.arc_possibilities(), []) self.assertEqual(analysis.missing, set([1, 2, 3, 6, 7]))
def test_should_trace_cache(self): # The tracers should only invoke should_trace once for each file name. # Make some files that invoke each other. self.make_file( "f1.py", """\ def f1(x, f): return f(x) """) self.make_file( "f2.py", """\ import f1 def func(x): return f1.f1(x, otherfunc) def otherfunc(x): return x*x for i in range(10): func(i) """) # Trace one file, but not the other. CheckUniqueFilenames will assert # that _should_trace hasn't been called twice for the same file. cov = coverage.Coverage(include=["f1.py"]) should_trace_hook = CheckUniqueFilenames.hook(cov, '_should_trace') # Import the Python file, executing it. self.start_import_stop(cov, "f2") # Double-check that our files were checked. abs_files = set( os.path.abspath(f) for f in should_trace_hook.filenames) self.assertIn(os.path.abspath("f1.py"), abs_files) self.assertIn(os.path.abspath("f2.py"), abs_files)