简体   繁体   English

测试在pytest中被跳过

[英]test getting skipped in pytest

I am trying to use parametrize for which I want to give testcases which I get from a different function using pytest. 我正在尝试使用参数化,我想给出使用pytest从其他函数获得的测试用例。 I have tried this 我已经试过了

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

But the second test case is getting skipped. 但是第二个测试用例却被跳过了。 It says 它说

test_mod_rarp1.py::test_mod_rarp[test_input10-test_input20-expected10] test_mod_rarp1.py::test_mod_rarp[test_input10-test_input20-expected10]

Why is the test case getting skipped? 为什么测试用例被跳过? I have checked that neither the function nor the input is wrong. 我检查了功能和输入均不错误。 Because the following code is working fine 因为以下代码可以正常工作

@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

I have not put actual inputs here. 我没有在这里实际输入。 Its correct anyway. 无论如何它是正确的。 Also I have config file from which I am taking inputs using configParser. 我也有配置文件,我使用configParser从中获取输入。 test_mod_rarp1.py is the python file name where I am doing this. test_mod_rarp1.py是执行此操作的python文件名。 I basically want to know if we can access variables(test_input in my example) from other functions to use in parametrize if that is causing problem here. 我基本上想知道我们是否可以从其他函数访问变量(在我的示例中为test_input)以在参数化中使用,如果这在这里引起了问题。 If we can't how do I change the scope of the variable? 如果我们不能更改变量的范围?

Parametrization happens at compile time so that is the reason if you want to parametrized on data generated at run time it skips that. 参数化发生在编译时,因此如果要对运行时生成的数据进行参数化,则会跳过该原因。

The ideal way to acheive what you are trying to do is by using fixture parametrization. 实现夹具的理想方法是使用夹具参数化。

Below example should clear things for you and then you could apply the same logic in your case 下面的示例应该为您清除所有内容,然后您可以对案例应用相同的逻辑

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 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]

See test_1 was skipped because parameterization happened before execution of generate_input() but test_2 gets parameterized as required 请参阅test_1被跳过,因为参数化发生在执行generate_input()之前,但是test_2根据需要进行了参数化

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM