| 1 |
Built-in strategies |
| 2 |
=================== |
| 3 |
|
| 4 |
This page shows some of the strategies that Hypothesis provides for you. |
| 5 |
|
| 6 |
Strategies provided by Hypothesis |
| 7 |
--------------------------------- |
| 8 |
|
| 9 |
Here is a selection of strategies provided by Hypothesis that may be useful to know: |
| 10 |
|
| 11 |
|st.integers| |
| 12 |
Generates integers. |
| 13 |
|st.floats| |
| 14 |
Generates floats. |
| 15 |
|st.booleans| |
| 16 |
Generates booleans. |
| 17 |
|st.text| |
| 18 |
Generates unicode strings (i.e., instances of |str|). Can be constrained to ASCII with ``st.text(st.characters(codec="ascii"))``. |
| 19 |
|st.lists| |
| 20 |
Generates lists with elements from the strategy passed to it. ``st.lists(st.integers())`` generates lists of integers. |
| 21 |
|st.tuples| |
| 22 |
Generates tuples of a fixed length. ``st.tuples(st.integers(), st.floats())`` generates tuples with two elements, where the first element is an integer and the second is a float. |
| 23 |
|st.one_of| |
| 24 |
Generates from any of the strategies passed to it. ``st.one_of(st.integers(), st.floats())`` generates either integers or floats. You can also use ``|`` to construct |st.one_of|, like ``st.integers() | st.floats()``. |
| 25 |
|st.builds| |
| 26 |
Generates instances of a class (or other callable) by specifying a strategy for each argument, like ``st.builds(Person, name=st.text(), age=st.integers())``. |
| 27 |
|st.just| |
| 28 |
Generates the exact value passed to it. ``st.just("a")`` generates the exact string ``"a"``. This is useful when something expects to be passed a strategy. For instance, ``st.lists(st.integers() | st.just("a"))`` generates lists whose elements are either integers or the string ``"a"``. |
| 29 |
|st.sampled_from| |
| 30 |
Generates a random value from a list. ``st.sampled_from(["a", 1])`` is roughly equivalent to ``st.just("a") | st.just(1)``. |
| 31 |
|st.none| |
| 32 |
Generates ``None``. Useful for parameters that can be optional, like ``st.integers() | st.none()``. |
| 33 |
|
| 34 |
.. seealso:: |
| 35 |
|
| 36 |
See the :doc:`strategies API reference </reference/strategies>` for a full list of strategies provided by Hypothesis. |