python - pytest 不承认基类中的 PASSED 依赖项导致派生类中的 SKIPPED 测试
问题描述
我有一个小项目,我在其中使用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%]
解决方案
在pytest-dependency
中,对另一个测试的依赖意味着该测试在依赖测试之前运行。如果不是这种情况(在您的示例test_b
中运行 before test_a
,因为test_a
位于子目录中),则只是跳过测试。pytest-dependency
不会对测试进行任何重新排序(不幸的是)。
如果您无法通过命名轻松确定运行测试的顺序,您可以使用pytest-ordering插件将测试带入所需的顺序。在你的情况下,你可以这样做:
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
-的顺序运行test_c
,所有测试都会运行。
更新:
您还可以使用pytest-order,它是pytest-ordering
. 如果您使用 pytest 选项--order-dependencies
,它将尝试使用由 创建的依赖项重新排序测试pytest-dependencies
,而无需添加额外的标记。
免责声明:我是那个叉子的作者。
推荐阅读
- c# - 在一个 for 循环中打印不同的数组
- sql - 如何对每个月 SQL 的列中的最大值进行分组
- c# - C# Sockets 代理使用
- mysql - 无法在搜索过滤器查询中找到想要的结果
- angular - 如何在 Angular / Spring Boot 中统一验证
- python - Python使用openpyxl操作Excel
- ms-access - 子窗体中的 MS Access 2016 图表
- php - PHP:如果数组键存在,则选择下拉选项
- ms-access - Combobox.requery 在另一个表单上的子表单中不起作用
- r - rbind 中的错误(meslek1,asd):在 r 中找不到对象“asd”