首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在pytest中跳过测试

在pytest中跳过测试
EN

Stack Overflow用户
提问于 2016-10-21 06:45:18
回答 1查看 2.6K关注 0票数 0

我试图使用参数化,我想给测试案例,我从一个不同的函数得到使用pytest。我试过这个

代码语言:javascript
复制
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

为什么要跳过测试用例?我已经检查了函数和输入都没有错误。因为下面的代码运行良好

代码语言:javascript
复制
@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),如果这在这里造成了问题。如果不能,如何更改变量的范围?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-10-21 07:43:29

参数化发生在编译时,所以如果您想要对在运行时生成的数据进行参数化,它就跳过了这一点。

理想的方法是通过使用夹具参数化来实现你想要做的事情。

下面的示例应该为您清除一些事情,然后您可以在您的情况下应用相同的逻辑。

代码语言:javascript
复制
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

代码语言:javascript
复制
<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会根据需要进行参数化。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40170015

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档