Pytest Timestamper
pytest-timestamper is a pytest plugin that enhances test output by adding a timestamp prefix to each line of the test session output. This helps users track the duration of individual tests and overall test suite progress. The current version is 0.0.10, with releases occurring periodically to maintain compatibility with `pytest` and address minor improvements.
Common errors
-
Pytest output does not show any timestamps.
cause The `pytest-timestamper` plugin is not installed or not active in the pytest environment.fixEnsure `pytest-timestamper` is installed in the same Python environment where `pytest` is being run (`pip install pytest-timestamper`). No further activation steps are required as it's an auto-activating plugin. -
Command line options --prefixfmt or --datefmt are not recognized.
cause The plugin is not installed, or pytest is running an older version of the plugin that doesn't support these specific command-line arguments, or the options are misspelled.fixVerify the plugin is installed and up-to-date (`pip install --upgrade pytest-timestamper`). Double-check the spelling of the command-line arguments: `--prefixfmt` and `--datefmt`.
Warnings
- breaking Version 0.0.10 removed deprecated decorators. Older versions of `pytest-timestamper` might rely on internal `pytest` APIs that have since been deprecated or removed in newer `pytest` releases, potentially leading to breakage or unexpected behavior if not updated.
- gotcha Timestamps added by `pytest-timestamper` apply to the overall test output, not necessarily to captured log messages (e.g., from `logging`). `pytest`'s default log capture mechanism often re-formats logs, which can strip custom timestamps from `logging` calls.
- gotcha There is another plugin named `pytest-timestamps` which provides similar functionality but with different implementation and options. Users might confuse the two or accidentally install both.
Install
-
pip install pytest-timestamper
Imports
- pytest-timestamper
No direct import is typically needed; the plugin activates automatically upon installation.
Quickstart
import pytest
def test_example_one():
print("This is the first example test.")
assert True
def test_example_two():
print("This is the second example test.")
assert 1 + 1 == 2
# To run this, save as 'test_timestamps.py' and execute in terminal:
# pytest -v --prefixfmt="[%H:%M:%S]" --datefmt="%Y-%m-%d"