You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

pytest-dependency配置session scope后测试用例意外跳过问题求助

问题原因

你遇到的问题是两个配置错误共同导致的:

  1. 测试运行范围不对
    你实际运行时只指定了执行test_mod_02.py,test_mod_01.py里的所有用例都没有被纳入本次测试会话。pytest-dependency只会统计同一次测试运行中实际执行过的用例结果,被依赖的用例如果根本没运行,插件会直接判定依赖不满足,跳过对应用例。
  2. 依赖的节点ID写法错误
    pytest的测试节点ID不需要加./前缀,正确格式为模块名::用例名,类下的用例格式为模块名::类名::用例名,多余的路径前缀会导致插件无法匹配到对应的依赖用例,同样判定依赖不满足。

修复方案

  • 调整test_mod_02.py里的依赖路径,去掉所有./前缀,修改后对应代码如下:
import pytest

@pytest.mark.dependency()
@pytest.mark.xfail(reason="deliberate fail")
def test_a():
    assert False

@pytest.mark.dependency(
    depends=["test_mod_01.py::test_a", "test_mod_01.py::test_c"],
    scope='session'
)
def test_e():
    pass

@pytest.mark.dependency(
    depends=["test_mod_01.py::test_b", "test_mod_02.py::test_e"],
    scope='session'
)
def test_f():
    pass

@pytest.mark.dependency(
    depends=["test_mod_01.py::TestClass::test_b"],
    scope='session'
)
def test_g():
    pass
  • 运行测试时不要只指定test_mod_02.py,直接在当前工作目录执行pytest命令,同时运行两个模块的所有用例即可。

修复后运行结果和你预期的一致:test_e、test_g执行通过,test_f跳过,test_a xfail。

内容的提问来源于stack exchange,提问作者CarolL

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.04 13:27:02