[英]test getting skipped in pytest
我正在尝试使用参数化,我想给出使用pytest从其他函数获得的测试用例。 我已经试过了
test_input = []
rarp_input1 = ""
rarp_output1 = ""
count =1
def test_first_rarp():
global test_input
config = ConfigParser.ConfigParser()
config.read(sys.argv[2])
global rarp_input1
global rarp_output1
rarp_input1 = config.get('rarp', 'rarp_input1')
rarp_input1 =dpkt.ethernet.Ethernet(rarp_input1)
rarp_input2 = config.get('rarp','rarp_input2')
rarp_output1 = config.getint('rarp','rarp_output1')
rarp_output2 = config.get('rarp','rarp_output2')
dict_input = []
dict_input.append(rarp_input1)
dict_output = []
dict_output.append(rarp_output1)
global count
test_input.append((dict_input[0],count,dict_output[0]))
#assert test_input == [something something,someInt]
@pytest.mark.parametrize("test_input1,test_input2,expected1",test_input)
def test_mod_rarp(test_input1,test_input2,expected1):
global test_input
assert mod_rarp(test_input1,test_input2) == expected1
但是第二个测试用例却被跳过了。 它说
test_mod_rarp1.py::test_mod_rarp[test_input10-test_input20-expected10]
为什么测试用例被跳过? 我检查了功能和输入均不错误。 因为以下代码可以正常工作
@pytest.mark.parametrize("test_input1,test_input2,expected1,[something something,someInt,someInt])
def test_mod_rarp(test_input1,test_input2,expected1):
assert mod_rarp(test_input1,test_input2) == expected1
我没有在这里实际输入。 无论如何它是正确的。 我也有配置文件,我使用configParser从中获取输入。 test_mod_rarp1.py是执行此操作的python文件名。 我基本上想知道我们是否可以从其他函数访问变量(在我的示例中为test_input)以在参数化中使用,如果这在这里引起了问题。 如果我们不能更改变量的范围?
参数化发生在编译时,因此如果要对运行时生成的数据进行参数化,则会跳过该原因。
实现夹具的理想方法是使用夹具参数化。
下面的示例应该为您清除所有内容,然后您可以对案例应用相同的逻辑
import pytest
input = []
def generate_input():
global input
input = [10,20,30]
@pytest.mark.parametrize("a", input)
def test_1(a):
assert a < 25
def generate_input2():
return [10, 20, 30]
@pytest.fixture(params=generate_input2())
def a(request):
return request.param
def test_2(a):
assert a < 25
OP
<SKIPPED:>pytest_suites/test_sample.py::test_1[a0]
********** test_2[10] **********
<EXECUTING:>pytest_suites/test_sample.py::test_2[10]
Collected Tests
TEST::pytest_suites/test_sample.py::test_1[a0]
TEST::pytest_suites/test_sample.py::test_2[10]
TEST::pytest_suites/test_sample.py::test_2[20]
TEST::pytest_suites/test_sample.py::test_2[30]
请参阅test_1
被跳过,因为参数化发生在执行generate_input()
之前,但是test_2
根据需要进行了参数化
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.