pytest-md-report
pytest-md-report is a pytest plugin designed to generate test outcomes reports formatted as markdown tables. It extends pytest's functionality by providing various command-line options and a programmatic interface to produce detailed, customizable reports. The library is actively maintained, with frequent updates to support new Python and pytest versions, currently at version 0.7.0.
Common errors
-
No markdown report file generated or no output on console.
cause The `--md-report` option was not used during the pytest execution, or `--md-report-output` was not specified for file output, and `--md-report-tee` was not used for console output.fixRun pytest with `pytest --md-report` (for console output by default), or `pytest --md-report --md-report-output=report.md` (to save to a file), or `pytest --md-report --md-report-tee --md-report-output=report.md` (for both). -
Test parameters are not visible in the generated Markdown report.
cause The `--md-report-verbose` option is not set to level 2, or the library version is older than 0.7.0 which introduced this feature.fixUpgrade to `pytest-md-report>=0.7.0` and execute pytest with the `--md-report-verbose=2` option: `pytest --md-report --md-report-verbose=2`. -
Traceback: AttributeError: module 'pytest_md_report.converter' has no attribute 'make_md_report' (or similar)
cause Incorrect import path for `make_md_report`. It's located in `pytest_md_report.converter`, not directly under `pytest_md_report`.fixChange the import statement to `from pytest_md_report.converter import make_md_report`. -
Report on Windows with `--md-report-flavor=gfm` shows malformed paths or incorrect rendering.
cause This is a known bug in versions prior to 0.6.2 where backslashes in Windows paths were not correctly handled for GFM.fixUpgrade to `pytest-md-report>=0.6.2` to resolve the Windows path rendering issue with GFM.
Warnings
- breaking Support for Python 3.7 and 3.8 was dropped in version 0.6.3. Users on these Python versions must either upgrade Python or pin pytest-md-report to a version prior to 0.6.3.
- breaking The maximum supported `pytest` version was bumped to `<9` in version 0.5.1. Users with `pytest` version 9.0.0 or higher may experience compatibility issues or unexpected behavior.
- gotcha When using the `--md-report-flavor=gfm` option on Windows, backslashes in paths (`\`) could be misinterpreted by GitHub Flavored Markdown in versions prior to 0.6.2, leading to incorrectly rendered reports.
- deprecated The `make_md_report` programmatic method no longer requires the `exclude_outcomes` parameter when called, starting from version 0.6.1. While still accepted, passing this argument might be considered redundant in future versions.
- gotcha To display test parameters in the report, the verbosity level must be set to `2` using `--md-report-verbose=2`. Previous versions (before 0.7.0) did not support this granular level of detail for parameters.
Install
-
pip install pytest-md-report
Imports
- pytest-md-report command-line plugin
pytest --md-report --md-report-output=report.md
- make_md_report
from pytest_md_report import make_md_report
from pytest_md_report.converter import make_md_report
Quickstart
import pytest
import os
def test_example_success():
assert True
def test_example_failure():
assert False
def test_example_skipped():
pytest.skip("demonstrating skip")
# Create a dummy test file
with open("test_example.py", "w") as f:
f.write("""
import pytest
def test_pass():
assert 1 + 1 == 2
def test_fail():
assert 1 + 1 == 3
@pytest.mark.skip(reason='demonstrating skip')
def test_skip():
assert True
"""
)
# Run pytest with md-report options
# This will generate 'report.md' in the current directory
# and also output to stdout due to --md-report-tee
# Using os.system for demonstration; in a real script, prefer pytest.main()
# For security, avoid os.system with untrusted input.
print("\n--- Running tests and generating report ---")
os.system("pytest --md-report --md-report-output=report.md --md-report-tee test_example.py")
print("\n--- Report generated: report.md ---")
print(open("report.md").read())
# Clean up
os.remove("test_example.py")
os.remove("report.md")