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 c70982d93cbab..dbb571c5272be 100644 --- a/crates/ruff/src/rules/flake8_pytest_style/rules/assertion.rs +++ b/crates/ruff/src/rules/flake8_pytest_style/rules/assertion.rs @@ -145,6 +145,30 @@ impl Violation for PytestAssertAlwaysFalse { } } +/// ## What it does +/// Checks for uses of assertion methods from the `unittest` module. +/// +/// ## Why is this bad? +/// To make use of `pytest`'s assertion rewriting, a regular `assert` statement +/// is preferred over `unittest`'s assertion methods. +/// +/// ## Example +/// ```python +/// class TestFoo(unittest.TestCase): +/// def test_foo(self): +/// self.assertEqual(a, b) +/// ``` +/// +/// Use instead: +/// ```python +/// class TestFoo(unittest.TestCase): +/// def test_foo(self): +/// assert a == b +/// ``` +/// +/// ## References +/// - [`pytest` documentation: Assertion introspection details](https://docs.pytest.org/en/7.1.x/how-to/assert.html#assertion-introspection-details) + #[violation] pub struct PytestUnittestAssertion { assertion: String, diff --git a/crates/ruff/src/rules/flake8_pytest_style/rules/patch.rs b/crates/ruff/src/rules/flake8_pytest_style/rules/patch.rs index fd6f10827697c..cfd9e7d8d35a9 100644 --- a/crates/ruff/src/rules/flake8_pytest_style/rules/patch.rs +++ b/crates/ruff/src/rules/flake8_pytest_style/rules/patch.rs @@ -5,6 +5,39 @@ use ruff_python_ast::visitor; use ruff_python_ast::visitor::Visitor; use ruff_python_ast::{self as ast, Expr, Parameters, Ranged}; +/// ## What it does +/// Checks for mocked calls that use a dummy `lambda` function instead of +/// `return_value`. +/// +/// ## Why is this bad? +/// When patching calls, an explicit `return_value` better conveys the intent +/// than a `lambda` function, assuming the `lambda` does not use the arguments +/// passed to it. +/// +/// `return_value` is also robust to changes in the patched function's +/// signature, and enables additional assertions to verify behavior. For +/// example, `return_value` allows for verification of the number of calls or +/// the arguments passed to the patched function via `assert_called_once_with` +/// and related methods. +/// +/// ## Example +/// ```python +/// def test_foo(mocker): +/// mocker.patch("module.target", lambda x, y: 7) +/// ``` +/// +/// Use instead: +/// ```python +/// def test_foo(mocker): +/// mocker.patch("module.target", return_value=7) +/// +/// # If the lambda makes use of the arguments, no diagnostic is emitted. +/// mocker.patch("module.other_target", lambda x, y: x) +/// ``` +/// +/// ## References +/// - [Python documentation: `unittest.mock.patch`](https://docs.python.org/3/library/unittest.mock.html#unittest.mock.patch) +/// - [`pytest-mock`](https://pypi.org/project/pytest-mock/) #[violation] pub struct PytestPatchWithLambda;