python config_error ai_generated true

pytest_dependency: Cycle detected in dependencies: test_a -> test_b -> test_a

ID: python/pytest-dependency-cycle

Also available as: JSON · Markdown · 中文
80%Fix Rate
84%Confidence
0Evidence
2024-09-14First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
0.6.0 active

Root Cause

Two or more tests are marked with @pytest.mark.dependency such that they depend on each other, creating a circular dependency that cannot be resolved.

generic

中文

两个或多个测试使用 @pytest.mark.dependency 标记,使得它们相互依赖,形成无法解决的循环依赖。

Workarounds

  1. 90% success
    @pytest.fixture
    def shared_setup():
        # Common setup
        return resource
    
    def test_a(shared_setup):
        pass
    
    def test_b(shared_setup):
        pass
  2. 88% success
    @pytest.fixture(scope='session')
    def shared_state():
        return {}
    
    @pytest.mark.dependency()
    def test_a(shared_state):
        shared_state['a'] = True
    
    @pytest.mark.dependency(depends=['test_a'])
    def test_b(shared_state):
        assert shared_state['a']

Dead Ends

Common approaches that don't work:

  1. 80% fail

    This breaks the intended test order and may cause other tests to fail or run out of order.

  2. 85% fail

    pytest-ordering does not support dependency tracking and may still run tests in an invalid order.