summaryrefslogtreecommitdiff
path: root/tests/test_plugins.py
diff options
context:
space:
mode:
authorNed Batchelder <ned@nedbatchelder.com>2021-01-31 07:16:56 -0500
committerNed Batchelder <ned@nedbatchelder.com>2021-01-31 07:16:56 -0500
commit843de4ea235e7eee3ff24a39a2f8b14da9ef0db0 (patch)
tree8a4d8435595334318b5e38ef42da803e512acd4f /tests/test_plugins.py
parent4fc64a97ce779c2d6bb972f0003b9b9f00e62c3a (diff)
downloadpython-coveragepy-git-843de4ea235e7eee3ff24a39a2f8b14da9ef0db0.tar.gz
refactor: unittest2pytest -w tests
One step of moving to pure pytest tests.
Diffstat (limited to 'tests/test_plugins.py')
-rw-r--r--tests/test_plugins.py153
1 files changed, 76 insertions, 77 deletions
diff --git a/tests/test_plugins.py b/tests/test_plugins.py
index 813d370e..3472522b 100644
--- a/tests/test_plugins.py
+++ b/tests/test_plugins.py
@@ -18,6 +18,7 @@ import coverage.plugin
from tests.coveragetest import CoverageTest
from tests.helpers import CheckUniqueFilenames
+import pytest
class FakeConfig(object):
@@ -53,10 +54,10 @@ class LoadPluginsTest(CoverageTest):
config = FakeConfig("plugin1", {})
plugins = Plugins.load_plugins([], config)
- self.assertFalse(plugins)
+ assert not plugins
plugins = Plugins.load_plugins(["plugin1"], config)
- self.assertTrue(plugins)
+ assert plugins
def test_importing_and_configuring(self):
self.make_file("plugin1.py", """\
@@ -74,10 +75,10 @@ class LoadPluginsTest(CoverageTest):
config = FakeConfig("plugin1", {'a': 'hello'})
plugins = list(Plugins.load_plugins(["plugin1"], config))
- self.assertEqual(len(plugins), 1)
- self.assertEqual(plugins[0].this_is, "me")
- self.assertEqual(plugins[0].options, {'a': 'hello'})
- self.assertEqual(config.asked_for, ['plugin1'])
+ assert len(plugins) == 1
+ assert plugins[0].this_is == "me"
+ assert plugins[0].options == {'a': 'hello'}
+ assert config.asked_for == ['plugin1']
def test_importing_and_configuring_more_than_one(self):
self.make_file("plugin1.py", """\
@@ -105,23 +106,23 @@ class LoadPluginsTest(CoverageTest):
config = FakeConfig("plugin1", {'a': 'hello'})
plugins = list(Plugins.load_plugins(["plugin1", "plugin2"], config))
- self.assertEqual(len(plugins), 2)
- self.assertEqual(plugins[0].this_is, "me")
- self.assertEqual(plugins[0].options, {'a': 'hello'})
- self.assertEqual(plugins[1].options, {})
- self.assertEqual(config.asked_for, ['plugin1', 'plugin2'])
+ assert len(plugins) == 2
+ assert plugins[0].this_is == "me"
+ assert plugins[0].options == {'a': 'hello'}
+ assert plugins[1].options == {}
+ assert config.asked_for == ['plugin1', 'plugin2']
# The order matters...
config = FakeConfig("plugin1", {'a': 'second'})
plugins = list(Plugins.load_plugins(["plugin2", "plugin1"], config))
- self.assertEqual(len(plugins), 2)
- self.assertEqual(plugins[0].options, {})
- self.assertEqual(plugins[1].this_is, "me")
- self.assertEqual(plugins[1].options, {'a': 'second'})
+ assert len(plugins) == 2
+ assert plugins[0].options == {}
+ assert plugins[1].this_is == "me"
+ assert plugins[1].options == {'a': 'second'}
def test_cant_import(self):
- with self.assertRaisesRegex(ImportError, "No module named '?plugin_not_there'?"):
+ with pytest.raises(ImportError, match="No module named '?plugin_not_there'?"):
_ = Plugins.load_plugins(["plugin_not_there"], None)
def test_plugin_must_define_coverage_init(self):
@@ -130,7 +131,7 @@ class LoadPluginsTest(CoverageTest):
Nothing = 0
""")
msg_pat = "Plugin module 'no_plugin' didn't define a coverage_init function"
- with self.assertRaisesRegex(CoverageException, msg_pat):
+ with pytest.raises(CoverageException, match=msg_pat):
list(Plugins.load_plugins(["no_plugin"], None))
@@ -156,11 +157,11 @@ class PluginTest(CoverageTest):
cov.stop() # pragma: nested
with open("evidence.out") as f:
- self.assertEqual(f.read(), "we are here!")
+ assert f.read() == "we are here!"
def test_missing_plugin_raises_import_error(self):
# Prove that a missing plugin will raise an ImportError.
- with self.assertRaisesRegex(ImportError, "No module named '?does_not_exist_woijwoicweo'?"):
+ with pytest.raises(ImportError, match="No module named '?does_not_exist_woijwoicweo'?"):
cov = coverage.Coverage()
cov.set_option("run:plugins", ["does_not_exist_woijwoicweo"])
cov.start()
@@ -169,7 +170,7 @@ class PluginTest(CoverageTest):
def test_bad_plugin_isnt_hidden(self):
# Prove that a plugin with an error in it will raise the error.
self.make_file("plugin_over_zero.py", "1/0")
- with self.assertRaises(ZeroDivisionError):
+ with pytest.raises(ZeroDivisionError):
cov = coverage.Coverage()
cov.set_option("run:plugins", ["plugin_over_zero"])
cov.start()
@@ -195,16 +196,16 @@ class PluginTest(CoverageTest):
out_lines = [line.strip() for line in debug_out.getvalue().splitlines()]
if env.C_TRACER:
- self.assertIn('plugins.file_tracers: plugin_sys_info.Plugin', out_lines)
+ assert 'plugins.file_tracers: plugin_sys_info.Plugin' in out_lines
else:
- self.assertIn('plugins.file_tracers: plugin_sys_info.Plugin (disabled)', out_lines)
- self.assertIn('plugins.configurers: -none-', out_lines)
+ assert 'plugins.file_tracers: plugin_sys_info.Plugin (disabled)' in out_lines
+ assert 'plugins.configurers: -none-' in out_lines
expected_end = [
"-- sys: plugin_sys_info.Plugin -------------------------------",
"hello: world",
"-- end -------------------------------------------------------",
]
- self.assertEqual(expected_end, out_lines[-len(expected_end):])
+ assert expected_end == out_lines[-len(expected_end):]
def test_plugin_with_no_sys_info(self):
self.make_file("plugin_no_sys_info.py", """\
@@ -224,13 +225,13 @@ class PluginTest(CoverageTest):
cov.stop() # pragma: nested
out_lines = [line.strip() for line in debug_out.getvalue().splitlines()]
- self.assertIn('plugins.file_tracers: -none-', out_lines)
- self.assertIn('plugins.configurers: plugin_no_sys_info.Plugin', out_lines)
+ assert 'plugins.file_tracers: -none-' in out_lines
+ assert 'plugins.configurers: plugin_no_sys_info.Plugin' in out_lines
expected_end = [
"-- sys: plugin_no_sys_info.Plugin ----------------------------",
"-- end -------------------------------------------------------",
]
- self.assertEqual(expected_end, out_lines[-len(expected_end):])
+ assert expected_end == out_lines[-len(expected_end):]
def test_local_files_are_importable(self):
self.make_file("importing_plugin.py", """\
@@ -249,9 +250,9 @@ class PluginTest(CoverageTest):
self.make_file("main_file.py", "print('MAIN')")
out = self.run_command("coverage run main_file.py")
- self.assertEqual(out, "MAIN\n")
+ assert out == "MAIN\n"
out = self.run_command("coverage html")
- self.assertEqual(out, "")
+ assert out == ""
class PluginWarningOnPyTracer(CoverageTest):
@@ -304,11 +305,11 @@ class GoodFileTracerTest(FileTracerTest):
self.start_import_stop(cov, "simple")
_, statements, missing, _ = cov.analysis("simple.py")
- self.assertEqual(statements, [1, 2, 3])
- self.assertEqual(missing, [])
+ assert statements == [1, 2, 3]
+ assert 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])
+ assert statements == [105, 106, 107, 205, 206, 207]
def make_render_and_caller(self):
"""Make the render.py and caller.py files we need."""
@@ -370,21 +371,21 @@ class GoodFileTracerTest(FileTracerTest):
# 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()))
+ assert statements == [1, 2, 3, 4, 5, 6, 7]
+ assert missing == [1, 2, 3, 6, 7]
+ assert "foo_7.html" in 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()))
+ assert statements == [1, 2, 3, 4]
+ assert missing == [1, 4]
+ assert "bar_4.html" in line_counts(cov.get_data())
- self.assertNotIn("quux_5.html", line_counts(cov.get_data()))
+ assert "quux_5.html" not in 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()))
+ assert statements == [1, 2, 3]
+ assert missing == [1]
+ assert "uni_3.html" in line_counts(cov.get_data())
def test_plugin2_with_branch(self):
self.make_render_and_caller()
@@ -400,12 +401,12 @@ class GoodFileTracerTest(FileTracerTest):
# 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, {1, 2, 3, 4, 5, 6, 7})
+ assert analysis.statements == {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(), [])
+ assert analysis.has_arcs() == True
+ assert analysis.arc_possibilities() == []
- self.assertEqual(analysis.missing, {1, 2, 3, 6, 7})
+ assert analysis.missing == {1, 2, 3, 6, 7}
def test_plugin2_with_text_report(self):
self.make_render_and_caller()
@@ -426,8 +427,8 @@ class GoodFileTracerTest(FileTracerTest):
'--------------------------------------------------------',
'TOTAL 11 7 0 0 36%',
]
- self.assertEqual(expected, report)
- self.assertAlmostEqual(total, 36.36, places=2)
+ assert expected == report
+ assert round(abs(total-36.36), 2) == 0
def test_plugin2_with_html_report(self):
self.make_render_and_caller()
@@ -438,7 +439,7 @@ class GoodFileTracerTest(FileTracerTest):
self.start_import_stop(cov, "caller")
total = cov.html_report(include=["*.html"], omit=["uni*.html"])
- self.assertAlmostEqual(total, 36.36, places=2)
+ assert round(abs(total-36.36), 2) == 0
self.assert_exists("htmlcov/index.html")
self.assert_exists("htmlcov/bar_4_html.html")
@@ -453,7 +454,7 @@ class GoodFileTracerTest(FileTracerTest):
self.start_import_stop(cov, "caller")
total = cov.xml_report(include=["*.html"], omit=["uni*.html"])
- self.assertAlmostEqual(total, 36.36, places=2)
+ assert round(abs(total-36.36), 2) == 0
dom = ElementTree.parse("coverage.xml")
classes = {}
@@ -525,8 +526,8 @@ class GoodFileTracerTest(FileTracerTest):
'-----------------------------------------------',
'TOTAL 6 3 50%',
]
- self.assertEqual(expected, report)
- self.assertEqual(total, 50)
+ assert expected == report
+ assert total == 50
def test_find_unexecuted(self):
self.make_file("unexecuted_plugin.py", """\
@@ -567,17 +568,17 @@ class GoodFileTracerTest(FileTracerTest):
# The file we executed claims to have run line 999.
_, statements, missing, _ = cov.analysis("foo.py")
- self.assertEqual(statements, [99, 999, 9999])
- self.assertEqual(missing, [99, 9999])
+ assert statements == [99, 999, 9999]
+ assert missing == [99, 9999]
# The completely missing file is in the results.
_, statements, missing, _ = cov.analysis("chimera.py")
- self.assertEqual(statements, [99, 999, 9999])
- self.assertEqual(missing, [99, 999, 9999])
+ assert statements == [99, 999, 9999]
+ assert missing == [99, 999, 9999]
# But completely new filenames are not in the results.
- self.assertEqual(len(cov.get_data().measured_files()), 3)
- with self.assertRaises(CoverageException):
+ assert len(cov.get_data().measured_files()) == 3
+ with pytest.raises(CoverageException):
cov.analysis("fictional.py")
@@ -641,7 +642,7 @@ class BadFileTracerTest(FileTracerTest):
if our_error:
errors = stderr.count("# Oh noes!")
# The exception we're causing should only appear once.
- self.assertEqual(errors, 1)
+ assert errors == 1
# There should be a warning explaining what's happening, but only one.
# The message can be in two forms:
@@ -650,12 +651,12 @@ class BadFileTracerTest(FileTracerTest):
# Disabling plug-in '...' due to an exception:
msg = "Disabling plug-in '%s.%s' due to " % (module_name, plugin_name)
warnings = stderr.count(msg)
- self.assertEqual(warnings, 1)
+ assert warnings == 1
if excmsg:
- self.assertIn(excmsg, stderr)
+ assert excmsg in stderr
if excmsgs:
- self.assertTrue(any(em in stderr for em in excmsgs), "expected one of %r" % excmsgs)
+ assert any(em in stderr for em in excmsgs), "expected one of %r" % excmsgs
def test_file_tracer_has_no_file_tracer_method(self):
self.make_file("bad_plugin.py", """\
@@ -703,7 +704,7 @@ class BadFileTracerTest(FileTracerTest):
""")
cov = self.run_plugin("bad_plugin")
expected_msg = "Plugin 'bad_plugin.Plugin' needs to implement file_reporter()"
- with self.assertRaisesRegex(NotImplementedError, expected_msg):
+ with pytest.raises(NotImplementedError, match=expected_msg):
cov.report()
def test_file_tracer_fails(self):
@@ -942,8 +943,8 @@ class ConfigurerPluginTest(CoverageTest):
cov.start()
cov.stop() # pragma: nested
excluded = cov.get_option("report:exclude_lines")
- self.assertIn("pragma: custom", excluded)
- self.assertIn("pragma: or whatever", excluded)
+ assert "pragma: custom" in excluded
+ assert "pragma: or whatever" in excluded
class DynamicContextPluginTest(CoverageTest):
@@ -1048,16 +1049,14 @@ class DynamicContextPluginTest(CoverageTest):
# Labeled coverage is collected
data = cov.get_data()
filenames = self.get_measured_filenames(data)
- self.assertEqual(
- ['', 'doctest:HTML_TAG', 'test:HTML_TAG', 'test:RENDERERS'],
- sorted(data.measured_contexts()),
- )
+ assert ['', 'doctest:HTML_TAG', 'test:HTML_TAG', 'test:RENDERERS'] == \
+ sorted(data.measured_contexts())
data.set_query_context("doctest:HTML_TAG")
- self.assertEqual([2], data.lines(filenames['rendering.py']))
+ assert [2] == data.lines(filenames['rendering.py'])
data.set_query_context("test:HTML_TAG")
- self.assertEqual([2], data.lines(filenames['rendering.py']))
+ assert [2] == data.lines(filenames['rendering.py'])
data.set_query_context("test:RENDERERS")
- self.assertEqual([2, 5, 8, 11], sorted(data.lines(filenames['rendering.py'])))
+ assert [2, 5, 8, 11] == sorted(data.lines(filenames['rendering.py']))
def test_static_context(self):
self.make_plugin_capitalized_testnames('plugin_tests.py')
@@ -1078,7 +1077,7 @@ class DynamicContextPluginTest(CoverageTest):
'mytests|test:HTML_TAG',
'mytests|test:RENDERERS',
]
- self.assertEqual(expected, sorted(data.measured_contexts()))
+ assert expected == sorted(data.measured_contexts())
def test_plugin_with_test_function(self):
self.make_plugin_capitalized_testnames('plugin_tests.py')
@@ -1103,11 +1102,11 @@ class DynamicContextPluginTest(CoverageTest):
'testsuite.test_html_tag',
'testsuite.test_renderers',
]
- self.assertEqual(expected, sorted(data.measured_contexts()))
+ assert expected == sorted(data.measured_contexts())
def assert_context_lines(context, lines):
data.set_query_context(context)
- self.assertEqual(lines, sorted(data.lines(filenames['rendering.py'])))
+ assert lines == sorted(data.lines(filenames['rendering.py']))
assert_context_lines("doctest:HTML_TAG", [2])
assert_context_lines("testsuite.test_html_tag", [2])
@@ -1141,11 +1140,11 @@ class DynamicContextPluginTest(CoverageTest):
'test:HTML_TAG',
'test:RENDERERS',
]
- self.assertEqual(expected, sorted(data.measured_contexts()))
+ assert expected == sorted(data.measured_contexts())
def assert_context_lines(context, lines):
data.set_query_context(context)
- self.assertEqual(lines, sorted(data.lines(filenames['rendering.py'])))
+ assert lines == sorted(data.lines(filenames['rendering.py']))
assert_context_lines("test:HTML_TAG", [2])
assert_context_lines("test:RENDERERS", [2, 5, 8, 11])