下面是 的片段conftest.py
,它接受命令行参数。
conftest.py
import sys,json,pytest
import logging
def tear_down():
logging.info(" tear-down after all tests")
def pytest_addoption(parser):
parser.addoption("--a1", action="store", default="default_a1",
help="value of a1")
parser.addoption("--b1", action="store", default="default_b1",
help="value of b1")
@pytest.fixture(scope="class",autouse=True)
def set_up(request):
logging.info("set-up before all tests")
a1 = request.config.getoption("--a1")
b1 = request.config.getoption("--b1")
if request.cls.__name__ == 'Test1':
return([a1,b1])
request.addfinalizer(tear_down)
下面的片段是
test.py
每当出现
counter value not equal to 1
ie,c1_1 != 1
(test_2)
那么它应该跳过以下所有情况。可能我可以尝试在每个测试用例中检查这个条件,然后pytest.skip or assert False
. 但这不是一个好主意。我尝试了标记
pytest.mark.skipif
,但在该标记内"can't access variable skip_all" (either "Test1.skip_all")
I would like to skip tests based on certain condition.
如果我的以下方法不正确,请帮助我以正确的方法根据条件跳过测试。
测试.py
import os,sys
import pytest
from other import other_class
a1_1 = b1_1 = c1_1 = ""
class Test1:
skip_all = False
def test_1(self,set_up):
try:
global a1_1,b1_1
a1_1 = set_up[0]
b1_1 = set_up[1]
except Exception as err:
logging.error(err)
Test1.skip_all = True
assert False, err
@pytest.mark.skipif(skip_all == True, reason="Invalid")
def test_2(self):
global c1_1
c1_1 = obj.func1(a1_1,b1_1)
if c1_1 != 1: #rest all test-cases shouldn't execute
logging.error("Invalid")
Test1.skip_all = True
d1_1 = obj.func2(a1_1, c1_1)
if d1_1 == 0:
assert False, "zero"
@pytest.mark.skipif(skip_all == True, reason="Zero")
def test_3(self):
d1_1 = obj.func2(b1_1, c1_1)
if d1_1 == 0:
assert False, "zero"
@pytest.mark.skipif(skip_all == True, reason="Zero")
def test_4(self):
d1_1 = obj.func2(a1_1, c1_1)
if d1_1 == 0:
assert False, "zero"
obj = other_class()
我错过了什么吗?或与固定装置有关?这可能吗 ?谁能让我知道该怎么做?
在下面的示例中,我们如何使用可在 中访问的外部变量marker:skipif
,而不是'sys.platform'
import sys
import pytest
@pytest.mark.skipif(sys.platform != 'darwin', reason="Mac tests")
def test_mac():
assert True
@pytest.mark.skipif(sys.platform != 'linux', reason="Linux tests")
def test_linux():
assert True
@pytest.mark.skipif(sys.platform != 'win32', reason="Windows tests")
def test_windows():
assert True
@pytest.mark.skip(reason="To show we can skip tests without any condition.")
def test_any():
assert True