diff options
author | Ondřej Nový <novy@ondrej.org> | 2016-02-14 19:26:09 +0100 |
---|---|---|
committer | Ondřej Nový <novy@ondrej.org> | 2016-02-14 19:26:09 +0100 |
commit | a85231280accdfec8ef9cf67213ff706eb242889 (patch) | |
tree | c84bb817ccf5bae224e473bb15959a4fb97d41ba /docs/fuzzy.rst | |
parent | d92aeedcf27326270cb3dcd8b780566728a489a9 (diff) | |
parent | 41560aa54e83fe539c0a5a1935bcaaf6363a522c (diff) | |
download | factory-boy-a85231280accdfec8ef9cf67213ff706eb242889.tar factory-boy-a85231280accdfec8ef9cf67213ff706eb242889.tar.gz |
Merge tag '2.6.1' into debian/unstable
Release of factory_boy 2.6.1
Diffstat (limited to 'docs/fuzzy.rst')
-rw-r--r-- | docs/fuzzy.rst | 39 |
1 files changed, 37 insertions, 2 deletions
diff --git a/docs/fuzzy.rst b/docs/fuzzy.rst index 1480419..6b06608 100644 --- a/docs/fuzzy.rst +++ b/docs/fuzzy.rst @@ -8,6 +8,8 @@ Some tests may be interested in testing with fuzzy, random values. This is handled by the :mod:`factory.fuzzy` module, which provides a few random declarations. +.. note:: Use ``import factory.fuzzy`` to load this module. + FuzzyAttribute -------------- @@ -62,8 +64,11 @@ FuzzyChoice The :class:`FuzzyChoice` fuzzer yields random choices from the given iterable. - .. note:: The passed in :attr:`choices` will be converted into a list at - declaration time. + .. note:: The passed in :attr:`choices` will be converted into a list upon + first use, not at declaration time. + + This allows passing in, for instance, a Django queryset that will + only hit the database during the database, not at import time. .. attribute:: choices @@ -338,3 +343,33 @@ They should inherit from the :class:`BaseFuzzyAttribute` class, and override its The method responsible for generating random values. *Must* be overridden in subclasses. + + +Managing randomness +------------------- + +Using :mod:`random` in factories allows to "fuzz" a program efficiently. +However, it's sometimes required to *reproduce* a failing test. + +:mod:`factory.fuzzy` uses a separate instance of :class:`random.Random`, +and provides a few helpers for this: + +.. method:: get_random_state() + + Call :meth:`get_random_state` to retrieve the random generator's current + state. + +.. method:: set_random_state(state) + + Use :meth:`set_random_state` to set a custom state into the random generator + (fetched from :meth:`get_random_state` in a previous run, for instance) + +.. method:: reseed_random(seed) + + The :meth:`reseed_random` function allows to load a chosen seed into the random generator. + + +Custom :class:`BaseFuzzyAttribute` subclasses **SHOULD** +use :obj:`factory.fuzzy._random` as a randomness source; this ensures that +data they generate can be regenerated using the simple state from +:meth:`get_random_state`. |