pytest-clarity
pytest-clarity is a pytest plugin that provides an alternative, colourful diff output for failing assertions. Inspired by the Ward test framework, it aims to significantly improve the readability of assertion errors by showing clear, unified, coloured diffs. The current version is 1.0.1, last released in June 2021. It is a stable plugin with infrequent updates as its core functionality is well-defined and stable.
Warnings
- gotcha pytest-clarity's enhanced diff output is only activated when running pytest with the `-vv` (very verbose) flag. Without this flag, you will see the default pytest assertion output.
- gotcha The plugin may not provide detailed diffs for objects compared using `unittest.mock` (e.g., `mock.MagicMock`). When comparing mocked objects, the output might revert to a less granular default.
- gotcha While PyPI metadata lists broader Python compatibility (`>=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*`), the GitHub README explicitly states 'Supports Python 3.6+'. Given the last release date (June 2021) and the end-of-life for older Python versions, practical and tested support is primarily for Python 3.6 and newer. Using it with Python 2.x or very old Python 3.x versions is not recommended and unsupported.
Install
-
pip install pytest-clarity
Imports
- pytest-clarity
No direct import needed. pytest-clarity is a pytest plugin and is automatically discovered and loaded by pytest upon installation.
Quickstart
# Save this to a file, e.g., test_example.py
def test_dict_failure():
expected = {'a': 1, 'b': 2, 'c': 3}
actual = {'a': 1, 'b': 99, 'd': 4}
assert expected == actual
# Run pytest with verbose output to enable pytest-clarity
# In your terminal:
# pytest -vv