Python 在pytest中跳过测试
我正在尝试使用参数化,我想为其提供使用pytest从不同函数获得的测试用例。 我试过这个Python 在pytest中跳过测试,python,pytest,Python,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 glo
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_输入),以便在参数化中使用,如果这导致了这里的问题。如果我们不能更改变量的范围,我该如何更改?参数化发生在编译时,因此,如果您希望对运行时生成的数据进行参数化,它将跳过该操作 实现您想要做的事情的理想方法是使用夹具参数化 下面的例子应该为您澄清一些事情,然后您可以在您的案例中应用相同的逻辑
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
导入pytest
输入=[]
def生成_输入():
全局输入
输入=[10,20,30]
@pytest.mark.parametize(“a”,输入)
def测试_1(a):
断言a<25
def生成_输入2():
返回[10,20,30]
@fixture(params=generate_input2())
def a(请求):
返回请求.param
def测试_2(a):
断言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]
pytest_suites/test_sample.py::test_1[a0]
**********测试_2[10]**********
pytest_suites/test_sample.py::test_2[10]
收集测试
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
会根据需要进行参数化。参数化发生在编译时,因此,如果要对在运行时生成的数据进行参数化,它会跳过该操作
实现您想要做的事情的理想方法是使用夹具参数化
下面的例子应该为您澄清一些事情,然后您可以在您的案例中应用相同的逻辑
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
导入pytest
输入=[]
def生成_输入():
全局输入
输入=[10,20,30]
@pytest.mark.parametize(“a”,输入)
def测试_1(a):
断言a<25
def生成_输入2():
返回[10,20,30]
@fixture(params=generate_input2())
def a(请求):
返回请求.param
def测试_2(a):
断言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]
pytest_suites/test_sample.py::test_1[a0]
**********测试_2[10]**********
pytest_suites/test_sample.py::test_2[10]
收集测试
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]
由于参数化发生在执行
generate_input()
之前,因此跳过了test_1
,但是test_2
会根据需要进行参数化为什么要为此使用全局变量?如果您想编写一个函数来创建测试用例,请让它返回一个新列表,其中包含测试用例。但是无论如何,你似乎没有调用它,我们也不知道mod_rarp应该做什么,所以没有明显的方法可以帮助。我想我可以在任何地方使用变量,即使在其他模块中,也不需要导入此模块。那好吧。返回所需的变量并与prametize一起使用效果非常好。非常感谢。但我仍然对变量的范围感到好奇。为什么我不能使用带有参数化的test_输入,即使我将其设置为全局的?为什么要使用全局的?如果您想编写一个函数来创建测试用例,请让它返回一个新列表,其中包含测试用例。但是无论如何,你似乎没有调用它,我们也不知道mod_rarp应该做什么,所以没有明显的方法可以帮助。我想我可以在任何地方使用变量,即使在其他模块中,也不需要导入此模块。那好吧。返回所需的变量并与prametize一起使用效果非常好。非常感谢。但我仍然对变量的范围感到好奇。为什么我不能使用带有参数化的test_输入,即使我将其设置为全局?