Test files and test classes are meant to verify behavior. When a file looks like a test module, or a class looks like a collected test class, readers expect it to contain executable test cases. If it only contains helpers, fixtures, or lifecycle code, it gives the false impression that the covered behavior is actually tested.
In Python, this can happen in different ways depending on the test framework:
pytest, a file such as test_example.py or example_test.py can define fixtures or helper classes but no
collected test functions or test methods.unittest, a class can inherit from unittest.TestCase but only define setup helpers and no test_
methods.Such files and classes are misleading because they appear to contribute test coverage while actually verifying nothing.
There are scenarios where not having local test cases is acceptable.
Shared helpers or setup code can live in a base class or mixin that is meant to be inherited by real test classes. Those classes are not problematic by themselves as long as the actual derived test classes contain collected tests.
Some test classes inherit test methods from a base class. In that case, the derived class may not declare its own test_ methods
locally, but it still participates in real tests and should not be flagged.
To fix this issue in pytest, it is important that all test files contain at least one test case.
Test cases in pytest are identified by:
test_ in files prefixed with test_ or suffixed with _test.pytest_ in classes prefixed with Test
# test_example.py
class TestSomeClass:
pass # Noncompliant: no test methods
# test_example.py
class TestSomeClass:
def test_some_method_should_return_true(self):
assert some_method() is True
To fix this issue in unittest, it is important that all test classes that inherit from unittest.TestCase contain at least
one test method.
Test methods in unittest must:
test_unittest.TestCase
import unittest
class TestSomeClass(unittest.TestCase):
pass # Noncompliant: no test methods
import unittest
class TestSomeClass(unittest.TestCase):
def test_some_method_should_return_true(self):
self.assertTrue(some_method())