Debian Codegraph @

doc/overview.rst

raw
1 Overview
2 ========
3
4 ``pytest-regressions`` provides some fixtures that make it easy to maintain tests that
5 generate lots of data or specific data files like images.
6
7 This plugin uses a *data directory* (courtesy of `pytest-datadir <https://github.com/gabrielcnr/pytest-datadir>`_) to
8 store expected data files, which are stored and used as baseline for future test runs.
9
10 Example
11 -------
12
13 Let's use ``data_regression`` as an example, but the workflow is the same for the other ``*_regression`` fixtures.
14
15 Suppose we have a ``summary_grids`` function which outputs a dictionary containing information about discrete grids
16 for simulation. Of course your function would actually return some computed/read value, but here it is using an inline
17 result for this example:
18
19 .. code-block:: python
20
21 def summary_grids():
22 return {
23 "Main Grid": {
24 "id": 0,
25 "cell_count": 1000,
26 "active_cells": 300,
27 "properties": [
28 {"name": "Temperature", "min": 75, "max": 85},
29 {"name": "Porosity", "min": 0.3, "max": 0.4},
30 ],
31 },
32 "Refin1": {
33 "id": 1,
34 "cell_count": 48,
35 "active_cells": 44,
36 "properties": [
37 {"name": "Temperature", "min": 78, "max": 81},
38 {"name": "Porosity", "min": 0.36, "max": 0.39},
39 ],
40 },
41 }
42
43
44 We could test the results of this function like this:
45
46
47 .. code-block:: python
48
49 def test_grids():
50 data = summary_grids()
51 assert data["Main Grid"]["id"] == 0
52 assert data["Main Grid"]["cell_count"] == 1000
53 assert data["Main Grid"]["active_cells"] == 300
54 assert data["Main Grid"]["properties"] == [
55 {"name": "Temperature", "min": 75, "max": 85},
56 {"name": "Porosity", "min": 0.3, "max": 0.4},
57 ]
58 ...
59
60
61 But this presents a number of problems:
62
63 * Gets old quickly.
64 * Error-prone.
65 * If a check fails, we don't know what else might be wrong with the obtained data.
66 * Does not scale for large data.
67 * **Maintenance burden**: if the data changes in the future (and it will) it will be a major headache to update the values,
68 specially if there are a lot of similar tests like this one.
69
70
71 Using data_regression
72 ---------------------
73
74 The ``data_regression`` fixture provides a method to check general dictionary data like the one in the previous example.
75
76 There is no need to import anything, just declare the ``data_regression`` fixture in your test's
77 arguments and call the ``check`` method in the test:
78
79 .. code-block:: python
80
81 def test_grids2(data_regression):
82 data = summary_grids()
83 data_regression.check(data)
84
85
86 The first time your run this test, it will *fail* with a message like this::
87
88
89 > pytest.fail(msg)
90 E Failed: File not found in data directory, created:
91 E - C:\Users\bruno\pytest-regressions\tests\test_grids\test_grids2.yml
92
93 The fixture will generate a ``test_grids2.yml`` file (same name as the test) in the *data directory* with the contents of the dictionary:
94
95 .. code-block:: yaml
96
97 Main Grid:
98 active_cells: 300
99 cell_count: 1000
100 id: 0
101 properties:
102 - max: 85
103 min: 75
104 name: Temperature
105 - max: 0.4
106 min: 0.3
107 name: Porosity
108 Refin1:
109 active_cells: 44
110 cell_count: 48
111 id: 1
112 properties:
113 - max: 81
114 min: 78
115 name: Temperature
116 - max: 0.39
117 min: 0.36
118 name: Porosity
119
120 This file should be committed to version control.
121
122 The next time you run this test, it will compare the results of ``summary_grids()`` with the contents of the YAML file.
123 If they match, the test passes. If they don't match the test will fail, showing a nice diff of the text differences.
124
125 ``--force-regen``
126 ~~~~~~~~~~~~~~~~~
127
128 If the test fails because the new data is correct (the implementation might be returning more information about the
129 grids for example), then you can use the ``--force-regen`` flag to update the expected file::
130
131 $ pytest --force-regen
132
133
134 This will fail the same test but with a different message saying that the file has been updated. Commit the new file.
135
136 This workflow makes it very simple to keep the files up to date and to check all the information we need.
137
138 ``--regen-all``
139 ~~~~~~~~~~~~~~~~~
140
141 If a single change will fail several regression tests, you can also use the ``--regen-all`` command-line flag::
142
143 $ pytest --regen-all
144
145
146 With this flag, the regression fixtures will regenerate all files but will not fail the tests themselves. This make it very
147 easy to update all regression files in a single pytest run when individual tests contain multiple regressions.
148
149
150 Parametrized tests
151 ------------------
152
153 When using parametrized tests, pytest will give each parametrization of your test a unique name.
154 This means that ``pytest-regressions`` will create a new file for each parametrization too.
155
156 Suppose we have an additional function ``summary_grids_2`` that generates longer data, we can
157 re-use the same test with the ``@pytest.mark.parametrize`` decorator:
158
159 .. code-block:: python
160
161 @pytest.mark.parametrize('data', [summary_grids(), summary_grids_2()])
162 def test_grids3(data_regression, data):
163 data_regression.check(data)
164
165 Pytest will automatically name these as ``test_grids3[data0]`` and ``test_grids3[data1]``, so files
166 ``test_grids3_data0.yml`` and ``test_grids3_data1.yml`` will be created.
167
168 The names of these files can be controlled using the ``ids`` `keyword for parametrize
169 <https://docs.pytest.org/en/stable/example/parametrize.html#different-options-for-test-ids>`_, so
170 instead of ``data0``, you can define more useful names such as ``short`` and ``long``:
171
172 .. code-block:: python
173
174 @pytest.mark.parametrize('data', [summary_grids(), summary_grids_2()], ids=['short', 'long'])
175 def test_grids3(data_regression, data):
176 data_regression.check(data)
177
178 which creates ``test_grids3_short.yml`` and ``test_grids3_long.yml`` respectively.