Skip to content

Commit

Permalink
PT008 and PT009 docs
Browse files Browse the repository at this point in the history
  • Loading branch information
harupy committed Aug 10, 2023
1 parent 001aa48 commit a48dafa
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
22 changes: 22 additions & 0 deletions crates/ruff/src/rules/flake8_pytest_style/rules/assertion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
24 changes: 24 additions & 0 deletions crates/ruff/src/rules/flake8_pytest_style/rules/patch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down

0 comments on commit a48dafa

Please sign in to comment.