How to mark one function not a test for pytest?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Pytest collects tests using naming rules and object discovery. That behavior is useful by default, but sometimes one function in a test module is support code and should not be collected as a test. The clean solution is to make your intent explicit so collection remains predictable.
How Pytest Decides What Is a Test
By default, pytest looks for files named with test_ prefixes or _test suffixes, then collects functions whose names start with test_. If a helper function is accidentally named like a test, pytest will treat it as one and may fail with fixture errors or false negatives.
A quick way to inspect what pytest is collecting is this command.
Use that output before and after changes to confirm the right functions are included.
Mark One Function as Not a Test
Pytest respects an object-level __test__ attribute. Set it to False on the specific function that should be ignored.
Now test_addition is still collected, while test_data_builder is ignored. This is useful when renaming is difficult during migration.
Prefer Explicit Helper Naming
Even though __test__ works, the most maintainable pattern is to avoid test_ prefixes for helpers in the first place. Naming helpers clearly reduces surprise for both developers and tools.
If helper code grows, move it into conftest.py fixtures or a separate support module.
Use Collection Configuration When Needed
In some projects, you may need custom discovery rules. You can restrict function naming conventions in pytest.ini.
This prevents accidental collection of non-standard names and keeps behavior consistent across teams.
Practical Verification Workflow
After applying one of these techniques, run collection checks in automation. A simple CI step can prevent regressions where helper functions accidentally become tests again.
Reviewing collection output is especially useful when refactoring large test suites or moving tests between files.
Keep Helper Code in Fixtures for Better Collection Hygiene
A robust way to avoid accidental collection is to move reusable setup into fixtures. Fixtures are explicit test support objects and are never collected as tests.
This approach scales better than per-function collection flags when the suite grows. It also improves reuse and readability because support code is centralized in fixture files.
Team Conventions That Prevent False Test Discovery
Define one naming guideline for tests, helpers, and fixtures. For example, use test_ only for executable tests, build_ for helper constructors, and fixture names that describe data shape. A short convention document can eliminate repeated collection mistakes in large codebases.
Consider adding a lint rule or pre-commit check that rejects helper functions starting with test_ unless they contain assertions.
Common Pitfalls
- Setting
__test__ = Falseon the module and unintentionally hiding real tests. - Keeping helper names with
test_prefixes, then forgetting why collection is disabled. - Mixing multiple discovery patterns across repositories and confusing contributors.
- Debugging failing collection without using
pytest --collect-onlyfirst.
Summary
- Pytest collects functions based on naming and discovery rules.
- To ignore one function, set
function.__test__ = False. - Prefer helper names that do not start with
test_. - Use collection config and CI checks to keep behavior stable.

