Package: pytest-instrument
Apytestplugin to instrument tests and write the resulting records to fileFeaturesa 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’snodeid)when (setup, call or teardown)outcome (passed, failed or skipped)start, stop and durationlabels (array) and tags (object) via@pytest.mark.instrument()decoratorfixtureshooks to edit labels, tags and fixtures before the record is written to filea logger to emit records with session id, record id, node id to the same log fileRequirementsPython 3.6 or higherpytest 5.1.0 or higherInstallationYou can install “pytest-instrument” viapipfromPyPI:$ pip install pytest-instrumentUsageRun your tests with:$ pytest --instrument=jsonAn./artifactsdirectory will be created if it doesn’t exist yet. For eachpytestsession one.jsonfile
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.logfile, usejq: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 fileIf you want to output a regular log file, use–instrument=log(or–instrument=json,logif 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.reportThe 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 thegetChild()method.
The node id is updated automatically via pytests’spytest_runtest_setup()hook.Labels and tagsYou can add labels and tags to tests with the plugin’smarkdecorator:@pytest.mark.instrument("a_label", my_tag="tagged")Allargs, such as"a_label", are put in the json arraylabels.
Allkwargs, such asmy_tag="tagged", are put in the json objecttags.Hookspytest_instrument_labels: edit list of labels after they’ve been parsed by the pluginpytest_instrument_tags: edit dictionary of tags after they’ve been parsed by the pluginpytest_instrument_fixtures: edit list of fixtures after they’ve been parsed by the pluginChangelogConsult thechangelogfor fixes and enhancements of each version.Issues/RequestsPlease use theGitHub issue trackerto submit bugs or request features.ContributingContributions are welcome. Tests can be run withtox, please ensureblackcode formatting and good test coverage before you submit a pull request.LicenseDistributed under the terms of theMITlicense, “pytest-instrument” is free and open source softwareAcknowledgementsThank you toMaaret Pyhäjärvifor inspiring this plugin by sharing the story of how her team
started instrumenting tests atTestCraftCamp2019.Thank you toTony S Yufor backporting thestacklevelargument from Python 3.8 logging.Thispytestplugin was generated withCookiecutteralong with@hackebrot’scookiecutter-pytest-plugintemplate.