2

使用 pytest,我正在使用 library 设置依赖项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.

当我尝试使用标记运行测试时,因为它取决于test_a哪个没有标记category,所以它被跳过:

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:我做了一个依赖方案的例子 方案 如果我想在测试中添加一个标记,我必须把这个标记放在所有的分支上,并且“根”会有很多标记。不是做起来复杂,而是繁琐。

4

1 回答 1

1

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

将来自同一类别的所有测试放在一个类中,该类继承自具有test_a. 将每个类放在一个单独的文件中,并将该特定文件传递给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 =======================
于 2021-11-29T09:08:31.273 回答