Pytest missing assert inspection¶
This inspection reports pytest tests that never assert anything, and tests that explicitly return a value instead of asserting. Both cases silently pass in pytest, so the test looks green while it verifies nothing.
How to use it¶
Enable the inspection under Settings | BetterPy | Testing (Pytest) | Code Inspections. BetterPy highlights the test name of a test without assertions, and the return <value> statement of a test that returns a value.
What counts as an assertion¶
BetterPy does not just look for a literal assert in the test body. A test is considered to assert something when it:
- contains a Python
assertstatement, - raises
AssertionErrorexplicitly, for exampleraise AssertionError("unexpected value"), - uses
pytest.raises(...),pytest.warns(...), orpytest.deprecated_call(), whether as a context manager or as a direct call such as the legacypytest.raises(ValueError, func, *args)form, - calls
pytest.fail(...),pytest.xfail(...), orpytest.skip(...)as the terminating check, - calls a
unittest.TestCase-style assertion onselforcls, such asself.assertEqual(...),self.assertTrue(...), orself.assertRaises(...), - calls a fluent assertion helper such as
assert_that(...)from assertpy or PyHamcrest, - calls standard
unittest.mockassertion methods, such asmock.assert_called_once_with(...),mock.assert_has_calls(...), orasync_mock.assert_not_awaited(), - calls a project helper function that itself asserts. Helper chains are followed across files in your own code, up to a bounded depth, and recursive helpers are handled without hanging.
All pytest forms are recognised through the same alias-aware resolution, so import pytest, import pytest as pt, from pytest import warns, and from pytest import raises as expect_error all work.
What is deliberately not reported¶
- Functions that pytest would not collect: test-named functions in non-test files, methods in classes that are not test classes, and helper functions or fixtures that happen to start with
test_. BetterPy honourspython_files,python_classes,python_functions, andtestpathsfrom your pytest configuration. - A bare
returnwithout a value, andreturnstatements inside nested functions. - Assertions inside a nested function that is never called — those do not satisfy the inspection, so such a test is still reported.
- Arbitrary methods that merely start with
assert_do not count; the call must be to a supported assertion API. - Third-party mock assertion helpers not listed above are not recognised.
Notes¶
- The inspection reports at weak warning level and only runs on your own code, not on library sources.
- It is gated by the Pytest missing assert inspection setting.