@@ -53,7 +53,7 @@ use super::helpers::{
53
53
/// - `flake8-pytest-style.fixture-parentheses`
54
54
///
55
55
/// ## References
56
- /// - [API Reference: Fixtures](https://docs.pytest.org/en/latest/reference/reference.html#fixtures-api)
56
+ /// - [`pytest` documentation: API Reference: Fixtures](https://docs.pytest.org/en/latest/reference/reference.html#fixtures-api)
57
57
#[ violation]
58
58
pub struct PytestFixtureIncorrectParenthesesStyle {
59
59
expected : Parentheses ,
@@ -169,7 +169,7 @@ impl Violation for PytestIncorrectFixtureNameUnderscore {
169
169
/// ```
170
170
///
171
171
/// ## References
172
- /// - [API Reference : `pytest.mark.usefixtures`](https://docs.pytest.org/en/latest/reference/reference.html#pytest-mark-usefixtures)
172
+ /// - [`pytest` documentation : `pytest.mark.usefixtures`](https://docs.pytest.org/en/latest/reference/reference.html#pytest-mark-usefixtures)
173
173
#[ violation]
174
174
pub struct PytestFixtureParamWithoutValue {
175
175
name : String ,
@@ -217,7 +217,7 @@ impl Violation for PytestFixtureParamWithoutValue {
217
217
/// ```
218
218
///
219
219
/// ## References
220
- /// - [`yield_fixture` functions](https://docs.pytest.org/en/latest/yieldfixture.html)
220
+ /// - [`pytest` documentation: ` yield_fixture` functions](https://docs.pytest.org/en/latest/yieldfixture.html)
221
221
#[ violation]
222
222
pub struct PytestDeprecatedYieldFixture ;
223
223
@@ -228,6 +228,49 @@ impl Violation for PytestDeprecatedYieldFixture {
228
228
}
229
229
}
230
230
231
+ /// ## What it does
232
+ /// Checks for unnecessary `request.addfinalizer` usages in `pytest` fixtures.
233
+ ///
234
+ /// ## Why is this bad?
235
+ /// `pytest` offers two ways to perform cleanup in fixture code. The first is
236
+ /// sequential (via the `yield` statement), the second callback-based (via
237
+ /// `request.addfinalizer`).
238
+ ///
239
+ /// The sequential approach is more readable and should be preferred, unless
240
+ /// the fixture uses the "factory as fixture" pattern.
241
+ ///
242
+ /// ## Example
243
+ /// ```python
244
+ /// @pytest.fixture()
245
+ /// def my_fixture(request):
246
+ /// resource = acquire_resource()
247
+ /// request.addfinalizer(resource.release)
248
+ /// return resource
249
+ /// ```
250
+ ///
251
+ /// Use instead:
252
+ /// ```python
253
+ /// @pytest.fixture()
254
+ /// def my_fixture():
255
+ /// resource = acquire_resource()
256
+ /// yield resource
257
+ /// resource.release()
258
+ ///
259
+ ///
260
+ /// # "factory-as-fixture" pattern
261
+ /// @pytest.fixture()
262
+ /// def my_factory(request):
263
+ /// def create_resource(arg):
264
+ /// resource = acquire_resource(arg)
265
+ /// request.addfinalizer(resource.release)
266
+ /// return resource
267
+ ///
268
+ /// return create_resource
269
+ /// ```
270
+ ///
271
+ /// ## References
272
+ /// - [`pytest` documentation: Adding finalizers directly](https://docs.pytest.org/en/latest/how-to/fixtures.html#adding-finalizers-directly)
273
+ /// - [`pytest` documentation: Factories as fixtures](https://docs.pytest.org/en/latest/how-to/fixtures.html#factories-as-fixtures)
231
274
#[ violation]
232
275
pub struct PytestFixtureFinalizerCallback ;
233
276
@@ -237,7 +280,39 @@ impl Violation for PytestFixtureFinalizerCallback {
237
280
format ! ( "Use `yield` instead of `request.addfinalizer`" )
238
281
}
239
282
}
240
-
283
+ /// ## What it does
284
+ /// Checks for unnecessary `yield` expressions in `pytest` fixtures.
285
+ ///
286
+ /// ## Why is this bad?
287
+ /// In `pytest` fixtures, the `yield` expression should only be used for fixtures
288
+ /// that include teardown code, to clean up the fixture after the test function
289
+ /// has finished executing.
290
+ ///
291
+ /// ## Example
292
+ /// ```python
293
+ /// @pytest.fixture()
294
+ /// def my_fixture():
295
+ /// resource = acquire_resource()
296
+ /// yield resource
297
+ /// ```
298
+ ///
299
+ /// Use instead:
300
+ /// ```python
301
+ /// @pytest.fixture()
302
+ /// def my_fixture_with_teardown():
303
+ /// resource = acquire_resource()
304
+ /// yield resource
305
+ /// resource.release()
306
+ ///
307
+ ///
308
+ /// @pytest.fixture()
309
+ /// def my_fixture_without_teardown():
310
+ /// resource = acquire_resource()
311
+ /// return resource
312
+ /// ```
313
+ ///
314
+ /// ## References
315
+ /// - [`pytest` documentation: Teardown/Cleanup](https://docs.pytest.org/en/latest/how-to/fixtures.html#teardown-cleanup-aka-fixture-finalization)
241
316
#[ violation]
242
317
pub struct PytestUselessYieldFixture {
243
318
name : String ,
0 commit comments