当前位置:   article > 正文

pytest 测试框架学习(10):pytest.param

pytest.param

pytest.param

含义

param: 在 pytest.mark.parametrize 中可以作为一个指定的参数进行调用。
源码:
在这里插入图片描述
values: 需要传入的参数,按照顺序。
**kw: 传入为字典,主要有两个:marks 和 id。
marks: 要应用于此参数集的单个标记或标记列表。
id: 这个参数集中的id属性。

使用

import pytest

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

执行结果:
在这里插入图片描述
上面我们将第2个参数传入的值标记为 xfail ,执行结果就是一个执行通过,一个忽略。

id 使用:

import pytest

@pytest.mark.parametrize("test_input, expected", [
        ("3+5", 8),
        pytest.param("6*9", 54, marks=pytest.mark.xfail, id="this case -- xfail"),
    ])
def test_eval(test_input, expected):
        assert eval(test_input) == expected
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

在这里插入图片描述
id 就是重新给该条测试参数化内容 重新命名打印出来。

说明:本篇参考官网并加入自己些许理解翻译而来,觉得有用,可以点赞和赞赏哦(^ v ^),谢谢支持;如果有不足地方,可留言评论。后续将继续更新。
在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/143688
推荐阅读
相关标签
  

闽ICP备14008679号