• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

pytest框架测试类使用parametrize参数化

武飞扬头像
redrose2100
帮助1

【原文链接】pytest框架中测试类使用parametrize参数化

介绍将参数化作用域测试类的使用方法,所为作用于测试类,即作用于测试类中的所有方法,那么根据参数化在测试函数上的使用方式可知,这里必须要求测试类中的所有测试方法的形参必须是一致的,具体使用方法如下:

import pytest
@pytest.mark.parametrize("n,expected", [(1, 2), (3, 4),(1,10])
class TestClass:
    def test_simple_case(self, n, expected):
        assert n   1 == expected
    def test_weird_simple_case(self, n, expected):
        assert (n * 1)   1 == expected

执行结果如下,可以看到当前测试类中有两个测试方法,参数化提供了三组数据,相当于是对每个测试方法都运用了三组数据,因此总共有6个测试用例,因为有一组数据对于这两个测试方法都是失败的,因此执行结果显示有4个通过,2个失败

(demo-HCIhX0Hq) E:\demo>pytest -s
=================== test session starts ===================
platform win32 -- Python 3.7.9, pytest-7.2.0, pluggy-1.0.0
rootdir: E:\demo
plugins: assume-2.4.3, rerunfailures-10.2
collected 6 items

test_demo.py ..F..F

======================== FAILURES =========================
____________ TestClass.test_simple_case[1-10] _____________

self = <demo.test_demo.TestClass object at 0x000001F3353A7C08>
n = 1, expected = 10

    def test_simple_case(self, n, expected):
>       assert n   1 == expected
E       assert (1   1) == 10

test_demo.py:5: AssertionError
_________ TestClass.test_weird_simple_case[1-10] __________

self = <demo.test_demo.TestClass object at 0x000001F33539FBC8>
n = 1, expected = 10

    def test_weird_simple_case(self, n, expected):
>       assert (n * 1)   1 == expected
E       assert ((1 * 1)   1) == 10

test_demo.py:7: AssertionError
================= short test summary info =================
FAILED test_demo.py::TestClass::test_simple_case[1-10] - assert (1   1) == 10
FAILED test_demo.py::TestClass::test_weird_simple_case[1-10] - assert ((1 * 1)   1) == 10
=============== 2 failed, 4 passed in 0.08s ===============

(demo-HCIhX0Hq) E:\demo>

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgcfgca
系列文章
更多 icon
同类精品
更多 icon
继续加载