pytest-csv
pytest-csv is a pytest plugin that generates test reports in CSV format. It allows users to customize the output columns and delimiters for detailed analysis of test results. The current version is 3.0.0, released in April 2021, and it maintains an infrequent release cadence.
Warnings
- breaking pytest-csv 3.0.0 was released when pytest 6.x was current. Subsequent major versions of pytest (7.x, 8.x, 9.x) have introduced breaking changes to internal plugin APIs, particularly around collection and reporting hooks. pytest-csv may not be fully compatible with newer pytest versions without updates.
- gotcha This plugin generates CSV *reports*. It is often confused with `pytest-csv-params`, a different plugin used for *parametrizing* tests from data in CSV files. Ensure you are using the correct plugin for your desired functionality.
- gotcha When customizing CSV columns using `--csv-columns`, providing unrecognized or improperly formatted column names can lead to `KeyError` exceptions or incomplete reports. Special characters or non-standard column definitions might not be handled gracefully.
- gotcha Defining custom columns with `pytest_csv_register_columns` must be done in your `conftest.py` file. Attempting to define or import custom column logic directly within test files will not be recognized by the plugin's hook system.
Install
-
pip install pytest-csv
Imports
- pytest-csv plugin functionality
Functionality is typically invoked via command-line options (e.g., `pytest --csv output.csv`) or by defining pytest hooks in `conftest.py` (e.g., `pytest_csv_register_columns`).
Quickstart
import pytest
import os
def test_example_passed():
assert True
def test_example_failed():
assert False
def test_example_skipped():
pytest.skip("Demonstrating skip")
# To run and generate a CSV report, save this as `test_report.py`
# then run from your terminal: `pytest --csv report.csv test_report.py`
# Optionally, customize columns: `pytest --csv report.csv --csv-columns id,status,duration`