pytest - pytest 依赖项跳过参数化测试
问题描述
我正在使用 pytest 编写测试。我有两个测试,一个取决于另一个,我为此使用 pytest-dependency==0.5.1 。当我有两个相互依赖但都参数化的测试时,会发生一些奇怪的事情——即使独立测试成功,也会跳过依赖测试。这是我的代码:
import pytest
@pytest.mark.parametrize('par1', ['val1', 'val2', 'val3'])
@pytest.mark.dependency()
def test_a(par1):
print('hi from test a')
assert 1 == 1
@pytest.mark.parametrize('par2', ['val21', 'val22', 'val23'])
@pytest.mark.dependency(depends=["test_a"])
def test_b(par2):
print('hi from test c')
当我运行 pytest 时,我得到:
pytest --log-cli-level=INFO
================================================================================================= test session starts ==================================================================================================
platform linux -- Python 3.7.9, pytest-6.1.2, py-1.9.0, pluggy-0.13.1
rootdir: /home/username/dev/tests/test
plugins: dependency-0.5.1, mock-3.1.1, anyio-2.0.2, dash-1.16.1, celery-4.4.7, allure-pytest-2.8.21
collected 6 items
test_something.py::test_a[val1] PASSED [ 16%]
test_something.py::test_a[val2] PASSED [ 33%]
test_something.py::test_a[val3] PASSED [ 50%]
test_something.py::test_b[val21] SKIPPED [ 66%]
test_something.py::test_b[val22] SKIPPED [ 83%]
test_something.py::test_b[val23] SKIPPED [100%]
============================================================================================= 3 passed, 3 skipped in 0.05s =============================================================================================
如果我取消参数化,一切都很好:
pytest --log-cli-level=INFO
================================================================================================= test session starts ==================================================================================================
platform linux -- Python 3.7.9, pytest-6.1.2, py-1.9.0, pluggy-0.13.1
rootdir: /home/username/dev/tests/test
plugins: dependency-0.5.1, mock-3.1.1, anyio-2.0.2, dash-1.16.1, celery-4.4.7, allure-pytest-2.8.21
collected 2 items
test_something.py::test_a PASSED [ 50%]
test_something.py::test_b PASSED [100%]
================================================================================================== 2 passed in 0.01s ===================================================================================================
为什么会发生,我该如何解决?
解决方案
The problem is that the name of the tests contains the test parameter, e.g. test_a[val1]
etc., and pytest-dependency
cannot find a test named test_a
. To solve this you can just add the name to the dependency marker - in this case pytest-dependency
ignores the real test name and uses this one:
import pytest
@pytest.mark.parametrize('par1', ['val1', 'val2', 'val3'])
@pytest.mark.dependency(name='test_a')
def test_a(par1):
print('hi from test a')
assert 1 == 1
@pytest.mark.parametrize('par2', ['val21', 'val22', 'val23'])
@pytest.mark.dependency(depends=['test_a'])
def test_b(par2):
print('hi from test b')
Now the test is executed if all of the test_a
tests succeed.
推荐阅读
- oracle - 完全下载 APEX 站点
- mysql - Micronaut:无法连接 MySQL 数据库并使用 Hibernate 使用表创建模式
- testng - 使用 TestNg 或 Cucumber 对 RestAssured 进行多线程执行
- javascript - Javascript解析excel文件并创建笛卡尔积
- python - Python3:在另一个线程打开输入时在新行上打印
- python - python tkinter错误“AttributeError:'NoneType'对象没有属性'插入'”
- c# - 如何使 ComboBox 向上而不是向下打开列表?
- spring - 谨慎模式的 Logback 会创建损坏的日志文件
- docker - docker 容器重启是如何工作的?
- javascript - 在循环对象时如何通过 KEY 获取每个值?