【发布时间】:2020-07-14 19:17:00
【问题描述】:
下面的 pytest 代码可以正常工作,它会增加 value。
import pytest
pytest.value = 1
def test_1():
pytest.value +=1
print(pytest.value)
def test_2():
pytest.value +=1
print(pytest.value)
def test_3():
pytest.value +=1
print(pytest.value)
输出:
Prints
2
3
4
我不想执行test_2,当value=2
pytest.dependency() 可以吗?如果是,我如何在pytest.dependency 中使用变量value?
如果不是pytest.dependency,还有其他选择吗?
或者有什么更好的方法来处理这种情况?
import pytest
pytest.value = 1
def test_1():
pytest.value +=1
print(pytest.value)
@pytest.dependency(value=2) # or @pytest.dependency(pytest.value=2)
def test_2():
pytest.value +=1
print(pytest.value)
def test_3():
pytest.value +=1
print(pytest.value)
你能指导我吗?这可以做到吗? 这可能吗?
【问题讨论】:
-
这可以实现吗?有人可以在这里指导吗?
-
我没用过
pytest.dependency,但是从我在文档中看到的,没有这样的选项——至少我找不到。你真正想要实现的是什么? -
@MrBean:感谢您的回复。场景是每当'value' = 2时跳过所有测试用例。此“值”会根据测试操作不断动态变化。
-
@MrBeanBremen :能想到什么吗?
标签: python-3.x pytest fixtures