diff options
Diffstat (limited to 'tests/test_json.py')
-rw-r--r-- | tests/test_json.py | 144 |
1 files changed, 144 insertions, 0 deletions
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) |