pytest 参数化

pytest文档9-参数化parametrize

 

前言

pytest.mark.parametrize装饰器可以实现测试用例参数化。

parametrizing

1.这里是一个实现检查一定的输入和期望输出测试功能的典型例子

# 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_canshu1.py"])

运行结果


================================== FAILURES ===================================
_____________________________ test_eval[6 * 9-42] _____________________________

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       AssertionError: assert 54 == 42
E        +  where 54 = eval('6 * 9')

test_canshu1.py:11: AssertionError
===================== 1 failed, 2 passed in 1.98 seconds ======================

在这个例子中设计的,只有一条输入/输出值的简单测试功能。和往常一样

函数的参数,你可以在运行结果看到在输入和输出值

2.它也可以标记单个测试实例在参数化,例如使用内置的mark.xfail

# content of test_expectation.py
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



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

运行结果:

test_canshu1.py -------开始用例------
.-------开始用例------
.-------开始用例------
x

===================== 2 passed, 1 xfailed in 1.84 seconds =====================

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

参数组合

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

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_canshu1.py"])

运行结果


test_canshu1.py 测试数据组合:x->0, y->2
.测试数据组合:x->1, y->2
.测试数据组合:x->0, y->3
.测试数据组合:x->1, y->3
.

========================== 4 passed in 1.75 seconds ===========================

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

posted @   bonelee  阅读(662)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· DeepSeek 开源周回顾「GitHub 热点速览」
历史上的今天:
2017-12-28 ES shrink ——一般是结合rollover一起使用的,一开始没有看懂官方shrink文档,当看了这个之后就明白了
2017-12-28 高效管理 Elasticsearch 中基于时间的索引——本质是在利用滚动模式做数据的冷热分离,热索引可以用ssd
2017-12-28 elasticsearch indices.recovery 流程分析(索引的_open操作也会触发recovery)——主分片recovery主要是从translog里恢复之前未写完的index,副分片recovery主要是从主分片copy segment和translog来进行恢复
2017-12-28 JS垃圾回收——和其他语言一样,JavaScript 的 GC 策略也无法避免一个问题:GC 时,停止响应其他操作,这是为了安全考虑
2016-12-28 怎样打造一个分布式数据库——rocksDB, raft, mvcc,本质上是为了解决跨数据中心的复制
点击右上角即可分享
微信分享提示