Debian Codegraph @

doc/en/proposals/parametrize_with_fixtures.rst

raw
1 :orphan:
2
3 ===================================
4 PROPOSAL: Parametrize with fixtures
5 ===================================
6
7 .. warning::
8
9 This document outlines a proposal around using fixtures as input
10 of parametrized tests or fixtures.
11
12 Problem
13 -------
14
15 As a user I have functional tests that I would like to run against various
16 scenarios.
17
18 In this particular example we want to generate a new project based on a
19 cookiecutter template. We want to test default values but also data that
20 emulates user input.
21
22 - use default values
23
24 - emulate user input
25
26 - specify 'author'
27
28 - specify 'project_slug'
29
30 - specify 'author' and 'project_slug'
31
32 This is how a functional test could look like:
33
34 .. code-block:: python
35
36 import pytest
37
38
39 @pytest.fixture
40 def default_context():
41 return {"extra_context": {}}
42
43
44 @pytest.fixture(
45 params=[
46 {"author": "alice"},
47 {"project_slug": "helloworld"},
48 {"author": "bob", "project_slug": "foobar"},
49 ]
50 )
51 def extra_context(request):
52 return {"extra_context": request.param}
53
54
55 @pytest.fixture(params=["default", "extra"])
56 def context(request):
57 if request.param == "default":
58 return request.getfuncargvalue("default_context")
59 else:
60 return request.getfuncargvalue("extra_context")
61
62
63 def test_generate_project(cookies, context):
64 """Call the cookiecutter API to generate a new project from a
65 template.
66 """
67 result = cookies.bake(extra_context=context)
68
69 assert result.exit_code == 0
70 assert result.exception is None
71 assert result.project.isdir()
72
73
74 Issues
75 ------
76
77 * By using ``request.getfuncargvalue()`` we rely on actual fixture function
78 execution to know what fixtures are involved, due to its dynamic nature
79 * More importantly, ``request.getfuncargvalue()`` cannot be combined with
80 parametrized fixtures, such as ``extra_context``
81 * This is very inconvenient if you wish to extend an existing test suite by
82 certain parameters for fixtures that are already used by tests
83
84 pytest version 3.0 reports an error if you try to run above code::
85
86 Failed: The requested fixture has no parameter defined for the current
87 test.
88
89 Requested fixture 'extra_context'
90
91
92 Proposed solution
93 -----------------
94
95 A new function that can be used in modules can be used to dynamically define
96 fixtures from existing ones.
97
98 .. code-block:: python
99
100 pytest.define_combined_fixture(
101 name="context", fixtures=["default_context", "extra_context"]
102 )
103
104 The new fixture ``context`` inherits the scope from the used fixtures and yield
105 the following values.
106
107 - ``{}``
108
109 - ``{'author': 'alice'}``
110
111 - ``{'project_slug': 'helloworld'}``
112
113 - ``{'author': 'bob', 'project_slug': 'foobar'}``
114
115 Alternative approach
116 --------------------
117
118 A new helper function named ``fixture_request`` would tell pytest to yield
119 all parameters marked as a fixture.
120
121 .. note::
122
123 The :pypi:`pytest-lazy-fixture` plugin implements a very
124 similar solution to the proposal below, make sure to check it out.
125
126 .. code-block:: python
127
128 @pytest.fixture(
129 params=[
130 pytest.fixture_request("default_context"),
131 pytest.fixture_request("extra_context"),
132 ]
133 )
134 def context(request):
135 """Returns all values for ``default_context``, one-by-one before it
136 does the same for ``extra_context``.
137
138 request.param:
139 - {}
140 - {'author': 'alice'}
141 - {'project_slug': 'helloworld'}
142 - {'author': 'bob', 'project_slug': 'foobar'}
143 """
144 return request.param
145
146 The same helper can be used in combination with ``pytest.mark.parametrize``.
147
148 .. code-block:: python
149
150
151 @pytest.mark.parametrize(
152 "context, expected_response_code",
153 [
154 (pytest.fixture_request("default_context"), 0),
155 (pytest.fixture_request("extra_context"), 0),
156 ],
157 )
158 def test_generate_project(cookies, context, exit_code):
159 """Call the cookiecutter API to generate a new project from a
160 template.
161 """
162 result = cookies.bake(extra_context=context)
163
164 assert result.exit_code == exit_code