MCPcopy Create free account
hub / github.com/pytest-dev/pytest / _make_xunit_fixture

Function _make_xunit_fixture

src/_pytest/unittest.py:122–180  ·  view source on GitHub ↗
(
    obj: type,
    setup_name: str,
    teardown_name: str,
    cleanup_name: Optional[str],
    scope: Scope,
    pass_self: bool,
)

Source from the content-addressed store, hash-verified

120
121
122def _make_xunit_fixture(
123 obj: type,
124 setup_name: str,
125 teardown_name: str,
126 cleanup_name: Optional[str],
127 scope: Scope,
128 pass_self: bool,
129):
130 setup = getattr(obj, setup_name, None)
131 teardown = getattr(obj, teardown_name, None)
132 if setup is None and teardown is None:
133 return None
134
135 if cleanup_name:
136 cleanup = getattr(obj, cleanup_name, lambda *args: None)
137 else:
138
139 def cleanup(*args):
140 pass
141
142 @pytest.fixture(
143 scope=scope.value,
144 autouse=True,
145 # Use a unique name to speed up lookup.
146 name=f"_unittest_{setup_name}_fixture_{obj.__qualname__}",
147 )
148 def fixture(self, request: FixtureRequest) -> Generator[None, None, None]:
149 if _is_skipped(self):
150 reason = self.__unittest_skip_why__
151 raise pytest.skip.Exception(reason, _use_item_location=True)
152 if setup is not None:
153 try:
154 if pass_self:
155 setup(self, request.function)
156 else:
157 setup()
158 # unittest does not call the cleanup function for every BaseException, so we
159 # follow this here.
160 except Exception:
161 if pass_self:
162 cleanup(self)
163 else:
164 cleanup()
165
166 raise
167 yield
168 try:
169 if teardown is not None:
170 if pass_self:
171 teardown(self, request.function)
172 else:
173 teardown()
174 finally:
175 if pass_self:
176 cleanup(self)
177 else:
178 cleanup()
179

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected