Add PT017 and PT019 docs (#6115)

This commit is contained in:
Harutaka Kawamura 2023-07-28 03:56:34 +09:00 committed by GitHub
parent bb08eea5cc
commit bf987f80f4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 69 additions and 0 deletions

View file

@ -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,

View file

@ -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,