Skip to main content

Replacement for the --resultlog option, focused in simplicity and extensibility

Project description

python version anaconda ci black

Replacement for the --resultlog option, focused in simplicity and extensibility.

Usage

Install pytest-reportlog as a test requirement in your test environment.

The --report-log=FILE option writes a file in JSON lines format as the test session executes.

Each line of the report log contains a self contained JSON object corresponding to a testing event, such as a collection or a test result report. The file is guaranteed to be flushed after writing each line, so systems can read and process events in real-time.

Each JSON object contains a special key $report_type, which contains a unique identifier for that kind of report object. For future compatibility, consumers of the file should ignore reports they don’t recognize, as well as ignore unknown properties/keys in JSON objects that they do know, as future pytest versions might enrich the objects with more properties/keys.

Compression

Common compression suffixes like .gz, .bz2 and .xz will automatically use the requested compression format. The implementations from the python stdlib are used and must be enabled in the python builds.

Example

Consider this file:

# content of test_report_example.py


def test_ok():
    assert 5 + 5 == 10


def test_fail():
    assert 4 + 4 == 1
$ pytest test_report_example.py -q --report-log=log.jsonl
.F                                                                   [100%]
================================= FAILURES =================================
________________________________ test_fail _________________________________

    def test_fail():
>       assert 4 + 4 == 1
E       assert (4 + 4) == 1

test_report_example.py:8: AssertionError
------------------- generated report log file: log.jsonl --------------------
1 failed, 1 passed in 0.12s

The generated log.jsonl will contain a JSON object per line:

$ cat log.jsonl
{"pytest_version": "5.2.2", "$report_type": "SessionStart"}
{"nodeid": "", "outcome": "passed", "longrepr": null, "result": null, "sections": [], "$report_type": "CollectReport"}
{"nodeid": "test_report_example.py", "outcome": "passed", "longrepr": null, "result": null, "sections": [], "$report_type": "CollectReport"}
{"nodeid": "test_report_example.py::test_ok", "location": ["test_report_example.py", 0, "test_ok"], "keywords": {"test_ok": 1, "pytest-reportlog": 1, "test_report_example.py": 1}, "outcome": "passed", "longrepr": null, "when": "setup", "user_properties": [], "sections": [], "duration": 0.0, "$report_type": "TestReport"}
{"nodeid": "test_report_example.py::test_ok", "location": ["test_report_example.py", 0, "test_ok"], "keywords": {"test_ok": 1, "pytest-reportlog": 1, "test_report_example.py": 1}, "outcome": "passed", "longrepr": null, "when": "call", "user_properties": [], "sections": [], "duration": 0.0, "$report_type": "TestReport"}
{"nodeid": "test_report_example.py::test_ok", "location": ["test_report_example.py", 0, "test_ok"], "keywords": {"test_ok": 1, "pytest-reportlog": 1, "test_report_example.py": 1}, "outcome": "passed", "longrepr": null, "when": "teardown", "user_properties": [], "sections": [], "duration": 0.00099945068359375, "$report_type": "TestReport"}
{"nodeid": "test_report_example.py::test_fail", "location": ["test_report_example.py", 4, "test_fail"], "keywords": {"test_fail": 1, "pytest-reportlog": 1, "test_report_example.py": 1}, "outcome": "passed", "longrepr": null, "when": "setup", "user_properties": [], "sections": [], "duration": 0.0, "$report_type": "TestReport"}
{"nodeid": "test_report_example.py::test_fail", "location": ["test_report_example.py", 4, "test_fail"], "keywords": {"test_fail": 1, "pytest-reportlog": 1, "test_report_example.py": 1}, "outcome": "failed", "longrepr": {"reprcrash": {"path": "D:\\projects\\pytest-reportlog\\test_report_example.py", "lineno": 6, "message": "assert (4 + 4) == 1"}, "reprtraceback": {"reprentries": [{"type": "ReprEntry", "data": {"lines": ["    def test_fail():", ">       assert 4 + 4 == 1", "E       assert (4 + 4) == 1"], "reprfuncargs": {"args": []}, "reprlocals": null, "reprfileloc": {"path": "test_report_example.py", "lineno": 6, "message": "AssertionError"}, "style": "long"}}], "extraline": null, "style": "long"}, "sections": [], "chain": [[{"reprentries": [{"type": "ReprEntry", "data": {"lines": ["    def test_fail():", ">       assert 4 + 4 == 1", "E       assert (4 + 4) == 1"], "reprfuncargs": {"args": []}, "reprlocals": null, "reprfileloc": {"path": "test_report_example.py", "lineno": 6, "message": "AssertionError"}, "style": "long"}}], "extraline": null, "style": "long"}, {"path": "D:\\projects\\pytest-reportlog\\test_report_example.py", "lineno": 6, "message": "assert (4 + 4) == 1"}, null]]}, "when": "call", "user_properties": [], "sections": [], "duration": 0.0009992122650146484, "$report_type": "TestReport"}
{"nodeid": "test_report_example.py::test_fail", "location": ["test_report_example.py", 4, "test_fail"], "keywords": {"test_fail": 1, "pytest-reportlog": 1, "test_report_example.py": 1}, "outcome": "passed", "longrepr": null, "when": "teardown", "user_properties": [], "sections": [], "duration": 0.0, "$report_type": "TestReport"}
{"exitstatus": 1, "$report_type": "SessionFinish"}

record_property

The record_property fixture allows to log additional information for a test, just like with JUnitXML format. Consider this test file:

def test_function(record_property):
    record_property("price", 12.34)
    record_property("fruit", "banana")
    assert True

This information will be recorded in the report JSON objects under the user_properties key as follows:

..., "user_properties": [["price", 12.34], ["fruit", "banana"]], ...

Note that this nested list construct is just the JSON representation of a list of tuples (name-value pairs).

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pytest_reportlog-1.0.0.tar.gz (5.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pytest_reportlog-1.0.0-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

Details for the file pytest_reportlog-1.0.0.tar.gz.

File metadata

  • Download URL: pytest_reportlog-1.0.0.tar.gz
  • Upload date:
  • Size: 5.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pytest_reportlog-1.0.0.tar.gz
Algorithm Hash digest
SHA256 75aec3a92bb53456c3e028605a636579d26f31c6f1e035ad9f706c203cfcb74e
MD5 72590156472020dc0131165f22851e83
BLAKE2b-256 93d52f4a73822efaad1d6aea6660468d6290963e5f61d2f9d9ca707f7f0b1c13

See more details on using hashes here.

Provenance

The following attestation bundles were made for pytest_reportlog-1.0.0.tar.gz:

Publisher: deploy.yml on pytest-dev/pytest-reportlog

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pytest_reportlog-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for pytest_reportlog-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3fc837ef3be6e50f33b52aaf99f88bfbb2ec525febbc3990389d24bbfba28753
MD5 80e43f10ff2a3362fb919083c47e6fd8
BLAKE2b-256 0570807cbdac584629623c1c2f76b7a39a343e3989510cd52385f1f40581e963

See more details on using hashes here.

Provenance

The following attestation bundles were made for pytest_reportlog-1.0.0-py3-none-any.whl:

Publisher: deploy.yml on pytest-dev/pytest-reportlog

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page