CtrlK
BlogDocsLog inGet started
Tessl Logo

testland/hypothesis-testing

Authors property-based tests in Python using Hypothesis - wires `@given` with `strategies` (`st.integers`, `st.text`, `st.lists`, `st.from_regex`, `st.composite`), uses `assume()` / `.filter()` for preconditions, configures via `@settings(max_examples=..., deadline=...)`, and exploits Hypothesis's automatic shrinking to find the falsifying example. Integrates with pytest fixtures + parametrize. Use when a Python project needs PBT to catch edge cases the example-based tests miss - bug clusters around input ranges / boundary values / interaction between fields.

80

Quality

100%

Does it follow best practices?

Run evals on this skill

Adds up to 20 points to the overall score

View guide

SecuritybySnyk

Passed

No findings from the security scan

Overview
Quality
Evals
Security
Files

hypothesis-reference.mdreferences/

Hypothesis reference

Full lookup tables for hypothesis-testing: the strategy catalog, the @settings fields, anti-patterns, and limitations. Source: Hypothesis quickstart.

Strategies catalog

StrategyGeneratesUseful for
st.integers(min, max)Bounded / unbounded integersNumeric inputs.
st.floats(min, max, allow_nan, allow_infinity)Floats with optional special-value handlingNumeric edge cases (NaN, ±Inf, denormals).
st.text(alphabet, min_size, max_size)StringsText inputs.
st.binary(min_size, max_size)BytesBinary protocol inputs.
st.lists(elements, min_size, max_size, unique)ListsCollection inputs.
st.dictionaries(keys, values)DictsMap inputs.
st.tuples(*element_strategies)TuplesMulti-field inputs.
st.from_regex(pattern, fullmatch=True)Strings matching a regexFormat-validated inputs (emails, dates).
st.sampled_from(iterable)One of a fixed setEnum-like inputs.
st.builds(callable, **kwargs)Construct objects from strategiesDomain objects.
st.composite (decorator)Custom strategy combining drawsDependent fields.

Settings

SettingDefaultUse
max_examples100More cases for higher confidence; budget against runtime.
deadline200 msPer-test time budget; None to disable.
derandomizeFalseTrue = same seed each run; useful for CI determinism.
phasesallDisable Phase.shrink to skip shrinking on slow tests.
verbositynormalquiet / normal / verbose / debug.

Anti-patterns

Anti-patternWhy it failsFix
Heavy assume() filtering (>50% rejection rate)Slow; Hypothesis warns; sometimes fails the test entirely.Restructure the strategy (Step 5).
Random seed in CI (default Hypothesis behavior)Tests pass locally, fail on CI; un-reproducible.derandomize=True or --hypothesis-seed=<fixed> (Step 6).
Asserting on specific generated valuesDefeats the property; regression tests should use @example.Property tests assert relationships; specifics go in @example (Step 9).
Overly broad strategies (st.text() for an email field)Wastes generation budget on non-meaningful inputs.Use st.from_regex(EMAIL_PATTERN) or domain-specific composite.
Property that's secretly an example test (one assertion on n=10)No property; just an example.Re-formulate as a real property (round-trip / metamorphic / invariant).
max_examples=10000 for a 5-second-per-case testCI never finishes.Budget per Total runtime / max_examples calculation.
Mocking inside the property testMocks don't satisfy properties; defeats PBT.Use real implementations OR property-test pure functions.

Limitations

  • Shrinking time. Complex strategies can take 30+ seconds to shrink a failing case to its minimum. Disable shrinking (phases=...) for slow tests where the un-shrunk failure is enough.
  • Non-determinism in tested code. A function whose output depends on time.now() or random state breaks PBT. Inject these as parameters or use Hypothesis's seeds for the randomness.
  • No statistical guarantee. 100 examples is convention, not proof. For correctness-critical code, supplement with formal methods.
  • Strategy composition complexity. Building strategies for deeply nested data takes effort; the payoff is reusability.
  • Test-runner integration. Hypothesis adapts to pytest / unittest; some custom runners need explicit support.

SKILL.md

tile.json