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..a596c7c333397 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,28 @@ impl Violation for PytestAssertAlwaysFalse { } } +/// ## What it does +/// Checks for `unittest`'s assertion methods. +/// +/// ## Why is this bad? +/// To make use of `pytest`'s assertion rewriting, a regular `assert` statement is +/// preferred over `unittest`'s assertion methods. +/// +/// ## Example +/// 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 +/// ``` +/// +/// - [`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..e91e8657abdc7 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,30 @@ 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 monkey patching calls that use `lambda` as the new value. +/// +/// ## Why is this bad? +/// `return_value` conveys the intent more clearly and allows using methods for +/// verifying the number of calls or the arguments passed to the patched function +/// (e.g., `assert_called_once_with`). +/// +/// ## Example +/// 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 lambda parameters are used, it's not a violation +/// mocker.patch('module.other_target', lambda x, y: x) +/// ``` +/// +/// ## References +/// - [`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;