Expecttest is a Python library that implements 'expect tests' (also known as 'golden tests' or 'snapshot tests'). Unlike traditional asserts, it automatically populates the expected output of a test. When the test output changes, the library facilitates updating the expected output directly within the Python source file by modifying it in-place. The current version is 0.3.0, released in December 2024, indicating active development with a fairly rapid release cadence.
pip install expecttestVerified import paths — ran on the pinned version, not inferred.
To use `expecttest`, define your test using `TestCase` (for `unittest`) or directly call `assert_expected_inline` (for Pytest or standalone). Initially, provide an empty triple-quoted string as the expected value. Run your tests normally; they will fail if the expected string is empty or differs. To automatically populate or update the expected string in your source file, run your tests with the `EXPECTTEST_ACCEPT=1` environment variable set. This modifies your Python file directly.
Be aware of when and how `EXPECTTEST_ACCEPT=1` is used. Always review the generated diffs before committing any changes made by `expecttest` to ensure they reflect intended behavior changes, not unintended regressions.
Always run `EXPECTTEST_ACCEPT=1` only when you intend to accept new output as the correct expectation. Treat the resulting diffs in your source code as a change to be reviewed, similar to any other code modification.
When creating functions to be tested with `expecttest`, consider implementing custom pretty-printers or helper functions to produce concise, stable, and readable output that focuses only on the essential aspects being tested.
Treat the auto-generated expected output as part of the test specification. Always review the diffs created by `EXPECTTEST_ACCEPT=1` to ensure the new expected behavior is indeed correct and intentional, not merely accepting unintended changes.
Install the expecttest library using pip: `pip install expecttest`
Instead of accessing it as `expecttest.ACCEPT`, set the environment variable before running your tests: `EXPECTTEST_ACCEPT=1 pytest` (for pytest) or `EXPECTTEST_ACCEPT=1 python your_test_file.py`.
If the change is intentional and the new output is correct, re-run your tests with the `EXPECTTEST_ACCEPT=1` environment variable to update the expected values in your source files: `EXPECTTEST_ACCEPT=1 pytest` or `EXPECTTEST_ACCEPT=1 python your_test_file.py`. Always review the resulting diffs.
Simplify the content of the expected string or the surrounding code if possible. Ensure the test file is writable. If the problem persists, check for updates to the expecttest library as it might be a bug in older versions.