pytest-dependency 跳过依赖测试

pytest-dependency skips dependent test

我想 运行 一些依赖于使用 pytest and pytest-dependency 的其他测试成功的测试。

我有这个目录结构:

root/
  tests/
    specs/
      a_test.py
      b_test.py

a_test.py的内容:

import pytest

@pytest.mark.dependency()
def test_a():
    assert 1

@pytest.mark.dependency()
def test_b():
    assert 1

b_test.py的内容:

import pytest


test_dependencies = [
    "tests/specs/a_test.py::test_a",
    "tests/specs/a_test.py::test_b",
]

@pytest.mark.dependency(depends=test_dependencies, scope="session")
def test_c():
    assert 1

要执行测试,我 运行 从根目录执行此命令:python -m pytest --rootdir=tests

我的问题是依赖项(test_a 和 test_b)通过了,但是依赖项测试(test_c)被跳过了

根据pytest-dependency's docs

...

Note that the references in session scope must use the full node id of the dependencies. This node id is composed of the module path, the name of the test class if applicable, and the name of the test, separated by a double colon “::”

...

知道为什么它没有像预期的那样工作吗?

对于未来的读者,在这句话中:

...

Note that the references in session scope must use the full node id of the dependencies. This node id is composed of the module path, the name of the test class if applicable, and the name of the test, separated by a double colon “::”

...

模块路径是相对于--rootdir参数的,所以test_dependencies应该是:

test_dependencies = [
    "specs/a_test.py::test_a",
    "specs/a_test.py::test_b",
]

PS : 经过测试,pytest 似乎按字母顺序加载测试模块(至少在同一目录中),因此您需要确保在依赖测试之前加载依赖项。