| 1 |
.. _strategies: |
| 2 |
|
| 3 |
Strategies Reference |
| 4 |
==================== |
| 5 |
|
| 6 |
Strategies are the way Hypothesis describes the values for |@given| to generate. For instance, passing the strategy ``st.lists(st.integers(), min_size=1)`` to |@given| tells Hypothesis to generate lists of integers with at least one element. |
| 7 |
|
| 8 |
This reference page lists all of Hypothesis' first-party functions which return a strategy. There are also many provided by :doc:`third-party libraries </extensions>`. Note that we often say "strategy" when we mean "function returning a strategy"; it's usually clear from context which one we mean. |
| 9 |
|
| 10 |
Strategies can be passed to other strategies as arguments, combined using :ref:`combinator strategies <combinators>`, or modified using |.filter|, |.map|, or |.flatmap|. |
| 11 |
|
| 12 |
Primitives |
| 13 |
---------- |
| 14 |
|
| 15 |
.. autofunction:: hypothesis.strategies.none |
| 16 |
.. autofunction:: hypothesis.strategies.nothing |
| 17 |
.. autofunction:: hypothesis.strategies.just |
| 18 |
.. autofunction:: hypothesis.strategies.booleans |
| 19 |
|
| 20 |
Numeric |
| 21 |
------- |
| 22 |
|
| 23 |
.. seealso:: |
| 24 |
|
| 25 |
See also the separate sections for :ref:`Numpy strategies <hypothesis-numpy>`, :ref:`Pandas strategies <hypothesis-pandas>`, and :ref:`Array API strategies <array-api>`. |
| 26 |
|
| 27 |
.. autofunction:: hypothesis.strategies.integers |
| 28 |
.. autofunction:: hypothesis.strategies.floats |
| 29 |
.. autofunction:: hypothesis.strategies.complex_numbers |
| 30 |
.. autofunction:: hypothesis.strategies.decimals |
| 31 |
.. autofunction:: hypothesis.strategies.fractions |
| 32 |
|
| 33 |
Strings |
| 34 |
------- |
| 35 |
|
| 36 |
.. note:: |
| 37 |
|
| 38 |
The |st.uuids| and |st.ip_addresses| strategies generate instances of :mod:`UUID <python:uuid>` and :mod:`IPAddress <python:ipaddress>` respectively. You can generate corresponding string values by using |.map|, such as ``st.uuids().map(str)``. |
| 39 |
|
| 40 |
.. autofunction:: hypothesis.strategies.text |
| 41 |
.. autofunction:: hypothesis.strategies.characters |
| 42 |
.. autofunction:: hypothesis.strategies.from_regex |
| 43 |
.. autofunction:: hypothesis.strategies.binary |
| 44 |
.. autofunction:: hypothesis.strategies.emails |
| 45 |
|
| 46 |
.. autofunction:: hypothesis.provisional.domains |
| 47 |
|
| 48 |
.. warning:: |
| 49 |
|
| 50 |
The |st.domains| strategy is provisional. Its interface may be changed in a minor release, without being subject to our :ref:`deprecation policy <deprecation-policy>`. That said, we expect it to be relatively stable. |
| 51 |
|
| 52 |
.. autofunction:: hypothesis.provisional.urls |
| 53 |
|
| 54 |
.. warning:: |
| 55 |
|
| 56 |
The |st.urls| strategy is provisional. Its interface may be changed in a minor release, without being subject to our :ref:`deprecation policy <deprecation-policy>`. That said, we expect it to be relatively stable. |
| 57 |
|
| 58 |
Collections |
| 59 |
----------- |
| 60 |
|
| 61 |
.. autofunction:: hypothesis.strategies.lists |
| 62 |
.. autofunction:: hypothesis.strategies.tuples |
| 63 |
.. autofunction:: hypothesis.strategies.sets |
| 64 |
.. autofunction:: hypothesis.strategies.frozensets |
| 65 |
.. autofunction:: hypothesis.strategies.dictionaries |
| 66 |
.. autofunction:: hypothesis.strategies.fixed_dictionaries |
| 67 |
.. autofunction:: hypothesis.strategies.iterables |
| 68 |
|
| 69 |
Datetime |
| 70 |
-------- |
| 71 |
|
| 72 |
.. autofunction:: hypothesis.strategies.dates |
| 73 |
.. autofunction:: hypothesis.strategies.times |
| 74 |
.. autofunction:: hypothesis.strategies.datetimes |
| 75 |
.. autofunction:: hypothesis.strategies.timezones |
| 76 |
.. autofunction:: hypothesis.strategies.timezone_keys |
| 77 |
.. autofunction:: hypothesis.strategies.timedeltas |
| 78 |
|
| 79 |
Recursive |
| 80 |
--------- |
| 81 |
|
| 82 |
.. autofunction:: hypothesis.strategies.recursive |
| 83 |
.. autofunction:: hypothesis.strategies.deferred |
| 84 |
|
| 85 |
Random |
| 86 |
------ |
| 87 |
|
| 88 |
.. autofunction:: hypothesis.strategies.randoms |
| 89 |
.. autofunction:: hypothesis.strategies.random_module |
| 90 |
.. autofunction:: hypothesis.register_random |
| 91 |
|
| 92 |
.. _combinators: |
| 93 |
|
| 94 |
Combinators |
| 95 |
----------- |
| 96 |
|
| 97 |
.. autofunction:: hypothesis.strategies.one_of |
| 98 |
.. autofunction:: hypothesis.strategies.builds |
| 99 |
.. autofunction:: hypothesis.strategies.composite |
| 100 |
.. autofunction:: hypothesis.strategies.data |
| 101 |
|
| 102 |
Typing |
| 103 |
------ |
| 104 |
|
| 105 |
.. autofunction:: hypothesis.strategies.from_type |
| 106 |
.. autofunction:: hypothesis.strategies.register_type_strategy |
| 107 |
|
| 108 |
Hypothesis |
| 109 |
---------- |
| 110 |
|
| 111 |
.. autofunction:: hypothesis.strategies.runner |
| 112 |
.. autofunction:: hypothesis.strategies.shared |
| 113 |
|
| 114 |
Misc |
| 115 |
---- |
| 116 |
|
| 117 |
.. autofunction:: hypothesis.strategies.functions |
| 118 |
.. autofunction:: hypothesis.strategies.slices |
| 119 |
.. autofunction:: hypothesis.strategies.uuids |
| 120 |
.. autofunction:: hypothesis.strategies.ip_addresses |
| 121 |
|
| 122 |
.. autofunction:: hypothesis.strategies.sampled_from |
| 123 |
.. autofunction:: hypothesis.strategies.permutations |
| 124 |
|
| 125 |
Related |
| 126 |
------- |
| 127 |
|
| 128 |
.. autoclass:: hypothesis.strategies.DrawFn |
| 129 |
.. autoclass:: hypothesis.strategies.DataObject |
| 130 |
|
| 131 |
.. automethod:: hypothesis.strategies.DataObject.draw |
| 132 |
|
| 133 |
.. autoclass:: hypothesis.strategies.SearchStrategy |
| 134 |
|
| 135 |
.. automethod:: hypothesis.strategies.SearchStrategy.example |
| 136 |
.. automethod:: hypothesis.strategies.SearchStrategy.filter |
| 137 |
.. automethod:: hypothesis.strategies.SearchStrategy.map |
| 138 |
.. automethod:: hypothesis.strategies.SearchStrategy.flatmap |
| 139 |
|
| 140 |
.. _hypothesis-numpy: |
| 141 |
|
| 142 |
NumPy |
| 143 |
----- |
| 144 |
|
| 145 |
Hypothesis offers a number of strategies for `NumPy <https://numpy.org/>`_ testing, |
| 146 |
available in the ``hypothesis[numpy]`` :doc:`extra </extras>`. |
| 147 |
It lives in the ``hypothesis.extra.numpy`` package. |
| 148 |
|
| 149 |
The centerpiece is the :func:`~hypothesis.extra.numpy.arrays` strategy, which generates arrays with |
| 150 |
any dtype, shape, and contents you can specify or give a strategy for. |
| 151 |
To make this as useful as possible, strategies are provided to generate array |
| 152 |
shapes and generate all kinds of fixed-size or compound dtypes. |
| 153 |
|
| 154 |
.. automodule:: hypothesis.extra.numpy |
| 155 |
:members: |
| 156 |
:exclude-members: ArrayStrategy, BasicIndexStrategy, BroadcastableShapes, MutuallyBroadcastableShapesStrategy |
| 157 |
|
| 158 |
|
| 159 |
.. _hypothesis-pandas: |
| 160 |
|
| 161 |
pandas |
| 162 |
------ |
| 163 |
|
| 164 |
Hypothesis provides strategies for several of the core pandas data types: |
| 165 |
:class:`pandas.Index`, :class:`pandas.Series` and :class:`pandas.DataFrame`. |
| 166 |
|
| 167 |
The general approach taken by the pandas module is that there are multiple |
| 168 |
strategies for generating indexes, and all of the other strategies take the |
| 169 |
number of entries they contain from their index strategy (with sensible defaults). |
| 170 |
So e.g. a Series is specified by specifying its :class:`numpy.dtype` (and/or |
| 171 |
a strategy for generating elements for it). |
| 172 |
|
| 173 |
.. automodule:: hypothesis.extra.pandas |
| 174 |
:members: |
| 175 |
|
| 176 |
Supported versions |
| 177 |
~~~~~~~~~~~~~~~~~~ |
| 178 |
|
| 179 |
There is quite a lot of variation between pandas versions. We only |
| 180 |
commit to supporting the latest version of pandas, but older minor versions are |
| 181 |
supported on a "best effort" basis. Hypothesis is currently tested against |
| 182 |
and confirmed working with every Pandas minor version from 1.1 through to 2.2. |
| 183 |
|
| 184 |
Releases that are not the latest patch release of their minor version are not |
| 185 |
tested or officially supported, but will probably also work unless you hit a |
| 186 |
pandas bug. |
| 187 |
|
| 188 |
|
| 189 |
.. _array-api: |
| 190 |
|
| 191 |
Array API |
| 192 |
--------- |
| 193 |
|
| 194 |
.. note:: |
| 195 |
|
| 196 |
Several array libraries have more library-specific strategies, including :pypi:`Xarray <xarray>` (via their :ref:`upstream strategies <xarray:testing.hypothesis>`) and :pypi:`NumPy` (via :ref:`its Hypothesis extra <hypothesis-numpy>`). Of course, strategies in the Array API namespace can still be used to test Xarray or NumPy, just like any other array library. |
| 197 |
|
| 198 |
Hypothesis offers strategies for `Array API <https://data-apis.org/>`_ adopting |
| 199 |
libraries in the ``hypothesis.extra.array_api`` package. See :issue:`3037` for |
| 200 |
more details. If you want to test with :pypi:`CuPy <cupy>`, :pypi:`Dask <dask>`, :pypi:`JAX <jax>`, |
| 201 |
:pypi:`MXNet <maxnet>`, :pypi:`PyTorch <torch>`, :pypi:`TensorFlow <tensorflow>`, or :pypi:`Xarray <xarray>` - |
| 202 |
or just :pypi:`NumPy <numpy>` - this is the extension for you! |
| 203 |
|
| 204 |
.. autofunction:: hypothesis.extra.array_api.make_strategies_namespace |
| 205 |
|
| 206 |
The resulting namespace contains all our familiar strategies like |
| 207 |
:func:`~xps.arrays` and :func:`~xps.from_dtype`, but based on the Array API |
| 208 |
standard semantics and returning objects from the ``xp`` module: |
| 209 |
|
| 210 |
.. automodule:: xps |
| 211 |
:members: |
| 212 |
from_dtype, |
| 213 |
arrays, |
| 214 |
array_shapes, |
| 215 |
scalar_dtypes, |
| 216 |
boolean_dtypes, |
| 217 |
numeric_dtypes, |
| 218 |
real_dtypes, |
| 219 |
integer_dtypes, |
| 220 |
unsigned_integer_dtypes, |
| 221 |
floating_dtypes, |
| 222 |
complex_dtypes, |
| 223 |
valid_tuple_axes, |
| 224 |
broadcastable_shapes, |
| 225 |
mutually_broadcastable_shapes, |
| 226 |
indices, |
| 227 |
|
| 228 |
.. _django-strategies: |
| 229 |
|
| 230 |
Django |
| 231 |
------ |
| 232 |
|
| 233 |
.. seealso:: |
| 234 |
|
| 235 |
See the :ref:`Django API reference <hypothesis-django>` for documentation on testing Django with Hypothesis. |
| 236 |
|
| 237 |
.. autofunction:: hypothesis.extra.django.from_model |
| 238 |
.. autofunction:: hypothesis.extra.django.from_form |
| 239 |
.. autofunction:: hypothesis.extra.django.from_field |
| 240 |
.. autofunction:: hypothesis.extra.django.register_field_strategy |
| 241 |
|
| 242 |
.. _hypothesis-lark: |
| 243 |
|
| 244 |
hypothesis[lark] |
| 245 |
---------------- |
| 246 |
|
| 247 |
.. note:: |
| 248 |
|
| 249 |
Strategies in this module require the ``hypothesis[lark]`` :doc:`extra </extras>`, via ``pip install hypothesis[lark]``. |
| 250 |
|
| 251 |
.. automodule:: hypothesis.extra.lark |
| 252 |
:members: |
| 253 |
|
| 254 |
Example grammars, which may provide a useful starting point for your tests, can be found |
| 255 |
`in the Lark repository <https://github.com/lark-parser/lark/tree/master/examples>`__ |
| 256 |
and in `this third-party collection <https://github.com/ligurio/lark-grammars>`__. |
| 257 |
|
| 258 |
.. _hypothesis-pytz: |
| 259 |
|
| 260 |
hypothesis[pytz] |
| 261 |
---------------- |
| 262 |
|
| 263 |
.. note:: |
| 264 |
|
| 265 |
Strategies in this module require the ``hypothesis[pytz]`` :doc:`extra </extras>`, via ``pip install hypothesis[pytz]``. |
| 266 |
|
| 267 |
.. automodule:: hypothesis.extra.pytz |
| 268 |
:members: |
| 269 |
|
| 270 |
.. _hypothesis-dateutil: |
| 271 |
|
| 272 |
hypothesis[dateutil] |
| 273 |
-------------------- |
| 274 |
|
| 275 |
.. note:: |
| 276 |
|
| 277 |
Strategies in this module require the ``hypothesis[dateutil]`` :doc:`extra </extras>`, via ``pip install hypothesis[dateutil]``. |
| 278 |
|
| 279 |
.. automodule:: hypothesis.extra.dateutil |
| 280 |
:members: |