Skip to main content

pytest-instrument

PyPI version Python versions PyPI - Downloads See Build Status on Circle CI https://img.shields.io/badge/code%20style-black-000000.svg MIT license

A pytest plugin to instrument tests and write the resulting records to file


Features

  • a log file with one record for each setup/call/teardown of each test containing:

    • session id and record id (UUIDs generated by plugin)

    • node_id (pytest’s nodeid)

    • when (setup, call or teardown)

    • outcome (passed, failed or skipped)

    • start, stop and duration

    • labels (array) and tags (object) via @pytest.mark.instrument() decorator

    • fixtures

  • hooks to edit labels, tags and fixtures before the record is written to file

  • a logger to emit records with session id, record id, node id to the same log file

Requirements

  • Python 3.6 or higher

  • pytest 5.1.0 or higher

Installation

You can install “pytest-instrument” via pip from PyPI:

$ pip install pytest-instrument

Usage

Run your tests with:

$ pytest --instrument=json

An ./artifacts directory will be created if it doesn’t exist yet. For each pytest session one .json file will be written to that directory. The format of the filename is %Y%m%dT%H%M%S_<first group of session id>.json.

To display the contents of the .log file, use jq: jq . <filename>. Or, if for instance you only want to see the message object of each record: jq '{message: .message}' <filename>

Output to regular log file

If you want to output a regular log file, use –instrument=log (or –instrument=json,log if you want both). Note that the regular log file only contains the time, log node name, log level, test node id and log message of each log record.

Loggers - instr.log and instr.report

The logger reporting on the outcome of pytest’s setup, call and teardown is named “instr.report”.

The logger emitting records to the same file as the “instr.report” logger is named “instr.log”. You can use this logger like this:

logger = logging.getLogger("instr.log")
logger.info("log record of level info")

You can also create children of that logger, which will emit records to that same file:

sublogger = logging.getLogger("instr.log").getChild("sublogger")
logger.warning("log record of level warning")

The session id and node id of the “instr.log” logger and its children are set automatically in the getChild() method. The node id is updated automatically via pytests’s pytest_runtest_setup() hook.

Labels and tags

You can add labels and tags to tests with the plugin’s mark decorator:

@pytest.mark.instrument("a_label", my_tag="tagged")

All args, such as "a_label", are put in the json array labels. All kwargs, such as my_tag="tagged", are put in the json object tags.

Hooks

  • pytest_instrument_labels: edit list of labels after they’ve been parsed by the plugin

  • pytest_instrument_tags: edit dictionary of tags after they’ve been parsed by the plugin

  • pytest_instrument_fixtures: edit list of fixtures after they’ve been parsed by the plugin

Changelog

Consult the changelog for fixes and enhancements of each version.

Issues/Requests

Please use the GitHub issue tracker to submit bugs or request features.

Contributing

Contributions are welcome. Tests can be run with tox, please ensure black code formatting and good test coverage before you submit a pull request.

License

Distributed under the terms of the MIT license, “pytest-instrument” is free and open source software

Acknowledgements

Thank you to Maaret Pyhäjärvi for inspiring this plugin by sharing the story of how her team started instrumenting tests at TestCraftCamp 2019.

Thank you to Tony S Yu for backporting the stacklevel argument from Python 3.8 logging.

This pytest plugin was generated with Cookiecutter along with @hackebrot’s cookiecutter-pytest-plugin template.

Release files for pytest-instrument 0.3.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pytest-instrument 0.3.1
File Size Uploaded
pytest-instrument-0.3.1.tar.gz 7.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pytest-instrument 0.3.1
File Interpreter ABI Platform
pytest_instrument-0.3.1-py3-none-any.whl Python 3 none any Details

Total release size: 17.3 kB

Release files / pytest-instrument-0.3.1.tar.gz

Download URL pytest-instrument-0.3.1.tar.gz
Size 7.1 kB
Tags Source
SHA-256 checksum
How to use checksums
d155aea3642528819808f419b08e654975ce9ef657781fc760521950b367249d
BLAKE2b-256 checksum
How to use checksums
c806f54516f394ff6dd3d18e8e29935c702d564408a90b387dc5e02c5c0e435c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.10.0 pkginfo/1.2.1 requests/2.18.4 setuptools/41.0.1 requests-toolbelt/0.8.0 tqdm/4.19.5 CPython/3.6.9

Release files / pytest_instrument-0.3.1-py3-none-any.whl

Download URL pytest_instrument-0.3.1-py3-none-any.whl
Size 10.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
03c479cb30ece637a149f9f81601e6f8a1de453044596e9d847dcb85c7e912f4
BLAKE2b-256 checksum
How to use checksums
25efcc659a031ec6806d2db39f051b2393baeb2e0dfd3eb9f90708b66b5c9f92
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.10.0 pkginfo/1.2.1 requests/2.18.4 setuptools/41.0.1 requests-toolbelt/0.8.0 tqdm/4.19.5 CPython/3.6.9

Release history Release notifications | RSS feed

This release

0.3.1 This release

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page