Skip to content

Commit ad25036

Browse files
committed
Add number of collected items and deselected flag to report
1 parent 3bdc925 commit ad25036

File tree

5 files changed

+67
-22
lines changed

5 files changed

+67
-22
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,13 +186,15 @@ Number of outcomes per category and the total number of test items.
186186

187187
| Key | Description |
188188
| --- | --- |
189+
| `collected` | Total number of tests collected. |
190+
| `total` | Total number of tests run. |
189191
| `<outcome>` | Number of tests with that outcome. (absent if number is 0) |
190-
| `total` | Total number of tests. |
191192

192193
#### Example
193194

194195
```python
195196
{
197+
"collected": 10,
196198
"passed": 2,
197199
"failed": 3,
198200
"xfailed": 1,
@@ -247,6 +249,7 @@ The `result` is a list of the collected nodes:
247249
| `nodeid` | ID of the node. |
248250
| `type` | Type of the collected node. |
249251
| `lineno` | Line number. (absent if not applicable) |
252+
| `deselected` | `true` if the test is deselected. (absent if not deselected) |
250253

251254
#### Example
252255

@@ -270,6 +273,7 @@ The `result` is a list of the collected nodes:
270273
"nodeid": "test_foo.py::test_pass",
271274
"type": "Function",
272275
"lineno": 24,
276+
"deselected": true
273277
},
274278
...
275279
]

pytest_jsonreport/plugin.py

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,29 @@ def pytest_sessionstart(self, session):
129129
self._start_time = time.time()
130130

131131
def pytest_collectreport(self, report):
132-
if not self._must_omit('collectors'):
133-
self._json_collectors.append(serialize.make_collector(report))
132+
if self._must_omit('collectors'):
133+
return
134+
json_result = []
135+
for item in report.result:
136+
json_item = serialize.make_collectitem(item)
137+
item._json_collectitem = json_item
138+
json_result.append(json_item)
139+
self._json_collectors.append(serialize.make_collector(report,
140+
json_result))
141+
142+
def pytest_deselected(self, items):
143+
if self._must_omit('collectors'):
144+
return
145+
for item in items:
146+
item._json_collectitem['deselected'] = True
147+
148+
@pytest.hookimpl(hookwrapper=True)
149+
def pytest_collection_modifyitems(self, items):
150+
yield
151+
if self._must_omit('collectors'):
152+
return
153+
for item in items:
154+
del item._json_collectitem
134155

135156
def pytest_runtest_logreport(self, report):
136157
nodeid = report.nodeid
@@ -183,7 +204,8 @@ def pytest_sessionfinish(self, session):
183204
exitcode=session.exitstatus,
184205
root=str(session.fspath),
185206
environment=getattr(self._config, '_metadata', {}),
186-
summary=serialize.make_summary(self._json_tests),
207+
summary=serialize.make_summary(self._json_tests,
208+
collected=session.testscollected),
187209
)
188210
if not self._config.option.json_report_summary:
189211
if self._json_collectors:
@@ -215,7 +237,7 @@ def save_report(self, path):
215237
os.makedirs(dirname)
216238
# Mimick FileExistsError for py2.7 compatibility
217239
except OSError as e:
218-
import errno
240+
import errno # pylint: disable=import-outside-toplevel
219241
if e.errno != errno.EEXIST:
220242
raise
221243
with open(path, 'w') as f:

pytest_jsonreport/serialize.py

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,8 @@
44
from collections import Counter
55

66

7-
def make_collector(report):
7+
def make_collector(report, result):
88
"""Return JSON-serializable collector node."""
9-
result = []
10-
for item in report.result:
11-
json_item = {
12-
'nodeid': item.nodeid,
13-
'type': item.__class__.__name__,
14-
}
15-
try:
16-
location = item.location
17-
except AttributeError:
18-
pass
19-
else:
20-
json_item['lineno'] = location[1]
21-
result.append(json_item)
22-
239
collector = {
2410
'nodeid': report.nodeid,
2511
# This is the outcome of the collection, not the test outcome
@@ -33,6 +19,21 @@ def make_collector(report):
3319
return collector
3420

3521

22+
def make_collectitem(item):
23+
"""Return JSON-serializable collection item."""
24+
json_item = {
25+
'nodeid': item.nodeid,
26+
'type': item.__class__.__name__,
27+
}
28+
try:
29+
location = item.location
30+
except AttributeError:
31+
pass
32+
else:
33+
json_item['lineno'] = location[1]
34+
return json_item
35+
36+
3637
def make_testitem(nodeid, keywords, location):
3738
"""Return JSON-serializable test item."""
3839
item = {
@@ -90,10 +91,11 @@ def make_traceback(traceback):
9091
} for entry in traceback.reprentries]
9192

9293

93-
def make_summary(tests):
94+
def make_summary(tests, **kwargs):
9495
"""Return JSON-serializable test result summary."""
9596
summary = Counter([t['outcome'] for t in tests.values()])
9697
summary['total'] = sum(summary.values())
98+
summary.update(kwargs)
9799
return summary
98100

99101

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
packages=['pytest_jsonreport'],
2222
author='numirias',
2323
author_email='numirias@users.noreply.github.com',
24-
version='1.1.0',
24+
version='1.2.0',
2525
url='https://github.com/numirias/pytest-json-report',
2626
license='MIT',
2727
install_requires=[

tests/test_jsonreport.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ def test_report_summary(make_json):
140140
'xpassed': 1,
141141
'xfailed': 1,
142142
'error': 2,
143+
'collected': 10,
143144
}
144145

145146

@@ -185,6 +186,21 @@ def test_report_crash_and_traceback(tests):
185186
assert call['traceback'] == traceback
186187

187188

189+
def test_report_item_deselected(make_json):
190+
data = make_json("""
191+
import pytest
192+
@pytest.mark.good
193+
def test_first():
194+
pass
195+
@pytest.mark.bad
196+
def test_second():
197+
pass
198+
""", ['--json-report', '-m', 'not bad'])
199+
assert data['summary']['collected'] == 1
200+
assert not data['collectors'][1]['result'][0].get('deselected')
201+
assert data['collectors'][1]['result'][1].get('deselected')
202+
203+
188204
def test_no_traceback(make_json):
189205
data = make_json(FILE, ['--json-report', '--json-report-omit=traceback'])
190206
tests_ = tests_only(data)
@@ -490,6 +506,7 @@ def test_flaky_fail():
490506
assert set(data['summary'].items()) == {
491507
('total', 2),
492508
('passed', 2),
509+
('collected', 2),
493510
}
494511

495512

0 commit comments

Comments
 (0)