Run a Property Test After a Claude Code Edit
In this article
Yes, you can run a property test right after a Claude Code edit to verify that your code still satisfies key invariants. This guide walks you through the exact steps, from saving your edit to running the test and interpreting the results. You'll need your project's test setup already in place, and you'll need to know which property-based testing library your project uses (e.g., Hypothesis for Python, QuickCheck for Haskell, or fast-check for JavaScript).
What You'll Need
- Your project's source code with the recent Claude Code edit saved (usually Claude Code saves automatically, but confirm the file is written to disk).
- The property-based testing library installed in your project's environment (e.g.,
hypothesisin Python,fast-checkin Node.js). - An existing property test file that covers the edited code, or the ability to write one quickly.
- Access to your terminal or command-line interface, and knowledge of the command to run your test suite (e.g.,
pytest,cargo test,npm test).
Step-by-Step
- Confirm the edit is saved. In Claude Code, after an edit, the file is usually written automatically. Check the file in your editor or run
git diffin your terminal to see the changes. If the edit is not saved, use Claude Code's save command or manually save the file. - Identify the property test file. Look for a test file that contains
@given(Hypothesis),property(fast-check), or similar decorators. If your project doesn't have one, create a new test file (e.g.,test_properties.py) and import the property testing library. - Write or update the property test. If the edit changed a function's logic, ensure the property test covers the relevant input space. For example, if you edited a sorting function, a property test might assert that the output is sorted and is a permutation of the input. If you're unsure, start with a simple property like "for all inputs, the function does not crash" and expand from there.
- Run the property test in isolation. Use your test runner's syntax to run only the property test file. For pytest, run
pytest test_properties.py -v. For npm, runnpx jest test_properties.test.js. This isolates the test and makes failures easier to diagnose. - Observe the output. Property tests generate many random inputs. If all pass, you'll see a success message. If a failure occurs, the test runner will print the minimal failing example (e.g., Hypothesis shows a counterexample). Note the exact input that caused the failure.
- If a failure occurs, fix the code or the test. The failure might be a real bug introduced by the edit, or it might be that the property is too strong. Examine the counterexample and decide: if the code is wrong, revert or adjust the edit; if the property is wrong, refine it to match the intended behavior.
- Re-run the test after any fix. After making a change, repeat steps 4–6 until the property test passes consistently. Run it a few times to ensure stability, since property tests are random.
- Run the full test suite. Once the property test passes, run the entire test suite to ensure no other tests broke due to your edit or the property test changes.
Common Mistakes
- Forgetting to save the Claude Code edit before running the test – the test runs against the old code.
- Running the property test with too few examples (default is usually fine, but if you're suspicious, increase the number of examples).
- Writing a property that is too weak (e.g., only checks that the function returns a value) and missing the actual invariant.
- Writing a property that is too strong (e.g., expecting exact output when the function is allowed to return any valid alternative).
- Ignoring the counterexample and just re-running – property tests are deterministic given a seed, but the failure will likely recur.
Tips
- Use the
@seeddecorator (Hypothesis) orseedoption (fast-check) to reproduce a specific failure. This is helpful when a counterexample is hard to understand. - Keep property tests separate from example-based tests – they have different failure modes and are easier to debug.
- When editing code, write the property test first (test-driven) so you know what the edit should satisfy.
- Run property tests with a higher number of examples (e.g.,
@given(st.integers())withmax_examples=1000) after a significant edit to increase confidence. - If the edit is in a pure function, property tests are ideal. If the edit involves I/O or state, consider using stateful property testing (e.g., Hypothesis's
RuleBasedStateMachine).
Troubleshooting
If the test doesn't run at all: Check that the testing library is installed. Run pip list | grep hypothesis (Python) or npm list fast-check (Node). If missing, install it via your package manager. Also ensure your test file is in the correct directory that your test runner scans.
If the test fails with a type error or import error: The edit may have changed the function's signature or return type. Update the property test to match the new signature, or check if the edit introduced a bug.
If the test fails with a counterexample but the code looks correct: The property might be too restrictive. For example, if you're testing a function that returns a list, don't assert that it's sorted if the function is allowed to return any order. Relax the property to match the specification.
If the test passes but you're not confident: Increase the number of examples. In Hypothesis, use @settings(max_examples=1000). In fast-check, pass { numRuns: 1000 } to the fc.assert call.
If the test hangs or takes too long: Your property might be generating extremely large inputs. Use strategies that limit size, like st.lists(st.integers(), max_size=100) or fc.array(fc.integer(), {maxLength: 100}).
If the test fails only intermittently: This is normal for property tests. The failure is real – the code has a bug that only triggers with certain inputs. Use the reported counterexample to debug, and consider adding it as a regular test to prevent regression.
Frequently Asked Questions
Do I need to run property tests after every Claude Code edit?
Not necessarily, but it's good practice after any edit that changes logic. For trivial edits like variable renames, a regular test suite is enough. For logic changes, property tests can catch edge cases you didn't think of.
How do I know which property testing library my project uses?
Check your project's dependencies (e.g., requirements.txt, package.json). Common ones are Hypothesis (Python), fast-check (JavaScript/TypeScript), and QuickCheck (Haskell/Rust). If you don't have one, you can add it.
Can I run property tests from within Claude Code?
Yes, you can use Claude Code's terminal command feature to run the test command directly. For example, type /terminal pytest test_properties.py in Claude Code. This is convenient after an edit.
What if the property test fails but the edit seems correct?
Double-check the property's assumptions. Maybe the function is allowed to return a different valid output. Also check if the edit changed behavior intentionally – if so, update the property test to reflect the new expected behavior.
How many examples should I run?
Defaults are usually fine (100 for Hypothesis, 100 for fast-check). For critical code, increase to 1000. More examples increase confidence but slow down the test.
Can I use property tests for code that involves randomness or time?
Yes, but you need to control the randomness. Use the library's seed parameter to make the test deterministic. For time-dependent code, mock the clock or use a strategy that generates timestamps within a range.