{"id":3995,"library":"expecttest","title":"Expecttest","description":"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.","status":"active","version":"0.3.0","language":"en","source_language":"en","source_url":"https://github.com/ezyang/expecttest","tags":["testing","expect tests","golden tests","snapshot testing","unittest","pytest"],"install":[{"cmd":"pip install expecttest","lang":"bash","label":"Install latest version"}],"dependencies":[{"reason":"Required runtime environment.","package":"python","optional":false}],"imports":[{"note":"For integration with `unittest.TestCase`.","symbol":"TestCase","correct":"from expecttest import TestCase"},{"note":"For direct use in non-unittest test functions (e.g., Pytest).","symbol":"assert_expected_inline","correct":"from expecttest import assert_expected_inline"}],"quickstart":{"code":"import unittest\nimport os\nfrom expecttest import TestCase, assert_expected_inline\n\n# Example for unittest integration\nclass TestMyFunction(TestCase):\n    def test_basic_output(self):\n        result = f\"Hello, {1 + 2}!\\nMultiline output.\" # Simulate some function output\n        # On first run, leave expected blank. Run with EXPECTTEST_ACCEPT=1 to populate.\n        # self.assertExpectedInline(result, \"\"\")\"\"\"\n        self.assertExpectedInline(result, \"\"\"Hello, 3!\\nMultiline output.\"\"\")\n\n# Example for pytest or general use\ndef test_another_function():\n    data = {'key': 'value', 'number': 123}\n    result = str(data)\n    # On first run, leave expected blank. Run with EXPECTTEST_ACCEPT=1 to populate.\n    # assert_expected_inline(result, \"\"\")\"\"\"\n    assert_expected_inline(result, \"\"\"{'key': 'value', 'number': 123}\"\"\")\n\nif __name__ == '__main__':\n    # To update the expected values in the source file, run:\n    # EXPECTTEST_ACCEPT=1 python your_test_file.py\n    # or EXPECTTEST_ACCEPT=1 pytest your_test_file.py\n    if os.environ.get('EXPECTTEST_ACCEPT') == '1':\n        print(\"Running in ACCEPT mode. Expected values will be updated.\")\n    unittest.main()\n","lang":"python","description":"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."},"warnings":[{"fix":"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.","message":"The primary mechanism of `expecttest` involves directly modifying your source code files when `EXPECTTEST_ACCEPT=1` is set. This is a deliberate feature, but it means test runs can alter committed files, requiring careful use and potentially source control review of changes.","severity":"breaking","affected_versions":"All versions"},{"fix":"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.","message":"Tests using `expecttest` will fail if the actual output differs from the stored expected value, even if the change is valid (e.g., due to a new feature). You must explicitly run tests with `EXPECTTEST_ACCEPT=1` to update the expected values in the source code.","severity":"gotcha","affected_versions":"All versions"},{"fix":"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.","message":"The effectiveness of `expecttest` heavily relies on designing the output format of your test results. Poorly structured or verbose output can lead to 'spurious changes' (e.g., minor whitespace variations, irrelevant details) that frequently cause tests to fail and require re-acceptance, diminishing their value.","severity":"gotcha","affected_versions":"All versions"},{"fix":"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.","message":"Over-reliance on `EXPECTTEST_ACCEPT=1` without thorough review of the updated expected values can lead to 'locking in' an implementation rather than verifying desired behavior, potentially masking bugs.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-11T00:00:00.000Z","next_check":"2026-07-10T00:00:00.000Z"}