我试图使用参数化,我想给测试案例,我从一个不同的函数得到使用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_rarptest_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),如果这在这里造成了问题。如果不能,如何更改变量的范围?
发布于 2016-10-21 07:43:29
参数化发生在编译时,所以如果您想要对在运行时生成的数据进行参数化,它就跳过了这一点。
理想的方法是通过使用夹具参数化来实现你想要做的事情。
下面的示例应该为您清除一些事情,然后您可以在您的情况下应用相同的逻辑。
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 < 25OP
<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会根据需要进行参数化。
https://stackoverflow.com/questions/40170015
复制相似问题