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 a mock assertion method such as
assert_called_once_with(...)orassert_not_awaited()on a value BetterPy recognises as aMockorAsyncMock, - or 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.
assert_-prefixed methods on ordinary objects, onMock(spec=...)where the spec class defines the method, or onMock(unsafe=True); only real mock assertion methods count.unittest-style assertions are only accepted in their camelCase form (assertEqual, notassert_equal) and only when called onselforcls.- Calls that cannot be resolved, for example helpers from third-party libraries — the test is reported, because BetterPy cannot see an assertion.
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.