diff options
author | Matt Bachmann <bachmann.matt@gmail.com> | 2019-07-08 23:09:38 -0400 |
---|---|---|
committer | Ned Batchelder <ned@nedbatchelder.com> | 2019-08-31 07:24:08 -0400 |
commit | 9a78a80aaf8f8161b857ebf3cf02dd511181dd07 (patch) | |
tree | d73603d17fc5269a3b8199085bf67c436d99aedf /tests | |
parent | 790f0b30010a3a1f68f4fa7c172ce3b31c7c4b24 (diff) | |
download | python-coveragepy-git-9a78a80aaf8f8161b857ebf3cf02dd511181dd07.tar.gz |
Create a JSON report
Diffstat (limited to 'tests')
-rw-r--r-- | tests/test_cmdline.py | 94 | ||||
-rw-r--r-- | tests/test_config.py | 6 | ||||
-rw-r--r-- | tests/test_json.py | 144 |
3 files changed, 230 insertions, 14 deletions
diff --git a/tests/test_cmdline.py b/tests/test_cmdline.py index 387bf61f..e15c5fcb 100644 --- a/tests/test_cmdline.py +++ b/tests/test_cmdline.py @@ -46,6 +46,10 @@ class BaseCmdLineTest(CoverageTest): ignore_errors=None, include=None, omit=None, morfs=[], outfile=None, contexts=None, ) + _defaults.Coverage().json_report( + ignore_errors=None, include=None, omit=None, morfs=[], outfile=None, + contexts=None, pretty_print=None, show_contexts=None + ) _defaults.Coverage( cover_pylib=None, data_suffix=None, timid=None, branch=None, config_file=True, source=None, include=None, omit=None, debug=None, @@ -69,6 +73,7 @@ class BaseCmdLineTest(CoverageTest): cov.report.return_value = 50.0 cov.html_report.return_value = 50.0 cov.xml_report.return_value = 50.0 + cov.json_report.return_value = 50.0 return mk @@ -667,6 +672,59 @@ class CmdLineTest(BaseCmdLineTest): cov.xml_report(morfs=["mod1", "mod2", "mod3"]) """) + def test_json(self): + # coverage json [-i] [--omit DIR,...] [FILE1 FILE2 ...] + self.cmd_executes("json", """\ + cov = Coverage() + cov.load() + cov.json_report() + """) + self.cmd_executes("json --pretty-print", """\ + cov = Coverage() + cov.load() + cov.json_report(pretty_print=True) + """) + self.cmd_executes("json --pretty-print --show-contexts", """\ + cov = Coverage() + cov.load() + cov.json_report(pretty_print=True, show_contexts=True) + """) + self.cmd_executes("json -i", """\ + cov = Coverage() + cov.load() + cov.json_report(ignore_errors=True) + """) + self.cmd_executes("json -o myjson.foo", """\ + cov = Coverage() + cov.load() + cov.json_report(outfile="myjson.foo") + """) + self.cmd_executes("json -o -", """\ + cov = Coverage() + cov.load() + cov.json_report(outfile="-") + """) + self.cmd_executes("json --omit fooey", """\ + cov = Coverage(omit=["fooey"]) + cov.load() + cov.json_report(omit=["fooey"]) + """) + self.cmd_executes("json --omit fooey,booey", """\ + cov = Coverage(omit=["fooey", "booey"]) + cov.load() + cov.json_report(omit=["fooey", "booey"]) + """) + self.cmd_executes("json mod1", """\ + cov = Coverage() + cov.load() + cov.json_report(morfs=["mod1"]) + """) + self.cmd_executes("json mod1 mod2 mod3", """\ + cov = Coverage() + cov.load() + cov.json_report(morfs=["mod1", "mod2", "mod3"]) + """) + def test_no_arguments_at_all(self): self.cmd_help("", topic="minimum_help", ret=OK) @@ -847,11 +905,12 @@ class CmdMainTest(CoverageTest): class CoverageReportingFake(object): """A fake Coverage.coverage test double.""" # pylint: disable=missing-docstring - def __init__(self, report_result, html_result, xml_result): + def __init__(self, report_result, html_result, xml_result, json_report): self.config = CoverageConfig() self.report_result = report_result self.html_result = html_result self.xml_result = xml_result + self.json_result = json_report def set_option(self, optname, optvalue): self.config.set_option(optname, optvalue) @@ -871,24 +930,31 @@ class CoverageReportingFake(object): def xml_report(self, *args_unused, **kwargs_unused): return self.xml_result + def json_report(self, *args_unused, **kwargs_unused): + return self.json_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), + ((20, 30, 40, 50), None, "report --fail-under=19", 0), + ((20, 30, 40, 50), None, "report --fail-under=21", 2), + ((20, 30, 40, 50), None, "html --fail-under=29", 0), + ((20, 30, 40, 50), None, "html --fail-under=31", 2), + ((20, 30, 40, 50), None, "xml --fail-under=39", 0), + ((20, 30, 40, 50), None, "xml --fail-under=41", 2), + ((20, 30, 40, 50), None, "json --fail-under=49", 0), + ((20, 30, 40, 50), None, "json --fail-under=51", 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), + ((20, 30, 40, 50), 19, "report", 0), + ((20, 30, 40, 50), 21, "report", 2), + ((20, 30, 40, 50), 29, "html", 0), + ((20, 30, 40, 50), 31, "html", 2), + ((20, 30, 40, 50), 39, "xml", 0), + ((20, 30, 40, 50), 41, "xml", 2), + ((20, 30, 40, 50), 49, "json", 0), + ((20, 30, 40, 50), 51, "json", 2), # Command-line overrides configuration. - ((20, 30, 40), 19, "report --fail-under=21", 2), + ((20, 30, 40, 50), 19, "report --fail-under=21", 2), ]) def test_fail_under(results, fail_under, cmd, ret): cov = CoverageReportingFake(*results) diff --git a/tests/test_config.py b/tests/test_config.py index 7b019f94..ebea18a7 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -332,6 +332,10 @@ class ConfigFileTest(UsingModulesMixin, CoverageTest): hello = world ; comments still work. names = Jane/John/Jenny + + [{section}json] + pretty_print = True + show_contexts = True """ # Just some sample setup.cfg text from the docs. @@ -399,6 +403,8 @@ class ConfigFileTest(UsingModulesMixin, CoverageTest): 'names': 'Jane/John/Jenny', }) self.assertEqual(cov.config.get_plugin_options("plugins.another"), {}) + self.assertEqual(cov.config.json_show_contexts, True) + self.assertEqual(cov.config.json_pretty_print, True) def test_config_file_settings(self): self.make_file(".coveragerc", self.LOTSA_SETTINGS.format(section="")) diff --git a/tests/test_json.py b/tests/test_json.py new file mode 100644 index 00000000..1ae5764e --- /dev/null +++ b/tests/test_json.py @@ -0,0 +1,144 @@ +# coding: utf-8 +# Licensed under the Apache License: http://www.apache.org/licenses/LICENSE-2.0 +# For details: https://github.com/nedbat/coveragepy/blob/master/NOTICE.txt + +"""Test json-based summary reporting for coverage.py""" +from datetime import datetime +import json +import os + +import coverage +from tests.coveragetest import UsingModulesMixin, CoverageTest + + +class JsonReportTest(UsingModulesMixin, CoverageTest): + """Tests of the JSON reports from coverage.py.""" + def _assert_expected_json_report(self, cov, expected_result): + """ + Helper for tests that handles the common ceremony so the tests can be clearly show the + consequences of setting various arguments. + """ + self.make_file("a.py", """\ + a = {'b': 1} + if a.get('a'): + b = 1 + """) + a = self.start_import_stop(cov, "a") + output_path = os.path.join(self.temp_dir, "a.json") + cov.json_report(a, outfile=output_path) + with open(output_path) as result_file: + parsed_result = json.load(result_file) + self.assert_recent_datetime( + datetime.strptime(parsed_result['meta']['timestamp'], "%Y-%m-%dT%H:%M:%S.%f") + ) + del (parsed_result['meta']['timestamp']) + assert parsed_result == expected_result + + def test_branch_coverage(self): + cov = coverage.Coverage(branch=True) + expected_result = { + 'meta': { + "version": coverage.__version__, + "branch_coverage": True, + "show_contexts": False, + }, + 'files': { + 'a.py': { + 'executed_lines': [1, 2], + 'missing_lines': [3], + 'excluded_lines': [], + 'summary': { + 'missing_lines': 1, + 'covered_lines': 2, + 'num_statements': 3, + 'num_branches': 2, + 'excluded_lines': 0, + 'num_partial_branches': 1, + 'percent_covered': 60.0 + } + } + }, + 'totals': { + 'missing_lines': 1, + 'covered_lines': 2, + 'num_statements': 3, + 'num_branches': 2, + 'excluded_lines': 0, + 'num_partial_branches': 1, + 'percent_covered': 60.0 + } + } + self._assert_expected_json_report(cov, expected_result) + + def test_simple_line_coverage(self): + cov = coverage.Coverage() + expected_result = { + 'meta': { + "version": coverage.__version__, + "branch_coverage": False, + "show_contexts": False, + }, + 'files': { + 'a.py': { + 'executed_lines': [1, 2], + 'missing_lines': [3], + 'excluded_lines': [], + 'summary': { + 'excluded_lines': 0, + 'missing_lines': 1, + 'covered_lines': 2, + 'num_statements': 3, + 'percent_covered': 66.66666666666667 + } + } + }, + 'totals': { + 'excluded_lines': 0, + 'missing_lines': 1, + 'covered_lines': 2, + 'num_statements': 3, + 'percent_covered': 66.66666666666667 + } + } + self._assert_expected_json_report(cov, expected_result) + + def test_context(self): + cov = coverage.Coverage(context="cool_test") + cov.config.json_show_contexts = True + expected_result = { + 'meta': { + "version": coverage.__version__, + "branch_coverage": False, + "show_contexts": True, + }, + 'files': { + 'a.py': { + 'executed_lines': [1, 2], + 'missing_lines': [3], + 'excluded_lines': [], + "contexts": { + "1": [ + "cool_test" + ], + "2": [ + "cool_test" + ] + }, + 'summary': { + 'excluded_lines': 0, + 'missing_lines': 1, + 'covered_lines': 2, + 'num_statements': 3, + 'percent_covered': 66.66666666666667 + } + } + }, + 'totals': { + 'excluded_lines': 0, + 'missing_lines': 1, + 'covered_lines': 2, + 'num_statements': 3, + 'percent_covered': 66.66666666666667 + } + } + self._assert_expected_json_report(cov, expected_result) |