Skip to content

Commit c46b08c

Browse files
authored
Merge pull request #6 from nicoddemus/xdist-fix
Fix integration with xdist
2 parents 54a6e8e + 63907f9 commit c46b08c

File tree

6 files changed

+69
-4
lines changed

6 files changed

+69
-4
lines changed

.github/workflows/main.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ jobs:
3131
- name: Install tox
3232
run: |
3333
python -m pip install --upgrade pip
34-
pip install tox
34+
pip install --upgrade virtualenv tox
3535
- name: Test
3636
run: |
3737
tox -e ${{ matrix.tox_env }}

.pre-commit-config.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ repos:
55
hooks:
66
- id: black
77
args: [--safe, --quiet]
8-
language_version: python3.7
98
- repo: https://github.com/pre-commit/pre-commit-hooks
109
rev: v2.2.3
1110
hooks:

CHANGELOG.rst

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
0.1.0 (unreleased)
1+
0.1.1 (2020-04-16)
2+
------------------
3+
4+
* `#5 <https://github.com/pytest-dev/pytest-reportlog/issues/5>`_: Fix support for `pytest-xdist <https://github.com/pytest-dev/pytest-xdist>`_.
5+
6+
0.1.0 (2019-11-18)
27
------------------
38

49
* First version.

src/pytest_reportlog/plugin.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import json
2+
from typing import Dict, Any
3+
24
from _pytest.pathlib import Path
35

46
import pytest
@@ -43,7 +45,12 @@ def close(self):
4345
self._file = None
4446

4547
def _write_json_data(self, data):
46-
self._file.write(json.dumps(data) + "\n")
48+
try:
49+
json_data = json.dumps(data)
50+
except TypeError:
51+
data = cleanup_unserializable(data)
52+
json_data = json.dumps(data)
53+
self._file.write(json_data + "\n")
4754
self._file.flush()
4855

4956
def pytest_sessionstart(self):
@@ -70,3 +77,15 @@ def pytest_terminal_summary(self, terminalreporter):
7077
terminalreporter.write_sep(
7178
"-", "generated report log file: {}".format(self._log_path)
7279
)
80+
81+
82+
def cleanup_unserializable(d: Dict[str, Any]) -> Dict[str, Any]:
83+
"""Return new dict with entries that are not json serializable by their str()."""
84+
result = {}
85+
for k, v in d.items():
86+
try:
87+
json.dumps({k: v})
88+
except TypeError:
89+
v = str(v)
90+
result[k] = v
91+
return result

tests/test_reportlog.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import pytest
44
from _pytest.reports import BaseReport
55

6+
from pytest_reportlog.plugin import cleanup_unserializable
7+
68

79
def test_basics(testdir, tmp_path, pytestconfig):
810
"""Basic testing of the report log functionality.
@@ -52,3 +54,41 @@ def test_fail():
5254
config=pytestconfig, data=json_obj
5355
)
5456
assert isinstance(rep, BaseReport)
57+
58+
59+
def test_xdist_integration(testdir, tmp_path):
60+
pytest.importorskip("xdist")
61+
testdir.makepyfile(
62+
"""
63+
def test_ok():
64+
pass
65+
66+
def test_fail():
67+
assert 0
68+
"""
69+
)
70+
fn = tmp_path / "result.log"
71+
result = testdir.runpytest("-n2", "--report-log={}".format(fn))
72+
result.stdout.fnmatch_lines("*1 failed, 1 passed*")
73+
74+
lines = fn.read_text("UTF-8").splitlines()
75+
data = json.loads(lines[0])
76+
assert data == {
77+
"pytest_version": pytest.__version__,
78+
"$report_type": "SessionStart",
79+
}
80+
81+
82+
def test_cleanup_unserializable():
83+
"""Unittest for the cleanup_unserializable function"""
84+
good = {"x": 1, "y": ["a", "b"]}
85+
new = cleanup_unserializable(good)
86+
assert new == good
87+
88+
class C:
89+
def __str__(self):
90+
return "C instance"
91+
92+
bad = {"x": 1, "y": ["a", "b"], "c": C()}
93+
new = cleanup_unserializable(bad)
94+
assert new == {"x": 1, "c": "C instance", "y": ["a", "b"]}

tox.ini

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
envlist = py{35,36,37,38}, linting
33

44
[testenv]
5+
deps =
6+
pytest-xdist
57
commands =
68
pytest tests
79

0 commit comments

Comments
 (0)