Meaning
Returns a single example value from a Hypothesis strategy that generates lists of integers. Eliminates the need to manually devise representative test data when writing property‑based tests. Used when a quick concrete sample is needed to inspect a strategy’s output or to seed a manual test.
Primary Function
Test data generation
Communicative Purpose
Provides a concrete example from a hypothesis strategy for inspection or testing.
Pattern
st.lists(element_strategy).example()
Core Structure
st.lists(...).example()
Função primária
Test data generation
Propósito comunicativo
Provides a concrete example from a hypothesis strategy for inspection or testing.
Situações de gatilho
Property-based testing: needing a sample list of integers to visualize test inputs. Debugging: quickly generating example data to reproduce a failing case. API prototyping: producing example payloads for manual endpoint testing.
Contextos
Hypothesis library, Python testing frameworks, property‑based testing
Padrão
st.lists(element_strategy).example()
Estrutura central
st.lists(...).example()
Slots de substituição
element_strategy: a Hypothesis strategy (e.g., st.integers(), st.text())
Colocados típicos
- @given decorator
- assert statements
- pytest
Substituições comuns
- st.builds(...) for constructing objects (more explicit)
- using list literals directly (less flexible
- no shrinking)
Erros comuns
Forgot to import hypothesis.strategies as st → NameError when st is undefined. Passed a plain Python type like int instead of a strategy → TypeError: argument must be a SearchStrategy. Called .example() on a strategy with unsatisfiable constraints (e.g., st.lists(min_size=5, max_size=3)) → Unsatisfiable. Used .example() inside a test without @given assuming it gives varied examples each run → same example each invocation, reducing test effectiveness. Neglected to install hypothesis → ImportError.
Similar / contraste
st.lists(...).filter(...) → adds a predicate to generated elements. st.sets(...) → generates unique‑element collections. st.tuples(...) → produces fixed‑length heterogeneous tuples. st.one_of(...) → chooses between alternative strategies.
Interferências
Coming from unittest: may use hard‑coded literals instead of generated examples → reduces ability to test edge cases. Coming from Rust: might expect .example() to return an iterator → in Hypothesis it returns a single value. Coming from JavaScript: could assume .example() is asynchronous → it is synchronous and immediate.
Família do chunk
- st.integers()
- st.text()
- st.builds()
- st.one_of()
Nuance
Do not use for generating large datasets in performance‑critical loops; .example() is O(n) but intended for small samples. Boundary: strategies with impossible constraints raise UnsatisfiableError. Note that the example is deterministic per seed.
Efeito pragmático
Enables concise property‑based tests with automatic example generation and shrinking on failure.
Dica de memória
Like a vending machine that dispenses a random snack each time you press the button, giving you a quick taste of what the machine can produce.
Nota
The .example() method returns a single instance; for multiple examples use .filter() or list comprehension.
Upgrade path
Using @given with strategies for full property‑based testing or creating custom strategies with st.composite().
Log in to save chunks.