| 1 | --- |
| 2 | tags: technical, faq, python |
| 3 | date: 2016-08-09 10:00 |
| 4 | title: How do I use pytest fixtures with Hypothesis? |
| 5 | author: drmaciver |
| 6 | --- |
| 7 | |
| 8 | [pytest](http://doc.pytest.org/en/latest/) is a great test runner, and is the one |
| 9 | Hypothesis itself uses for testing (though Hypothesis works fine with other test |
| 10 | runners too). |
| 11 | |
| 12 | It has a fairly elaborate [fixture system](http://doc.pytest.org/en/latest/fixture.html), |
| 13 | and people are often unsure how that interacts with Hypothesis. In this article we'll |
| 14 | go over the details of how to use the two together. |
| 15 | |
| 16 | <!--more--> |
| 17 | |
| 18 | Mostly, Hypothesis and py.test fixtures don't interact: Each just ignores the other's |
| 19 | presence. |
| 20 | |
| 21 | When using a @given decorator, any arguments that are not provided in the @given |
| 22 | will be left visible in the final function: |
| 23 | |
| 24 | ```python |
| 25 | from inspect import signature |
| 26 | |
| 27 | from hypothesis import given, strategies as st |
| 28 | |
| 29 | |
| 30 | @given(a=st.none(), c=st.none()) |
| 31 | def test_stuff(a, b, c, d): |
| 32 | pass |
| 33 | |
| 34 | |
| 35 | print(signature(test_stuff)) |
| 36 | ``` |
| 37 | |
| 38 | This then outputs the following: |
| 39 | |
| 40 | ``` |
| 41 | <Signature (b, d)> |
| 42 | ``` |
| 43 | |
| 44 | We've hidden the arguments 'a' and 'c', but the unspecified arguments 'b' and 'd' |
| 45 | are still left to be passed in. In particular, they can be provided as py.test |
| 46 | fixtures: |
| 47 | |
| 48 | ```python |
| 49 | from pytest import fixture |
| 50 | |
| 51 | from hypothesis import given, strategies as st |
| 52 | |
| 53 | |
| 54 | @fixture |
| 55 | def stuff(): |
| 56 | return "kittens" |
| 57 | |
| 58 | |
| 59 | @given(a=st.none()) |
| 60 | def test_stuff(a, stuff): |
| 61 | assert a is None |
| 62 | assert stuff == "kittens" |
| 63 | ``` |
| 64 | |
| 65 | This also works if we want to use @given with positional arguments: |
| 66 | |
| 67 | ```python |
| 68 | from pytest import fixture |
| 69 | |
| 70 | from hypothesis import given, strategies as st |
| 71 | |
| 72 | |
| 73 | @fixture |
| 74 | def stuff(): |
| 75 | return "kittens" |
| 76 | |
| 77 | |
| 78 | @given(t.none()) |
| 79 | def test_stuff(stuff, a): |
| 80 | assert a is None |
| 81 | assert stuff == "kittens" |
| 82 | ``` |
| 83 | |
| 84 | The positional argument fills in from the right, replacing the 'a' |
| 85 | argument and leaving us with 'stuff' to be provided by the fixture. |
| 86 | |
| 87 | Personally I don't usually do this because I find it gets a bit |
| 88 | confusing - if I'm going to use fixtures then I always use the named |
| 89 | variant of given. There's no reason you *can't* do it this way if |
| 90 | you prefer though. |
| 91 | |
| 92 | @given also works fine in combination with parametrized tests: |
| 93 | |
| 94 | ```python |
| 95 | import pytest |
| 96 | |
| 97 | from hypothesis import given, strategies as st |
| 98 | |
| 99 | |
| 100 | @pytest.mark.parametrize("stuff", [1, 2, 3]) |
| 101 | @given(a=st.none()) |
| 102 | def test_stuff(a, stuff): |
| 103 | assert a is None |
| 104 | assert 1 <= stuff <= 3 |
| 105 | ``` |
| 106 | |
| 107 | This will run 3 tests, one for each value for 'stuff'. |
| 108 | |
| 109 | There is one unfortunate feature of how this interaction works though: In pytest |
| 110 | you can declare fixtures which do set up and tear down per function. These will |
| 111 | "work" with Hypothesis, but they will run once for the entire test function |
| 112 | rather than once for each time given calls your test function. So the following |
| 113 | will fail: |
| 114 | |
| 115 | ```python |
| 116 | from pytest import fixture |
| 117 | |
| 118 | from hypothesis import given, strategies as st |
| 119 | |
| 120 | counter = 0 |
| 121 | |
| 122 | |
| 123 | @fixture(scope="function") |
| 124 | def stuff(): |
| 125 | global counter |
| 126 | counter = 0 |
| 127 | |
| 128 | |
| 129 | @given(a=st.none()) |
| 130 | def test_stuff(a, stuff): |
| 131 | global counter |
| 132 | counter += 1 |
| 133 | assert counter == 1 |
| 134 | ``` |
| 135 | |
| 136 | The counter will not get reset at the beginning of each call to the test function, |
| 137 | so it will be incremented each time and the test will start failing after the |
| 138 | first call. |
| 139 | |
| 140 | There currently aren't any great ways around this unfortunately. The best you can |
| 141 | really do is do manual setup and teardown yourself in your tests using |
| 142 | Hypothesis (e.g. by implementing a version of your fixture as a context manager). |
| 143 | |
| 144 | Long-term, I'd like to resolve this by providing a mechanism for allowing fixtures |
| 145 | to be run for each example (it's probably not correct to have *every* function scoped |
| 146 | fixture run for each example), but for now it's stalled because it [requires changes |
| 147 | on the py.test side as well as the Hypothesis side](https://github.com/pytest-dev/pytest/issues/916) |
| 148 | and we haven't quite managed to find the time and place to collaborate on figuring |
| 149 | out how to fix this yet. |