diff options
author | Ned Batchelder <ned@nedbatchelder.com> | 2017-03-08 21:06:27 -0500 |
---|---|---|
committer | Ned Batchelder <ned@nedbatchelder.com> | 2017-03-08 21:06:27 -0500 |
commit | 3ebdde3ae5bbac6744a8c71092bd56caa47c007f (patch) | |
tree | 397642178000ba4fd9e19135a93dfd8884ddeaec | |
parent | 8d7ff030459280d2f20b9c4facf7a21ede37a3b0 (diff) | |
download | python-coveragepy-3ebdde3ae5bbac6744a8c71092bd56caa47c007f.tar.gz |
Move fail-under tests from process-oriented integration tests to fake-oriented unit tests
-rw-r--r-- | tests/test_cmdline.py | 59 | ||||
-rw-r--r-- | tests/test_process.py | 56 |
2 files changed, 61 insertions, 54 deletions
diff --git a/tests/test_cmdline.py b/tests/test_cmdline.py index abcc79f..a9b02bc 100644 --- a/tests/test_cmdline.py +++ b/tests/test_cmdline.py @@ -9,6 +9,7 @@ import sys import textwrap import mock +import pytest import coverage import coverage.cmdline @@ -718,3 +719,61 @@ class CmdMainTest(CoverageTest): def test_exit(self): ret = coverage.cmdline.main(['exit']) self.assertEqual(ret, 23) + + +class CoverageReportingFake(object): + """A fake Coverage and Coverage.coverage test double.""" + # pylint: disable=missing-docstring + def __init__(self, report_result, html_result, xml_result): + self.report_result = report_result + self.html_result = html_result + self.xml_result = xml_result + + def Coverage(self, *args_unused, **kwargs_unused): + return self + + coverage = Coverage # shouldn't need this... + + def set_option(self, optname, optvalue): + setattr(self, optname, optvalue) + + def get_option(self, optname): + return getattr(self, optname) + + def load(self): + pass + + def report(self, *args_unused, **kwargs_unused): + return self.report_result + + def html_report(self, *args_unused, **kwargs_unused): + return self.html_result + + def xml_report(self, *args_unused, **kwargs_unused): + return self.xml_result + + +@pytest.mark.parametrize("results, fail_under, cmd, ret", [ + # Command-line switch properly checks the result of reporting functions. + ((20, 30, 40), None, "report --fail-under=19", 0), + ((20, 30, 40), None, "report --fail-under=21", 2), + ((20, 30, 40), None, "html --fail-under=29", 0), + ((20, 30, 40), None, "html --fail-under=31", 2), + ((20, 30, 40), None, "xml --fail-under=39", 0), + ((20, 30, 40), None, "xml --fail-under=41", 2), + # Configuration file setting properly checks the result of reporting. + ((20, 30, 40), 19, "report", 0), + ((20, 30, 40), 21, "report", 2), + ((20, 30, 40), 29, "html", 0), + ((20, 30, 40), 31, "html", 2), + ((20, 30, 40), 39, "xml", 0), + ((20, 30, 40), 41, "xml", 2), + # Command-line overrides configuration. + ((20, 30, 40), 19, "report --fail-under=21", 2), +]) +def test_fail_under(results, fail_under, cmd, ret): + cov = CoverageReportingFake(*results) + if fail_under: + cov.set_option("report:fail_under", fail_under) + ret_actual = command_line(cmd, _covpkg=cov) + assert ret_actual == ret diff --git a/tests/test_process.py b/tests/test_process.py index 3c0eed0..2ecc912 100644 --- a/tests/test_process.py +++ b/tests/test_process.py @@ -973,83 +973,31 @@ class FailUnderTest(CoverageTest): ) def test_report(self): - st, _ = self.run_command_status("coverage report --fail-under=42") - self.assertEqual(st, 0) st, _ = self.run_command_status("coverage report --fail-under=43") self.assertEqual(st, 0) st, _ = self.run_command_status("coverage report --fail-under=44") self.assertEqual(st, 2) - def test_html_report(self): - st, _ = self.run_command_status("coverage html --fail-under=42") - self.assertEqual(st, 0) - st, _ = self.run_command_status("coverage html --fail-under=43") - self.assertEqual(st, 0) - st, _ = self.run_command_status("coverage html --fail-under=44") - self.assertEqual(st, 2) - - def test_xml_report(self): - st, _ = self.run_command_status("coverage xml --fail-under=42") - self.assertEqual(st, 0) - st, _ = self.run_command_status("coverage xml --fail-under=43") - self.assertEqual(st, 0) - st, _ = self.run_command_status("coverage xml --fail-under=44") - self.assertEqual(st, 2) - - def test_fail_under_in_config(self): - self.make_file(".coveragerc", "[report]\nfail_under = 43\n") - st, _ = self.run_command_status("coverage report") - self.assertEqual(st, 0) - - self.make_file(".coveragerc", "[report]\nfail_under = 44\n") - st, _ = self.run_command_status("coverage report") - self.assertEqual(st, 2) - class FailUnderNoFilesTest(CoverageTest): """Test that nothing to report results in an error exit status.""" - def setUp(self): - super(FailUnderNoFilesTest, self).setUp() - self.make_file(".coveragerc", "[report]\nfail_under = 99\n") - def test_report(self): + self.make_file(".coveragerc", "[report]\nfail_under = 99\n") st, out = self.run_command_status("coverage report") self.assertIn('No data to report.', out) self.assertEqual(st, 1) - def test_xml(self): - st, out = self.run_command_status("coverage xml") - self.assertIn('No data to report.', out) - self.assertEqual(st, 1) - - def test_html(self): - st, out = self.run_command_status("coverage html") - self.assertIn('No data to report.', out) - self.assertEqual(st, 1) - class FailUnderEmptyFilesTest(CoverageTest): """Test that empty files produce the proper fail_under exit status.""" - def setUp(self): - super(FailUnderEmptyFilesTest, self).setUp() - + def test_report(self): self.make_file(".coveragerc", "[report]\nfail_under = 99\n") self.make_file("empty.py", "") st, _ = self.run_command_status("coverage run empty.py") self.assertEqual(st, 0) - - def test_report(self): st, _ = self.run_command_status("coverage report") self.assertEqual(st, 2) - def test_xml(self): - st, _ = self.run_command_status("coverage xml") - self.assertEqual(st, 2) - - def test_html(self): - st, _ = self.run_command_status("coverage html") - self.assertEqual(st, 2) - class UnicodeFilePathsTest(CoverageTest): """Tests of using non-ascii characters in the names of files.""" |