Why is this an issue?

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:

Such files and classes are misleading because they appear to contribute test coverage while actually verifying nothing.

Exceptions

There are scenarios where not having local test cases is acceptable.

Base classes used only for shared test logic

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.

Derived classes that inherit 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.

How to fix it in pytest

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:

Code examples

Noncompliant code example

# test_example.py
class TestSomeClass:
    pass  # Noncompliant: no test methods

Compliant solution

# test_example.py
class TestSomeClass:
    def test_some_method_should_return_true(self):
        assert some_method() is True

How to fix it in unittest

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:

Code examples

Noncompliant code example

import unittest

class TestSomeClass(unittest.TestCase):
    pass  # Noncompliant: no test methods

Compliant solution

import unittest

class TestSomeClass(unittest.TestCase):
    def test_some_method_should_return_true(self):
        self.assertTrue(some_method())

Resources

Documentation