Skip to content

Commit

Permalink
Merge pull request #6 from nicoddemus/xdist-fix
Browse files Browse the repository at this point in the history
Fix integration with xdist
  • Loading branch information
nicoddemus authored Apr 16, 2020
2 parents 54a6e8e + 63907f9 commit c46b08c
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:
- name: Install tox
run: |
python -m pip install --upgrade pip
pip install tox
pip install --upgrade virtualenv tox
- name: Test
run: |
tox -e ${{ matrix.tox_env }}
Expand Down
1 change: 0 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ repos:
hooks:
- id: black
args: [--safe, --quiet]
language_version: python3.7
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v2.2.3
hooks:
Expand Down
7 changes: 6 additions & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
0.1.0 (unreleased)
0.1.1 (2020-04-16)
------------------

* `#5 <https://github.com/pytest-dev/pytest-reportlog/issues/5>`_: Fix support for `pytest-xdist <https://github.com/pytest-dev/pytest-xdist>`_.

0.1.0 (2019-11-18)
------------------

* First version.
21 changes: 20 additions & 1 deletion src/pytest_reportlog/plugin.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import json
from typing import Dict, Any

from _pytest.pathlib import Path

import pytest
Expand Down Expand Up @@ -43,7 +45,12 @@ def close(self):
self._file = None

def _write_json_data(self, data):
self._file.write(json.dumps(data) + "\n")
try:
json_data = json.dumps(data)
except TypeError:
data = cleanup_unserializable(data)
json_data = json.dumps(data)
self._file.write(json_data + "\n")
self._file.flush()

def pytest_sessionstart(self):
Expand All @@ -70,3 +77,15 @@ def pytest_terminal_summary(self, terminalreporter):
terminalreporter.write_sep(
"-", "generated report log file: {}".format(self._log_path)
)


def cleanup_unserializable(d: Dict[str, Any]) -> Dict[str, Any]:
"""Return new dict with entries that are not json serializable by their str()."""
result = {}
for k, v in d.items():
try:
json.dumps({k: v})
except TypeError:
v = str(v)
result[k] = v
return result
40 changes: 40 additions & 0 deletions tests/test_reportlog.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import pytest
from _pytest.reports import BaseReport

from pytest_reportlog.plugin import cleanup_unserializable


def test_basics(testdir, tmp_path, pytestconfig):
"""Basic testing of the report log functionality.
Expand Down Expand Up @@ -52,3 +54,41 @@ def test_fail():
config=pytestconfig, data=json_obj
)
assert isinstance(rep, BaseReport)


def test_xdist_integration(testdir, tmp_path):
pytest.importorskip("xdist")
testdir.makepyfile(
"""
def test_ok():
pass
def test_fail():
assert 0
"""
)
fn = tmp_path / "result.log"
result = testdir.runpytest("-n2", "--report-log={}".format(fn))
result.stdout.fnmatch_lines("*1 failed, 1 passed*")

lines = fn.read_text("UTF-8").splitlines()
data = json.loads(lines[0])
assert data == {
"pytest_version": pytest.__version__,
"$report_type": "SessionStart",
}


def test_cleanup_unserializable():
"""Unittest for the cleanup_unserializable function"""
good = {"x": 1, "y": ["a", "b"]}
new = cleanup_unserializable(good)
assert new == good

class C:
def __str__(self):
return "C instance"

bad = {"x": 1, "y": ["a", "b"], "c": C()}
new = cleanup_unserializable(bad)
assert new == {"x": 1, "c": "C instance", "y": ["a", "b"]}
2 changes: 2 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
envlist = py{35,36,37,38}, linting

[testenv]
deps =
pytest-xdist
commands =
pytest tests

Expand Down

0 comments on commit c46b08c

Please sign in to comment.