diff --git a/crates/ruff/src/rules/flake8_pytest_style/rules/assertion.rs b/crates/ruff/src/rules/flake8_pytest_style/rules/assertion.rs index 1964fd3c44..e0fe8da267 100644 --- a/crates/ruff/src/rules/flake8_pytest_style/rules/assertion.rs +++ b/crates/ruff/src/rules/flake8_pytest_style/rules/assertion.rs @@ -69,6 +69,34 @@ impl Violation for PytestCompositeAssertion { } } +/// ## What it does +/// Checks for `assert` statements in `except` clauses. +/// +/// ## Why is this bad? +/// When testing for exceptions, `pytest.raises()` should be used instead of +/// `assert` statements in `except` clauses, as it's more explicit and +/// idiomatic. Further, `pytest.raises()` will fail if the exception is _not_ +/// raised, unlike the `assert` statement. +/// +/// ## Example +/// ```python +/// def test_foo(): +/// try: +/// 1 / 0 +/// except ZeroDivisionError as e: +/// assert e.args +/// ``` +/// +/// Use instead: +/// ```python +/// def test_foo(): +/// with pytest.raises(ZeroDivisionError) as exc_info: +/// 1 / 0 +/// assert exc_info.value.args +/// ``` +/// +/// ## References +/// - [API Reference: `pytest.raises`](https://docs.pytest.org/en/latest/reference/reference.html#pytest-raises) #[violation] pub struct PytestAssertInExcept { name: String, diff --git a/crates/ruff/src/rules/flake8_pytest_style/rules/fixture.rs b/crates/ruff/src/rules/flake8_pytest_style/rules/fixture.rs index b73cb4676e..5e4de7db65 100644 --- a/crates/ruff/src/rules/flake8_pytest_style/rules/fixture.rs +++ b/crates/ruff/src/rules/flake8_pytest_style/rules/fixture.rs @@ -129,6 +129,47 @@ impl Violation for PytestIncorrectFixtureNameUnderscore { } } +/// ## What it does +/// Checks for `pytest` test functions that should be decorated with +/// `@pytest.mark.usefixtures`. +/// +/// ## Why is this bad? +/// In `pytest`, fixture injection is used to activate fixtures in a test +/// function. +/// +/// Fixtures can be injected either by passing them as parameters to the test +/// function, or by using the `@pytest.mark.usefixtures` decorator. +/// +/// If the test function depends on the fixture being activated, but does not +/// use it in the test body or otherwise rely on its return value, prefer +/// the `@pytest.mark.usefixtures` decorator, to make the dependency explicit +/// and avoid the confusion caused by unused arguments. +/// +/// ## Example +/// ```python +/// @pytest.fixture +/// def _patch_something(): +/// ... +/// +/// +/// def test_foo(_patch_something): +/// ... +/// ``` +/// +/// Use instead: +/// ```python +/// @pytest.fixture +/// def _patch_something(): +/// ... +/// +/// +/// @pytest.mark.usefixtures("_patch_something") +/// def test_foo(): +/// ... +/// ``` +/// +/// ## References +/// - [API Reference: `pytest.mark.usefixtures`](https://docs.pytest.org/en/latest/reference/reference.html#pytest-mark-usefixtures) #[violation] pub struct PytestFixtureParamWithoutValue { name: String,