软件测试|Pytest必会技巧(三)

举报
霍格沃兹测试开发 发表于 2023/02/08 18:35:12 2023/02/08
【摘要】 Pytest参数化——pytest.mark.parametrize parametrizing首先来看一个实现检查一定的输入和期望输出测试功能的典型例子# content of test_expectation.py# coding:utf-8import pytest@pytest.mark.parametrize("test_input,expected", ...

Pytest参数化——pytest.mark.parametrize

parametrizing

首先来看一个实现检查一定的输入和期望输出测试功能的典型例子

# content of test_expectation.py

# coding:utf-8

import pytest
@pytest.mark.parametrize("test_input,expected",
                         [ ("3+5", 8),
                           ("2+4", 6),
                           ("6 * 9", 42),
                         ])
def test_eval(test_input, expected):
    assert eval(test_input) == expected

if __name__ == "__main__":
    pytest.main(["-s", "test_demo.py"])

运行结果

============================= test session starts =============================
collecting ... collected 3 items

test_demo.py::test_eval[3+5-8] 
test_demo.py::test_eval[2+4-6] 
test_demo.py::test_eval[6 * 9-42] PASSED                                    [ 33%]PASSED                                    [ 66%]FAILED                                 [100%]
test_demo.py:5 (test_eval[6 * 9-42])
54 != 42

Expected :42
Actual   :54
<Click to see difference>

test_input = '6 * 9', expected = 42

    @pytest.mark.parametrize("test_input,expected",
                             [ ("3+5", 8),
                               ("2+4", 6),
                               ("6 * 9", 42),
                             ])
    def test_eval(test_input, expected):
>       assert eval(test_input) == expected
E       assert 54 == 42

test_demo.py:12: AssertionError




========================= 1 failed, 2 passed in 0.04s =========================

在这个例子中设计的,只有一条输入/输出值的简单测试功能。和往常一样,函数的参数,你可以在运行结果看到在输入和输出值。

标记单个测试实例参数化

import pytest
@pytest.mark.parametrize("test_input,expected", [
                        ("3+5", 8),
                        ("2+4", 6),
                        pytest.param("6 * 9", 42, marks=pytest.mark.xfail),
                        ])
def test_eval(test_input, expected):
    print("-------开始用例------")
    assert eval(test_input) == expected

运行结果:

============================= test session starts =============================
collecting ... collected 3 items

test_demo.py::test_eval[3+5-8] 
test_demo.py::test_eval[2+4-6] 
test_demo.py::test_eval[6 * 9-42] 

======================== 2 passed, 1 xfailed in 0.06s =========================

Process finished with exit code 0
PASSED                                    [ 33%]-------开始用例------
PASSED                                    [ 66%]-------开始用例------
XFAIL                                  [100%]-------开始用例------

test_input = '6 * 9', expected = 42

    @pytest.mark.parametrize("test_input,expected", [
                            ("3+5", 8),
                            ("2+4", 6),
                            pytest.param("6 * 9", 42, marks=pytest.mark.xfail),
                            ])
    def test_eval(test_input, expected):
        print("-------开始用例------")
>       assert eval(test_input) == expected
E       assert 54 == 42

test_demo.py:10: AssertionError

标记为失败的用例,预期结果是失败,实际运行也是失败,显示xfailed

参数组合

若要获得多个参数化参数的所有组合,可以堆叠参数化装饰器

import pytest
@pytest.mark.parametrize("x", [0, 1])
@pytest.mark.parametrize("y", [2, 3])
def test_foo(x, y):
    print("测试数据组合:x->%s, y->%s" % (x, y))


if __name__ == "__main__":
    pytest.main(["-s", "test_demo.py"])

运行结果:

============================= test session starts =============================
collecting ... collected 4 items

test_demo.py::test_foo[2-0] PASSED                                       [ 25%]测试数据组合:x->0, y->2

test_demo.py::test_foo[2-1] PASSED                                       [ 50%]测试数据组合:x->1, y->2

test_demo.py::test_foo[3-0] PASSED                                       [ 75%]测试数据组合:x->0, y->3

test_demo.py::test_foo[3-1] PASSED                                       [100%]测试数据组合:x->1, y->3


============================== 4 passed in 0.03s ==============================

这将运行测试,参数设置为x=0/y=2,x=1/y=2,x=0/y=3,x=1/y=3组合参数。

【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。