Debian Codegraph @

hypothesis-python/docs/tutorial/settings.rst

raw
1 Configuring test settings
2 =========================
3
4 This page discusses how to configure the behavior of a single Hypothesis test, or of an entire test suite.
5
6 Configuring a single test
7 -------------------------
8
9 Hypothesis lets you configure the default behavior of a test using the |@settings| decorator. You can use settings to configure how many examples Hypothesis generates, how Hypothesis replays failing examples, and the verbosity level of the test, among others.
10
11 Using |@settings| on a single test looks like this:
12
13 .. code-block:: python
14
15 from hypothesis import given, settings, strategies as st
16
17 @given(st.integers())
18 @settings(max_examples=200)
19 def runs_200_times_instead_of_100(n):
20 pass
21
22 You can put |@settings| either before or after |@given|. Both are equivalent.
23
24 Changing the number of examples
25 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
26
27 If you have a test which is very expensive or very cheap to run, you can change the number of examples (inputs) Hypothesis generates with the |max_examples| setting:
28
29 .. code-block:: python
30
31 from hypothesis import given, settings, strategies as st
32
33 @given(st.integers())
34 @settings(max_examples=5)
35 def test(n):
36 print("prints five times")
37
38 The default is 100 examples.
39
40 .. note::
41
42 See :doc:`../explanation/example-count` for details on how |max_examples| interacts with other parts of Hypothesis.
43
44
45 Other settings options
46 ~~~~~~~~~~~~~~~~~~~~~~
47
48 Here are a few of the more commonly used setting values:
49
50 * |settings.phases| controls which phases of Hypothesis run, like replaying from the database or generating new inputs.
51 * |settings.database| controls how and if Hypothesis replays failing examples.
52 * |settings.verbosity| can print debug information.
53 * |settings.derandomize| makes Hypothesis deterministic. (`Two kinds of testing <https://blog.nelhage.com/post/two-kinds-of-testing/>`__ discusses when and why you might want that).
54
55 .. note::
56
57 See the |settings| reference for a full list of possible settings.
58
59
60 Changing settings across your test suite
61 ----------------------------------------
62
63 In addition to configuring individual test functions with |@settings|, you can configure test behavior across your test suite using a settings profile. This might be useful for creating a development settings profile which runs fewer examples, or a settings profile in CI which connects to a separate database.
64
65 To create a settings profile, use |settings.register_profile|:
66
67 .. code-block:: python
68
69 from hypothesis import HealthCheck, settings
70
71 settings.register_profile("fast", max_examples=10)
72
73 You can place this code in any file which gets loaded before your tests get run. This includes an ``__init__.py`` file in the test directory or any of the test files themselves. If using pytest, the standard location to place this code is in a ``confest.py`` file (though an ``__init__.py`` or test file will also work).
74
75 Note that registering a new profile will not affect tests until it is loaded with |settings.load_profile|:
76
77 .. code-block:: python
78
79 from hypothesis import HealthCheck, settings
80
81 settings.register_profile("fast", max_examples=10)
82
83 # any tests executed before loading this profile will still use the
84 # default active profile of 100 examples.
85
86 settings.load_profile("fast")
87
88 # any tests executed after this point will use the active fast
89 # profile of 10 examples.
90
91 There is no limit to the number of settings profiles you can create. Hypothesis creates a profile called ``"default"``, which is active by default. You can also explicitly make it active at any time using ``settings.load_profile("default")``, if for instance you wanted to revert a custom profile you had previously loaded.
92
93 Loading profiles from environment variables
94 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
95
96 Using an environment variable to load a settings profile is a useful trick for choosing a settings profile depending on the environment:
97
98 .. code-block:: pycon
99
100 >>> import os
101 >>> from hypothesis import settings, Verbosity
102 >>> settings.register_profile("long", max_examples=1000)
103 >>> settings.register_profile("fast", max_examples=10)
104 >>> settings.register_profile("debug", max_examples=10, verbosity=Verbosity.verbose)
105 >>> settings.load_profile(os.getenv("HYPOTHESIS_PROFILE", "default"))
106
107 If using pytest, you can also easily select the active profile with ``--hypothesis-profile``:
108
109 .. code:: bash
110
111 $ pytest --hypothesis-profile fast
112
113 See the :ref:`Hypothesis pytest plugin <pytest-plugin>`.