如何根据pytest中的条件跳过测试? [英] How to skip tests based on condition in pytest?
问题描述
下面是 conftest.py
的片段,它接受命令行参数.
conftest.py
import sys,json,pytest导入日志def tear_down():logging.info(所有测试后拆除")def pytest_addoption(解析器):parser.addoption("--a1", action="store", default="default_a1",帮助 =a1 的值")parser.addoption("--b1", action="store", default="default_b1",帮助 =b1 的值")@pytest.fixture(scope=class",autouse=True)定义设置(请求):logging.info(所有测试前的设置")a1 = request.config.getoption("--a1")b1 = request.config.getoption("--b1")如果 request.cls.__name__ == 'Test1':返回([a1,b1])request.addfinalizer(tear_down)
<块引用>
下面的代码片段是 test.py
每当 计数器值不等于 1
即 c1_1 != 1
(test_2)
那么它应该跳过以下所有情况.大概我可以尝试在每个测试用例中检查这个条件,然后pytest.skip 或断言 False
.但这不是个好主意.
我尝试了标记 pytest.mark.skipif
,但在该标记内 无法访问变量 skip_all";(要么是Test1.skip_all")
我想根据特定条件跳过测试.
如果我下面的方法不正确,请帮助我根据条件跳过测试的正确方法.
test.py
import os,sys导入pytest从其他进口other_classa1_1 = b1_1 = c1_1 = "类测试1:skip_all = 假def test_1(self,set_up):尝试:全局 a1_1,b1_1a1_1 = 设置 [0]b1_1 = 设置 [1]除了作为错误的异常:日志记录.错误(错误)Test1.skip_all = True断言错误,错误@pytest.mark.skipif(skip_all == True,原因=无效")def test_2(self):全局 c1_1c1_1 = obj.func1(a1_1,b1_1)if c1_1 != 1: #rest 所有测试用例不应该执行logging.error("无效")Test1.skip_all = Trued1_1 = obj.func2(a1_1, c1_1)如果 d1_1 == 0:断言假,零"@pytest.mark.skipif(skip_all == True, reason=零")def test_3(self):d1_1 = obj.func2(b1_1, c1_1)如果 d1_1 == 0:断言假,零"@pytest.mark.skipif(skip_all == True, reason=零")def test_4(self):d1_1 = obj.func2(a1_1, c1_1)如果 d1_1 == 0:断言假,零"obj = other_class()
我错过了什么吗?或与固定装置有关吗?这可能吗 ?谁能告诉我怎么做?
在下面的例子中,我们如何使用 marker:skipif
中可访问的外部变量,而不是 'sys.platform'
导入系统导入pytest@pytest.mark.skipif(sys.platform != 'darwin', reason="Mac 测试")def test_mac():断言真@pytest.mark.skipif(sys.platform != 'linux', reason="Linux 测试")def test_linux():断言真@pytest.mark.skipif(sys.platform != 'win32', reason="Windows 测试")def test_windows():断言真@pytest.mark.skip(reason=为了表明我们可以在没有任何条件的情况下跳过测试.")def test_any():断言真
首先,一个函数skip_test()
func skip_test():返回 1# 注意引用和函数调用skipmeif = pytest.mark.skipif("skip_test() == 1")
跳过一个函数@skipmeif test_func1
,跳过一个类做@skipmeif class TestSth
其次,对于您的情况下的跳过条件,这是一个可能的解决方案,但不确定是否适合您的目的
func skip_test():obj = SomeClass()c1 = obj.sth()返回 c1
Below is the snippet of conftest.py
, which accepts command line arguments.
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)
Below snippet is of
test.py
Whenever the
counter value not equal to 1
i.e.,c1_1 != 1
(test_2)
then it should skip all the following cases. Probably I can try checking this condition in each and every test-case and thenpytest.skip or assert False
. But this is not great idea.I tried marker
pytest.mark.skipif
, but within that marker"can't access variable skip_all" (either "Test1.skip_all")
I would like to skip tests based on certain condition.
If my below approach is not right, please help me with right approach to skip tests based on condition.
test.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()
Am I missing anything ? or anything to do with fixtures ? Is this possible ? Can anyone please let me know how to do that ?
In below example, how can we use external variable that's accessible in marker:skipif
, instead of '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
first, a function skip_test()
func skip_test():
return 1
# attention the quote and func call
skipmeif = pytest.mark.skipif("skip_test() == 1")
to skipif a function @skipmeif test_func1
, to skipif a class do @skipmeif class TestSth
second, for the skip condition in your case, here is a possible solution but not sure fit your purpose
func skip_test():
obj = SomeClass()
c1 = obj.sth()
return c1
这篇关于如何根据pytest中的条件跳过测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!