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

1.创建   test_parametrize.py

通过

@pytest.mark.parametrize 方法设置参数
import pytestimport 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.pyPS 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.pyPS 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.powtest_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.powtest_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.powtest_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 * bclass TestMultiply:# =====fixtures========@classmethoddef setup_class(cls):print("setup_class=========>")@classmethoddef 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) == 30def 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 pytestif __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 itemsfixture/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 ==============================

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.xdnf.cn/news/140773.html

如若内容造成侵权/违法违规/事实不符,请联系一条长河网进行投诉反馈,一经查实,立即删除!

相关文章

R语言风险价值:ARIMA,GARCH,Delta-normal法滚动估计VaR(Value at Risk)和回测分析股票数据...

全文链接&#xff1a;http://tecdat.cn/?p24492 此分析的目的是构建一个过程&#xff0c;以在给定时变波动性的情况下正确估计风险价值。风险价值被广泛用于衡量金融机构的市场风险。我们的时间序列数据包括 1258 天的股票收益&#xff08;点击文末“阅读原文”获取完整代码数…

Java————网络编程

一 、网络编程基础 1. 为什么需要网络编程 用户在浏览器中&#xff0c;打开在线视频网站&#xff0c; 如优酷看视频&#xff0c;实质是通过网络&#xff0c; 获取到网络上的一个视频资源。 与本地打开视频文件类似&#xff0c;只是视频文件这个资源的来源是网络。 相比本地资…

汽车电子——产品标准规范汇总和梳理(车载网络)

文章目录 前言 一、菊花链 二、K Line 三、L Line 四、RS485 五、LIN 六、CAN 七、FlexRay 八、MOST 九、Bluetooth 十、LAN 十一、移动网络 十二、实施和测试 总结 前言 见《汽车电子——产品标准规范汇总和梳理》 一、菊花链 暂无统一的正式标准。 菊花链通信&…

Linux查看系统信息

# 查看操作系统的详细信息 uname -a# 查看已安装的Linux发行版信息 cat /etc/os-release# 查看Linux Standard Base (LSB)的信息 lsb_release -a# 查看主机的信息 hostnamectl# 查看文件系统的磁盘空间使用情况 df -h# 查看系统内存的使用情况 free -h# 查看网络接口的信息 ifc…

[React] react-hooks如何使用

react-hooks思想和初衷&#xff0c;也是把组件&#xff0c;颗粒化&#xff0c;单元化&#xff0c;形成独立的渲染环境&#xff0c;减少渲染次数&#xff0c;优化性能。 文章目录 1.为什么要使用hooks2.如何使用hooks2.1 useState2.2 useEffect2.3 useLayoutEffect2.4 useRef2.5…

【网络编程】TCP Socket编程

TCP Socket编程 1. ServerSocket2. Socket3. TCP的长短连接4. Socket 通信模型5. 代码示例&#xff1a;TCP 回显服务器 流套接字&#xff1a; 使用传输层TCP协议 TCP: 即Transmission Control Protocol&#xff08;传输控制协议&#xff09;&#xff0c;传输层协议。 TCP的特点…

【计算机网络】IP协议(下)

文章目录 1. 特殊的IP地址2. IP地址的数量限制3. 私有IP地址和公网IP地址私有IP为什么不能出现在公网上&#xff1f;解决方案——NAT技术的使用 4. 路由5. IP分片问题为什么要进行切片&#xff1f;如何做的分片和组装&#xff1f;16位标识3位标志13位片偏移例子 细节问题如何区…

基于springboot地方废物回收机构管理系统springboot11

大家好✌&#xff01;我是CZ淡陌。一名专注以理论为基础实战为主的技术博主&#xff0c;将再这里为大家分享优质的实战项目&#xff0c;本人在Java毕业设计领域有多年的经验&#xff0c;陆续会更新更多优质的Java实战项目&#xff0c;希望你能有所收获&#xff0c;少走一些弯路…

智慧农业农场小程序源码 智慧农场系统源码

智慧农业农场小程序源码 智慧农场系统源码 一、 智慧农场系统的组成 智慧农场系统一般包括传感器、控制器、数据采集与处理平台、应用软件等组成部分。其中, 传感器主要用于采集土壤温度、湿度、光照强度等环境参数,以及作物生长状态、水肥情况等生产信息。控制器则根据传感器…

GLTF编辑器的另一个作用

1、GLB模型介绍 GLB&#xff08;GLTF Binary&#xff09;是一种用于表示三维模型和场景的文件格式。GLTF是"GL Transmission Format"的缩写&#xff0c;是一种开放的、跨平台的标准&#xff0c;旨在在各种3D图形应用程序和引擎之间进行交换和共享。 GLB文件是GLTF文件…

PyCharm 手动下载插件

插件模块一直加载失败&#xff0c;报错信息&#xff1a; Marketplace plugins are not loaded. Check the internet connection and refresh. 尝试了以下方法&#xff0c;均告失败&#xff1a; pip 换源Manage Plugin Repositories...HTTP 代理设置...关闭三个防火墙 最后选…

RK3568平台开发系列讲解(工具命令篇)ADB的安装

🚀返回专栏总目录 文章目录 一、ADB介绍二、Windows 下安装 adb 工具沉淀、分享、成长,让自己和他人都能有所收获!😄 一、ADB介绍 adb 全称 Android Debug Bridge,直译过来就是 Android 调试桥,它是一个通用的命令行工具。adb 做为 Android 设备与 PC 端连接的一个桥梁…

MissionPlanner编译过程

环境 windows 10 mission planner 1.3.80 visual studio 2022 git 2.22.0 下载源码 (已配置git和ssh) 从github上克隆源码 git clone gitgithub.com:ArduPilot/MissionPlanner.git进入根目录 cd MissionPlanner在根目录下的ExtLibs文件下是链接的其它github源码&#xff0…

MySQL 高级(进阶) SQL 语句(二) -----存储过程

目录 1 存储过程 1.1 创建存储过程​ 1.2 调用存储过程 1.3 查看存储过程 1.4 存储过程的参数 1.5 修改存储过程 1.6 删除存储过程 2 条件语句 3 循环语句 1 存储过程 存储过程是一组为了完成特定功能的SQL语句集合。 存储过程在使用过程中是将常用或者复杂的工作预…

ClickHouse分布式集群部署

目录 ​编辑 一、环境说明 二、安装部署 2.1 RPM方式安装 2.1.1 安装yum-utils 2.1.2 配置yum repo源 2.1.3 yum install 下载安装clickhouse 2.2 信息配置 2.2.1 配置外网可访问地址 2.2.2 修改存储路径 2.2.2.1 新建存储目录 2.2.2.2 授权 2.2.2.3 修改配置 2.…

单片机第三季-第三课:STM32开发板原理图、配置、浮点运算单元

目录 1&#xff0c;开发板原理图 2&#xff0c;浮点运算单元&#xff08;FPU&#xff09; 1&#xff0c;开发板原理图 课程视频比较早&#xff0c;介绍了三款开发板。观看视频时用的开发板说和51单片机共板的STM32核心板&#xff0c;将51单片机从底座拆下来后&#xff0c;安…

【从0学习Solidity】35. 荷兰拍卖

【从0学习Solidity】35. 荷兰拍卖 博主简介&#xff1a;不写代码没饭吃&#xff0c;一名全栈领域的创作者&#xff0c;专注于研究互联网产品的解决方案和技术。熟悉云原生、微服务架构&#xff0c;分享一些项目实战经验以及前沿技术的见解。关注我们的主页&#xff0c;探索全栈…

黑马JVM总结(十四)

&#xff08;1&#xff09;分代回收_1 Java虚拟机都是结合前面几种算法&#xff0c;让他们协同工作&#xff0c;具体实现是虚拟机里面一个叫做分代的垃圾回收机制&#xff0c;把我们堆内存大的区域划分为两块新生代、老年代 新生代有划分为伊甸园、幸存区Form、幸存区To 为什…

ARMv8 cache的包含策略inclusive 和 exclusive之间的区别以及Cortex-A55示例详解

Inclusive 和 Exclusive 一&#xff0c; 什么是cache的inclusive 和 exclusive二&#xff0c;Inclusive 和 Exclusive cache示例2.1 Inclusive cache2.2 Exclusive cache 三&#xff0c; inclusive cache和 exclusive cache的比较3.1 cache coherency3.2 miss rate3.3 cache ca…

使用 Docker 安装 Elasticsearch (本地环境 M1 Mac)

Elasticsearchkibana下载安装 docker pull elasticsearch:7.16.2docker run --name es -d -e ES_JAVA_OPTS“-Xms512m -Xmx512m” -e “discovery.typesingle-node” -p 9200:9200 -p 9300:9300 elasticsearch:7.16.2docker pull kibana:7.16.2docker run --name kibana -e EL…