pytest
testing
pytest-mark
test-exclusion
Python

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.

bash
pytest --collect-only -q

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.

python
1# test_math_utils.py
2
3def test_addition():
4    assert 2 + 2 == 4
5
6
7def test_data_builder():
8    # This is a helper that should not be collected as a test.
9    return [1, 2, 3]
10
11
12test_data_builder.__test__ = False

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.

python
1# test_math_utils.py
2
3def build_data():
4    return [1, 2, 3]
5
6
7def test_length():
8    data = build_data()
9    assert len(data) == 3

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.

ini
1# pytest.ini
2[pytest]
3python_files = test_*.py
4python_functions = test_*

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.

bash
pytest --collect-only -q > collected.txt
pytest -q

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.

python
1# conftest.py
2import pytest
3
4
5@pytest.fixture
6def sample_numbers():
7    return [1, 2, 3]
python
1# test_numbers.py
2
3def test_total(sample_numbers):
4    assert sum(sample_numbers) == 6

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__ = False on 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-only first.

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.

Course illustration
Course illustration

All Rights Reserved.