【pytest】 参数化@pytest.mark.parametrize

2023-09-17 23:40:25

1.创建   test_parametrize.py

通过

@pytest.mark.parametrize 方法设置参数
import pytest

import math

#pytest参数化
@pytest.mark.parametrize(
   "base,exponent,expected",  # 参数变量名称
    # 每个元组都是一条测试用例测试数据
    [
        (2,2,4),
        (3,3,9),
        (1,9,1),
        (0,9,0)
    ],
    ids=['case1','case2','case3','case4']  # 默认为None 用来定义测试用例的名称
)
def test_pow(base,exponent,expected):
    print(base,exponent,expected)
    assert math.pow(base,exponent)==expected

2.运行结果:pytest -s test_parametrize.py

PS E:\data\web测试\Selenium3自动化测试实战——基于Python语言\mycode\pytest_example> pytest -v test_parametrize.py
======================================================================== test session starts ========================================================================
platform win32 -- Python 3.10.9, pytest-7.1.2, pluggy-1.0.0 -- D:\software\python3\anconda3\python.exe
cachedir: .pytest_cache
rootdir: E:\data\web测试\Selenium3自动化测试实战——基于Python语言\mycode\pytest_example
plugins: anyio-3.5.0
collected 0 items                                                                                                                                                    

======================================================================= no tests ran in 0.03s =======================================================================
ERROR: file or directory not found: test_parametrize.py

PS E:\data\web测试\Selenium3自动化测试实战——基于Python语言\mycode\pytest_example> pytest -s test_parametrize.py
======================================================================== test session starts ========================================================================
platform win32 -- Python 3.10.9, pytest-7.1.2, pluggy-1.0.0
rootdir: E:\data\web测试\Selenium3自动化测试实战——基于Python语言\mycode\pytest_example
plugins: anyio-3.5.0
collected 0 items                                                                                                                                                    

======================================================================= no tests ran in 0.04s =======================================================================
ERROR: file or directory not found: test_parametrize.py

PS E:\data\web测试\Selenium3自动化测试实战——基于Python语言\mycode\pytest_example> cd parametrize
PS E:\data\web测试\Selenium3自动化测试实战——基于Python语言\mycode\pytest_example\parametrize> pytest -s test_parametrize.py
======================================================================== test session starts ========================================================================
platform win32 -- Python 3.10.9, pytest-7.1.2, pluggy-1.0.0
rootdir: E:\data\web测试\Selenium3自动化测试实战——基于Python语言\mycode\pytest_example\parametrize
plugins: anyio-3.5.0
collected 4 items                                                                                                                                                    

test_parametrize.py .F..

============================================================================= FAILURES ==============================================================================
__________________________________________________________________________ test_pow[case2] __________________________________________________________________________

base = 3, exponent = 3, expected = 9

    @pytest.mark.parametrize(
       "base,exponent,expected",  # 参数变量名称
        # 每个元组都是一条测试用例测试数据
        [
            (2,2,4),
            (3,3,9),
            (1,9,1),
            (0,9,0)
        ],
        ids=['case1','case2','case3','case4']  # 默认为None 用来定义测试用例的名称
    )
    def test_pow(base,exponent,expected):
>       assert math.pow(base,exponent)==expected
E       assert 27.0 == 9
E        +  where 27.0 = <built-in function pow>(3, 3)
E        +    where <built-in function pow> = math.pow

test_parametrize.py:18: AssertionError
====================================================================== short test summary info ======================================================================
FAILED test_parametrize.py::test_pow[case2] - assert 27.0 == 9
==================================================================== 1 failed, 3 passed in 1.66s ====================================================================
PS E:\data\web测试\Selenium3自动化测试实战——基于Python语言\mycode\pytest_example\parametrize> pytest -s test_parametrize.py
======================================================================== test session starts ========================================================================
platform win32 -- Python 3.10.9, pytest-7.1.2, pluggy-1.0.0
rootdir: E:\data\web测试\Selenium3自动化测试实战——基于Python语言\mycode\pytest_example\parametrize
plugins: anyio-3.5.0
collected 4 items                                                                                                                                                    

test_parametrize.py 2 2 4
.3 3 9
F1 9 1
.0 9 0
.

============================================================================= FAILURES ==============================================================================
__________________________________________________________________________ test_pow[case2] __________________________________________________________________________

base = 3, exponent = 3, expected = 9

    @pytest.mark.parametrize(
       "base,exponent,expected",  # 参数变量名称
        # 每个元组都是一条测试用例测试数据
        [
            (2,2,4),
            (3,3,9),
            (1,9,1),
            (0,9,0)
        ],
        ids=['case1','case2','case3','case4']  # 默认为None 用来定义测试用例的名称
    )
    def test_pow(base,exponent,expected):
        print(base,exponent,expected)
>       assert math.pow(base,exponent)==expected
E       assert 27.0 == 9
E        +  where 27.0 = <built-in function pow>(3, 3)
E        +    where <built-in function pow> = math.pow

test_parametrize.py:19: AssertionError
====================================================================== short test summary info ======================================================================
FAILED test_parametrize.py::test_pow[case2] - assert 27.0 == 9
==================================================================== 1 failed, 3 passed in 0.44s ====================================================================
PS E:\data\web测试\Selenium3自动化测试实战——基于Python语言\mycode\pytest_example\parametrize>

  运行  pytest -v test_parametrize.py

======================================================================== test session starts ========================================================================
platform win32 -- Python 3.10.9, pytest-7.1.2, pluggy-1.0.0 -- D:\software\python3\anconda3\python.exe
cachedir: .pytest_cache
rootdir: E:\data\web测试\Selenium3自动化测试实战——基于Python语言\mycode\pytest_example\parametrize
plugins: anyio-3.5.0
collected 4 items                                                                                                                                                    

test_parametrize.py::test_pow[case1] PASSED                                                                                                                    [ 25%]
test_parametrize.py::test_pow[case2] FAILED                                                                                                                    [ 50%]
test_parametrize.py::test_pow[case3] PASSED                                                                                                                    [ 75%]
test_parametrize.py::test_pow[case4] PASSED                                                                                                                    [100%]

============================================================================= FAILURES ==============================================================================
__________________________________________________________________________ test_pow[case2] __________________________________________________________________________

base = 3, exponent = 3, expected = 9

    @pytest.mark.parametrize(
       "base,exponent,expected",  # 参数变量名称
        # 每个元组都是一条测试用例测试数据
        [
            (2,2,4),
            (3,3,9),
            (1,9,1),
            (0,9,0)
        ],
        ids=['case1','case2','case3','case4']  # 默认为None 用来定义测试用例的名称
    )
    def test_pow(base,exponent,expected):
        print(base,exponent,expected)
>       assert math.pow(base,exponent)==expected
E       assert 27.0 == 9
E        +  where 27.0 = <built-in function pow>(3, 3)
E        +    where <built-in function pow> = math.pow

test_parametrize.py:19: AssertionError
----------------------------------------------------------------------- Captured stdout call ------------------------------------------------------------------------
3 3 9
====================================================================== short test summary info ======================================================================
FAILED test_parametrize.py::test_pow[case2] - assert 27.0 == 9
==================================================================== 1 failed, 3 passed in 1.79s ====================================================================
PS E:\data\web测试\Selenium3自动化测试实战——基于Python语言\mycode\pytest_example\parametrize>

3.运行参数控制


pytest -s test_parametrize.py     其中 -s 用于关闭捕捉 

pytest -v test_parametrize.py   增加测试用例冗长

pytest --help  查看帮助

pytest  -q  test_parametrize.py 减少测试冗长

pytest -x   test_parametrize.py 如果出现一条测试用例失败,就停止

pytest  ./test_dir    运行测试目录 

pytest      指定特定类或是方法

 pytest   ./fixture/test_fixtures_02.py::TestMultiply::test_numbers_5_6

# 功能函数
def multiply(a, b):
    return a * b


class TestMultiply:
    # =====fixtures========
    @classmethod
    def setup_class(cls):
        print("setup_class=========>")

    @classmethod
    def teardown_class(cls):
        print("teardown_class=========>")

    def setup_method(self, method):
        print("setup_method----->>")

    def teardown_method(self, method):
        print("teardown_method-->>")

    def setup(self):
        print("setup----->")

    def teardown(self):
        print("teardown-->")

    # =====测试用例========
    def test_numbers_5_6(self):
        print('test_numbers_5_6')
        assert multiply(5, 6) == 30

    def test_strings_b_2(self):
        print('test_strings_b_2')
        assert multiply('b', 2) == 'bb'
 pytest   ./fixture/test_fixtures_02.py::TestMultiply::test_numbers_5_6
======================================================================== test session starts ========================================================================
platform win32 -- Python 3.10.9, pytest-7.1.2, pluggy-1.0.0
rootdir: E:\data\web测试\Selenium3自动化测试实战——基于Python语言\mycode\pytest_example
plugins: anyio-3.5.0
collected 1 item                                                                                                                                                     

fixture\test_fixtures_02.py .                                                                                                                                  [100%]

通过 main 方法运行 

 

import pytest

if __name__=='__main__':
    #pytest.main(['-s','./fixture'])
    pytest.main(['-v', './fixture'])

 


============================== 6 passed in 0.06s ==============================
============================= test session starts =============================
platform win32 -- Python 3.10.9, pytest-7.1.2, pluggy-1.0.0 -- D:\software\python3\anconda3\python.exe
cachedir: .pytest_cache
rootdir: E:\data\web测试\Selenium3自动化测试实战——基于Python语言\mycode\pytest_example
plugins: anyio-3.5.0
collecting ... collected 6 items

fixture/test_fixtures_01.py::test_mult_3_4 PASSED                        [ 16%]
fixture/test_fixtures_01.py::test_mult_a_4 PASSED                        [ 33%]
fixture/test_fixtures_011.py::test_multiply_3_4 PASSED                   [ 50%]
fixture/test_fixtures_011.py::test_multiply_a_3 PASSED                   [ 66%]
fixture/test_fixtures_02.py::TestMultiply::test_numbers_5_6 PASSED       [ 83%]
fixture/test_fixtures_02.py::TestMultiply::test_strings_b_2 PASSED       [100%]

============================== 6 passed in 0.06s ==============================

更多推荐

0 杂项知识

文章目录加密算法对称加密非对称加密散列加密sha-512与md5算法的对比加密算法参照:链接一般将加密算法分为三种:对称加密,非对称加密,散列加密对称加密对称加密就是只存在一把钥匙,这把钥匙可以用来加密文件,也可以用来解密文件(个人理解)常用于需要经常性沟通的加密,即给出钥匙的对方与己方需要有经常性的沟通。非对称加密非

【FAQ】安防监控系统/视频云存储EasyCVR平台安全检查Proxy出现sql injection的漏洞,该如何修改?

安防视频监控/视频集中存储/云存储/磁盘阵列EasyCVR平台可拓展性强、视频能力灵活、部署轻快,可支持的主流标准协议有国标GB28181、RTSP/Onvif、RTMP等,以及支持厂家私有协议与SDK接入,包括海康Ehome、海大宇等设备的SDK等。最近有用户反馈,在使用视频监控系统EasyCVR平台安全扫描时,发现

企业工程项目管理系统源码(三控:进度组织、质量安全、预算资金成本、二平台:招采、设计管理)

工程项目管理软件(工程项目管理系统)对建设工程项目管理组织建设、项目策划决策、规划设计、施工建设到竣工交付、总结评估、运维运营,全过程、全方位的对项目进行综合管理工程项目各模块及其功能点清单一、系统管理1、数据字典:实现对数据字典标签的增删改查操作2、编码管理:实现对系统编码的增删改查操作3、用户管理:管理和查看用户角

JVM调优笔记

双亲委派机制app---->ext----->bootstrap保证系统的核心库不被修改沙箱安全机制限制系统资源访问,将java代码限制在虚拟机特定的运行范围中基本组件字节码校验器确保java类文件遵循java规范,帮助java程序实现内存保护类加载器nativeJava的作用范围达不到了,需要调用底层栈栈内存主管程序

Java流式编程的使用

流式编程的使用步骤使用流式编程的步骤就是:设置数据源,设置数据处理的方式,设置收集结果的方式。使用filter方法实现过滤条件例子为下(查询年龄大于18的用户):@TestpublicvoidstreamTest1(){List<Student>students=Arrays.asList(newStudent("to

搜索——最短路模型,多源bfs

最短路模型,即求从起点到终点的最短路径,我们可以选择dijkstra,spfa等等,在这里我们可以利用宽搜(bfs)的特性来求,因为bfs是一层一层的向外扩展的,所以当我们第一次遍历到终点时,所在的层数即为起点到终点的最短路径。多源bfs,顾名思义,多个起点的bfs,与一般的bfs不同的地方在于根据题目要求,将多个起点

国外访问学者面签需要注意什么?

国外访问学者面签是前往国外进行学术研究或合作的关键一步,因此需要谨慎准备。以下是知识人网小编整理的一些需要注意的重要事项,以确保面签顺利进行:1.签证申请材料准备:首先,要仔细阅读所申请国家的签证要求,并准备所需的申请材料。通常,这些材料包括护照、签证申请表、邀请函、学术证明、财务证明和研究计划等。2.邀请函:如果你是

dirpro:专业的目录扫描工具教程

项目在github已开源,获取地址:https://github.com/coleak2021/dirpro1.简介dirpro是一款由python编写的目录扫描器,操作简单,功能强大,高度自动化自动根据返回状态码和返回长度,对扫描结果进行二次整理和判断,准确性非常高已实现功能可自定义扫描线程每扫描10%自动显示扫描进

位图的实现,布隆过滤器

位图:概念:由于位图是通过一个比特位来判断是不是在范围里面的,所以其对应的时间复杂度都是O(1)的。位图的实现:如上图所示:上图对应的就是3个字节,即24个比特位要想实现位图,就得知道我们要记录的这个数应该存储到哪个位置,假设这个数是x那么对应:x/32所得的值就是该数字应该存在第几个字节上x%32所得的值就是对应在此

传统的经典问题 Java 的 Interface 是干什么的

传统的经典问题Java的Interface是干什么解答上面的这个问题应该还是比较好回答的吧。只要你做过Java,通常Interface的问题多多少少会遇到,而且可能会遇到一大堆。在JAVA编程语言中是一个抽象类型(AbstractType),它被用来要求类(Class)必须实现指定的方法,使不同类的对象可以利用相同的界

Dubbo源码理解

这里写自定义目录标题欢迎使用Markdown编辑器新的改变功能快捷键合理的创建标题,有助于目录的生成如何改变文本的样式插入链接与图片如何插入一段漂亮的代码片生成一个适合你的列表创建一个表格设定内容居中、居左、居右SmartyPants创建一个自定义列表如何创建一个注脚注释也是必不可少的KaTeX数学公式新的甘特图功能,

热文推荐