python config_error ai_generated true

pytest_dependency: 检测到依赖循环: test_a -> test_b -> test_a

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

ID: python/pytest-dependency-cycle

其他格式: JSON · Markdown 中文 · English
80%修复率
84%置信度
0证据数
2024-09-14首次发现

版本兼容性

版本状态引入弃用备注
0.6.0 active

根因分析

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

English

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

解决方案

  1. 90% 成功率
    @pytest.fixture
    def shared_setup():
        # Common setup
        return resource
    
    def test_a(shared_setup):
        pass
    
    def test_b(shared_setup):
        pass
  2. 88% 成功率
    @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']

无效尝试

常见但无效的做法:

  1. 80% 失败

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

  2. 85% 失败

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