运行 使用 pytest 进行依赖测试

run dependant tests with pytest

对于 pytest,我正在使用库 pytest-dependency 设置依赖项。我还为这些测试添加了标记。这是一个 ECM :

# test_test.py
import pytest

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

@pytest.mark.category
@pytest.mark.dependency(depends=['test_a'])
def test_b():
   assert True

pytest.ini文件设置标记:

; pytest.ini
[pytest]
markers =
    category: category of tests.

当我尝试 运行 带有标记的测试时,因为它依赖于没有标记 categorytest_a,它被跳过了:

user@pc → [~/Documents/test] $ pytest -vv -k category
============================================== test session starts ===============================================
platform darwin -- Python 3.9.8, pytest-6.2.5, py-1.11.0, pluggy-1.0.0 -- /usr/local/opt/python@3.9/bin/python3.9
cachedir: .pytest_cache
rootdir: /Users/saigre/Documents/test, configfile: pytest.ini
plugins: dependency-0.5.1
collected 2 items / 1 deselected / 1 selected

test_test.py::test_b SKIPPED (test_b depends on test_a)                                                    [100%]

======================================== 1 skipped, 1 deselected in 0.05s ========================================

有没有办法 强制 test_a 的 运行 因为依赖关系。 一个解决方案是将标记添加到第一个测试中,但对于我正在处理的情况来说这会很复杂...

为@MrBean Bremen 编辑: 我做了一个依赖方案的例子 如果我想在测试中添加一个标记,我将不得不把这个标记放在所有的分支上,而“根”会有很多标记。不是做起来复杂,而是繁琐

你可以试试这个(我已经实现了类似的东西):

将同一类别的所有测试放在 class 中,它继承自具有 test_a 的基础 class。将每个 class 放在一个单独的文件中,然后将该特定文件传递给 pytest。这样,您也可以摆脱标记。

考虑一个伪示例:

# tests/test_a.py #
import pytest

class TestA:

    @pytest.mark.dependency(name="TEST-A")
    def test_a(self):
        print("TEST-A was called")
        assert False


# tests/test_b.py #
import pytest
from tests.test_a import TestA

class TestB(TestA):

    def test_b1(self):
        print("TEST-B1 was called")
        assert True

    @pytest.mark.dependency(depends=["TEST-A"])
    def test_b2(self):
        print("TEST-B2 was called")
        assert True

现在,在 CLI 上,只需执行以下操作: $ pytest tests/test_b.py

tests/test_b.py::TestB::test_a <- tests/test_a.py FAILED                      [ 33%]
tests/test_b.py::TestB::test_b1 PASSED                                        [ 66%]
tests/test_b.py::TestB::test_b2 SKIPPED (test_b2 depends on TEST-A)           [100%]

===================================== FAILURES ======================================
___________________________________ TestB.test_a ____________________________________

self = <tests.test_b.TestB object at 0x7fc21886db20>

    @pytest.mark.dependency(name="TEST-A")
    def test_a(self):
        print("TEST-A was called")
>       assert False
E       assert False

tests/test_a.py:8: AssertionError
------------------------------- Captured stdout call --------------------------------
TEST-A was called
====================================== PASSES =======================================
___________________________________ TestB.test_b1 ___________________________________
------------------------------- Captured stdout call --------------------------------
TEST-B1 was called
============================== short test summary info ==============================
PASSED tests/test_b.py::TestB::test_b1
SKIPPED [1] ...: test_b2 depends on TEST-A
FAILED tests/test_b.py::TestB::test_a - assert False
====================== 1 failed, 1 passed, 1 skipped in 0.19s =======================