问题描述
我有一个小项目,其中我将 pytest 和 pytest-dependency 与 tox 一起使用,以对某些代码进行集成测试。到目前为止,我使用了一个基类(BTestClass
),它在根目录中进行了一些常见测试,并在其旁边的test_Component.py file
中针对每个代码组件的特定测试实现了继承的TestC
类来自BTestClass
。
在那之前,一切正常。现在,我想为另一组组件添加一个BTestClass2
。因此,我添加了另一层继承,但是现在不起作用了, pytest 会验证常见的A
测试,但是会跳过依赖它的测试。我不知道为什么。
这是文件系统布局:
λ tree /F
Folder PATH listing
Volume serial number is F029-7357
C:.
│ B.py
│ requirements-tox.txt
│ tox.ini
│
├───app_C
│ └───tests
│ test_C.py
│
└───common
A.py
common\A.py
import pytest
class ATestClass():
@pytest.mark.dependency(name='test_a')
def test_a(self):
assert True
B.py
import pytest
from common.A import ATestClass
class BTestClass(ATestClass):
@pytest.mark.dependency(name='test_b',depends=['test_a'])
def test_b(self):
assert True
test_C.py
import pytest
import sys
sys.path.append('.')
from B import *
class TestC(BTestClass):
@pytest.mark.dependency(name='test_c',depends=['test_b'])
def test_c(self):
assert True
pytest 输出:
λ tox -- -rs
py38 installed: ...
py38 run-test-pre: PYTHONHASHSEED='367'
py38 run-test: commands[0] | pytest -x -v -rs
=============================================== test session starts ===============================================
platform win32 -- Python 3.8.1,pytest-6.1.1,py-1.9.0,pluggy-0.13.1 -- ...\poc\.tox\py38\scripts\python.exe
cachedir: .tox\py38\.pytest_cache
rootdir: ...\poc
plugins: dependency-0.5.1
collected 3 items
app_C/tests/test_C.py::TestC::test_b SKIPPED [ 33%]
app_C/tests/test_C.py::TestC::test_c SKIPPED [ 66%]
app_C/tests/test_C.py::TestC::test_a PASSED [100%]
============================================= short test summary info =============================================
SKIPPED [1] .tox\py38\lib\site-packages\pytest_dependency.py:103: test_b depends on test_a
SKIPPED [1] .tox\py38\lib\site-packages\pytest_dependency.py:103: test_c depends on test_b
===================================== 1 passed,2 skipped,1 warning in 0.14s =====================================
_____________________________________________________ summary _____________________________________________________
py38: commands succeeded
congratulations :)
您知道为什么test_b
被跳过而不执行吗?
编辑:如果我将BTestClass
独立设置,从图片中删除A
/ ATestClass
,则效果很好。
collected 2 items
app_C/tests/test_C.py::TestC::test_b PASSED [ 50%]
app_C/tests/test_C.py::TestC::test_c PASSED [100%]
解决方法
在function gc(max) {
var arr = [];
for (var i = 0; i < max; i++) {
arr.push(i);
}
return arr.length;
}
//now attempt to force IE11 garbage collector to run
for (var i = 0; i < 10; i++) { // repeat until you have enough:
gc(Math.pow(2,i));
}
中,对另一个测试的依赖项意味着该测试在依赖测试之前运行。如果不是这种情况(在您的示例中,pytest-dependency
在test_b
之前运行,因为test_a
位于子目录中),则仅跳过测试。 test_a
并没有对测试进行任何排序(不幸的是)。
如果您不能通过命名轻松确定测试的运行顺序,则可以使用@Jaroslav Tulach's answer插件将测试置于所需的顺序。您可以这样做:
pytest-dependency
在这种情况下,测试将按class ATestClass:
@pytest.mark.dependency(name='test_a')
@pytest.mark.run(order=0)
def test_a(self):
assert True
...
class BTestClass(ATestClass):
@pytest.mark.dependency(name='test_b',depends=['test_a'])
@pytest.mark.run(order=1)
def test_b(self):
assert True
-test_a
-test_b
的顺序运行,并且所有测试都将运行。
更新:
您还可以使用pytest-ordering,它是test_c
的分支。如果您使用pytest选项pytest-ordering
,它将尝试使用--order-dependencies
创建的依赖项对测试进行重新排序,而无需添加额外的标记。
免责声明:我是那个叉子的作者。