首页 > 解决方案 > pytest 不承认基类中的 PASSED 依赖项导致派生类中的 SKIPPED 测试

问题描述

我有一个小项目,我在其中使用pytestpytest-dependencytox来开发一些代码的集成测试。到目前为止,我使用了一个基类 ( ),在根目录中有一些常见的测试,并在它旁边BTestClass的每个代码组件的特定测试中实现了一个继承自.test_Component.py fileTestCBTestClass

在那之前一切都很好。现在我想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%]

标签: pythontestingpytesttoxpytest-dependency

解决方案


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,而无需添加额外的标记。

免责声明:我是那个叉子的作者。


推荐阅读