pytest-excel
pytest-excel is a plugin for pytest that enables the generation of detailed Excel reports from test results. Currently at version 1.8.1, the library maintains an active development status with regular updates, including recent support for Python 3.11+.
Common errors
-
pytest command line usage error. Exit code 5. No tests were collected.
cause pytest could not find any test files or functions, often due to incorrect naming conventions (e.g., test files not starting with `test_` or ending with `_test.py`, or test functions not starting with `test_`).fixEnsure your test files and functions follow pytest's naming conventions. For example, `test_my_module.py` containing `def test_feature():`. -
No Excel report file created or report is empty.
cause The `--excelreport` option was not correctly used, or no tests were successfully executed/collected by pytest to populate the report. This can also happen if there are permissions issues in the output directory.fixVerify that you are passing the `--excelreport=your_report_name.xls` argument when running pytest. Ensure tests are collected and passing, and check file write permissions in the target directory. Use `-v` for verbose output to confirm test collection and execution. -
INTERNALERROR> AttributeError: 'ExcelTestItem' object has no attribute 'funcargs'
cause This error can occur in advanced scenarios, particularly when dynamically collecting tests (e.g., from Excel files using custom collection hooks) and attempting to access `funcargs` on an `ExcelTestItem` within `pytest_runtest_makereport` hook, suggesting an incompatibility in the item's attributes or the way it's being handled by the `pytest-excel` plugin.fixThis is often a conflict with custom `pytest` collection or reporting hooks. Review your `conftest.py` for custom `pytest_collect_file` or `pytest_runtest_makereport` implementations that might not be fully compatible with `pytest-excel`'s internal item structure. Simplification of custom hooks or adapting them to `pytest-excel`'s item types might be necessary. (Specific to highly customized setups).
Warnings
- breaking Version 1.8.0 and above explicitly require Python 3.11 or higher. Older Python versions are no longer supported.
- gotcha The filename for the Excel report can be specified via the command line or a pytest configuration file. The command-line option (`--excelreport`) will override any filename set in the configuration.
- breaking Marker handling and deprecated hook annotations have been refactored for compatibility with newer pytest versions. This may affect custom plugins or advanced usages relying on specific hook or marker behaviors.
Install
-
pip install pytest-excel openpyxl
Quickstart
# 1. Create a simple test file (e.g., test_example.py) # def test_addition(): # assert 1 + 1 == 2 # 2. Run pytest with the --excelreport option # The report will be generated as 'report.xls' or specified name. # Use %Y-%M-dT%H% for a timestamped filename. # To run all tests and generate a report: # pytest --excelreport=report.xls # To get a verbose output and generate a report: # pytest -v --excelreport=report.xls # To collect tests without execution and generate a report showing docstrings: # pytest --excelreport=report.xls --collect-only # Example with timestamped filename: pytest --excelreport=report_%Y-%m-%dT%H%M.xls