Skip to content

Commit f3d8d83

Browse files
mwg-reaionelmc
authored andcommitted
Add support for JSON reporter
This exposes an additional reporter that's provided by coverage.py. THe implementation is basically a carbon copy of the code that powers the XML implementation.
1 parent dec02ab commit f3d8d83

File tree

5 files changed

+34
-7
lines changed

5 files changed

+34
-7
lines changed

docs/config.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ The complete list of command line options is:
5656

5757
--cov=PATH Measure coverage for filesystem path. (multi-allowed)
5858
--cov-report=type Type of report to generate: term, term-missing,
59-
annotate, html, xml, lcov (multi-allowed). term, term-
59+
annotate, html, xml, json, lcov (multi-allowed). term, term-
6060
missing may be followed by ":skip-covered". annotate,
61-
html, xml and lcov may be followed by ":DEST" where DEST
61+
html, xml, json and lcov may be followed by ":DEST" where DEST
6262
specifies the output location. Use --cov-report= to
6363
not generate any output.
6464
--cov-config=path Config file for coverage. Default: .coveragerc

docs/reporting.rst

+4-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Reporting
33

44
It is possible to generate any combination of the reports for a single test run.
55

6-
The available reports are terminal (with or without missing line numbers shown), HTML, XML, LCOV and
6+
The available reports are terminal (with or without missing line numbers shown), HTML, XML, JSON, LCOV and
77
annotated source code.
88

99
The terminal report without line numbers (default)::
@@ -53,16 +53,18 @@ These four report options output to files without showing anything on the termin
5353

5454
pytest --cov-report html
5555
--cov-report xml
56+
--cov-report json
5657
--cov-report lcov
5758
--cov-report annotate
5859
--cov=myproj tests/
5960

60-
The output location for each of these reports can be specified. The output location for the XML and LCOV
61+
The output location for each of these reports can be specified. The output location for the XML, JSON and LCOV
6162
report is a file. Where as the output location for the HTML and annotated source code reports are
6263
directories::
6364

6465
pytest --cov-report html:cov_html
6566
--cov-report xml:cov.xml
67+
--cov-report json:cov.json
6668
--cov-report lcov:cov.info
6769
--cov-report annotate:cov_annotate
6870
--cov=myproj tests/

src/pytest_cov/engine.py

+7
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,13 @@ def summary(self, stream):
196196
total = self.cov.xml_report(ignore_errors=True, outfile=output)
197197
stream.write('Coverage XML written to file %s\n' % (self.cov.config.xml_output if output is None else output))
198198

199+
# Produce json report if wanted
200+
if 'json' in self.cov_report:
201+
output = self.cov_report['json']
202+
with _backup(self.cov, "config"):
203+
total = self.cov.json_report(ignore_errors=True, outfile=output)
204+
stream.write('Coverage JSON written to file %s\n' % (self.cov.config.json_output if output is None else output))
205+
199206
# Produce lcov report if wanted.
200207
if 'lcov' in self.cov_report:
201208
output = self.cov_report['lcov']

src/pytest_cov/plugin.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class CovReportWarning(PytestCovWarning):
2929

3030

3131
def validate_report(arg):
32-
file_choices = ['annotate', 'html', 'xml', 'lcov']
32+
file_choices = ['annotate', 'html', 'xml', 'json', 'lcov']
3333
term_choices = ['term', 'term-missing']
3434
term_modifier_choices = ['skip-covered']
3535
all_choices = term_choices + file_choices
@@ -99,9 +99,9 @@ def pytest_addoption(parser):
9999
group.addoption('--cov-report', action=StoreReport, default={},
100100
metavar='TYPE', type=validate_report,
101101
help='Type of report to generate: term, term-missing, '
102-
'annotate, html, xml, lcov (multi-allowed). '
102+
'annotate, html, xml, json, lcov (multi-allowed). '
103103
'term, term-missing may be followed by ":skip-covered". '
104-
'annotate, html, xml and lcov may be followed by ":DEST" '
104+
'annotate, html, xml, json and lcov may be followed by ":DEST" '
105105
'where DEST specifies the output location. '
106106
'Use --cov-report= to not generate any output.')
107107
group.addoption('--cov-config', action='store', default='.coveragerc',

tests/test_pytest_cov.py

+18
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ def test_foo(cov):
151151
PARENT_SCRIPT_RESULT = '9 * 100%'
152152
DEST_DIR = 'cov_dest'
153153
XML_REPORT_NAME = 'cov.xml'
154+
JSON_REPORT_NAME = 'cov.json'
154155
LCOV_REPORT_NAME = 'cov.info'
155156

156157
xdist_params = pytest.mark.parametrize('opts', [
@@ -346,6 +347,23 @@ def test_xml_output_dir(testdir):
346347
assert result.ret == 0
347348

348349

350+
def test_json_output_dir(testdir):
351+
script = testdir.makepyfile(SCRIPT)
352+
353+
result = testdir.runpytest('-v',
354+
'--cov=%s' % script.dirpath(),
355+
'--cov-report=json:' + JSON_REPORT_NAME,
356+
script)
357+
358+
result.stdout.fnmatch_lines([
359+
'*- coverage: platform *, python * -*',
360+
'Coverage JSON written to file ' + JSON_REPORT_NAME,
361+
'*10 passed*',
362+
])
363+
assert testdir.tmpdir.join(JSON_REPORT_NAME).check()
364+
assert result.ret == 0
365+
366+
349367
@pytest.mark.skipif("coverage.version_info < (6, 3)")
350368
def test_lcov_output_dir(testdir):
351369
script = testdir.makepyfile(SCRIPT)

0 commit comments

Comments
 (0)