Skip to content

Generated unittest/xunit fixtures should be private by default (regression since 6.2.0) #37

@rowan-stein

Description

@rowan-stein

User request

Starting v6.2.0, unittest setUpClass fixtures are no longer "private"
Minimal example:

import unittest

class Tests(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        pass

    def test_1(self):
        pass
$ pytest --fixtures
...
unittest_setUpClass_fixture_Tests [class scope] -- .../site-packages/_pytest/unittest.py:145
    ... no docstring available

Expected behavior: generated fixtures should be private (prefixed with underscore) unless -v is used.

Additional context: Also affects xunit setup/teardown fixtures; likely introduced in PRs 7990, 7931, 7929. Proposed fix: prefix generated fixture names with '_'.


Researcher specification

Summary

  • Internal autouse fixtures generated for unittest/xunit setup/teardown began appearing as public in pytest --fixtures since 6.2.0. These should be hidden by default (shown only with -v) by prefixing their names with _.

Scope and locations

  • src/_pytest/unittest.py
    • Function: _make_xunit_fixture (around line ~147)
    • Current pattern: name=f"unittest_{setup_name}fixture{obj.qualname}"
    • Change to: name=f"unittest{setup_name}fixture{obj.qualname}"
  • src/_pytest/python.py
    • Module._inject_setup_module_fixture (around ~531)
      • Change name to: f"xunit_setup_module_fixture{self.obj.name}"
    • Module._inject_setup_function_fixture (around ~560)
      • Change name to: f"xunit_setup_function_fixture{self.obj.name}"
    • Class._inject_setup_class_fixture (around ~812)
      • Change name to: f"xunit_setup_class_fixture{self.obj.qualname}"
    • Class._inject_setup_method_fixture (around ~841)
      • Change name to: f"xunit_setup_method_fixture{self.obj.qualname}"

Rationale

  • The fixture listing code hides fixtures with a leading underscore unless -v is used. Prefixing the generated names restores expected behavior without changing functionality.

Tests to add

  • New tests under testing/python/test_unittest_xunit_fixture_visibility.py to assert that:
    • Default pytest --fixtures does not show these internal fixtures.
    • pytest --fixtures -v does show them with leading underscores.
    • Similarly for --fixtures-per-test default vs -v.
    • Cover unittest setUpClass and xunit setup at module/class/method levels.

Reproduction

  • Before change: create a unittest.TestCase with setUpClass and a module with setup_module; run pytest --fixtures and observe public names like unittest_setUpClass_fixture_* and xunit_setup_module_fixture_*.
  • After change: those names are suppressed by default and visible with -v only.

Notes

  • Behavior change likely introduced in PRs 7990/7931/7929 timeframe (pytest 6.2.0).
  • No behavioral changes to test execution; only fixture display names.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions