从0到1精通自动化测试,pytest自动化测试框架,配置文件pytest.ini(十三)

一、前言

pytest配置文件可以改变pytest的运行方式,它是一个固定的文件pytest.ini文件,读取配置信息,按指定的方式去运行

二、ini配置文件

  1. pytest里面有些文件是非test文件
  2. pytest.ini pytest的主配置文件,可以改变pytest的默认行为
  3. conftest.py 测试用例的一些fixture配置
  4. _init_.py 识别该文件夹为python的package包
  5. tox.ini 与pytest.ini类似,用tox工具时候才有用
  6. setup.cfg 也是ini格式文件,影响setup.py的行为

ini文件基本格式

# 保存为pytest.ini文件[pytest]addopts = -rsxX
xfail_strict = ture

使用pytest —help指令可以查看pytest.ini的设置选项

—rsxX 表示pytest报告所有测试用例被跳过、预计失败、预计失败但实际被通过的原因

三、mark标记

如下案例,使用了2个标签:webtest和hello,使用mark标记功能对于以后分类测试非常有用处

# content of test_mark.py
import pytest@pytest.mark.webtest
def test_send_http():print("mark web test")def test_something_quick():passdef test_another():pass@pytest.mark.hello
class TestClass:def test_01(self):print("hello :")def test_02(self):print("hello world!")if __name__ == "__main__":pytest.main(["-v", "test_mark.py", "-m=hello"])

运行结果

============================= test session starts =============================
platform win32 -- Python 3.6.0, pytest-3.6.3, py-1.5.4, pluggy-0.6.0 -- D:\soft\python3.6\python.exe
cachedir: .pytest_cache
metadata: {'Python': '3.6.0', 'Platform': 'Windows-7-6.1.7601-SP1', 'Packages': {'pytest': '3.6.3', 'py': '1.5.4', 'pluggy': '0.6.0'}, 'Plugins': {'metadata': '1.7.0', 'html': '1.19.0', 'allure-adaptor': '1.7.10'}, 'JAVA_HOME': 'D:\\soft\\jdk18\\jdk18v'}
rootdir: D:\MOMO, inifile:
plugins: metadata-1.7.0, html-1.19.0, allure-adaptor-1.7.10
collecting ... collected 5 items / 3 deselectedtest_mark.py::TestClass::test_01 PASSED                                  [ 50%]
test_mark.py::TestClass::test_02 PASSED                                  [100%]=================== 2 passed, 3 deselected in 0.11 seconds ====================

有时候标签多了,不容易记住,为了方便后续执行指令的时候能准确使用mark的标签,可以写入到pytest.ini文件

# pytest.ini
[pytest]markers =webtest:  Run the webtest casehello: Run the hello case

标记好之后,可以使用pytest —markers查看到

$ pytest —markers
D:\MOMO>pytest --markers
@pytest.mark.webtest:  Run the webtest case@pytest.mark.hello: Run the hello case@pytest.mark.skip(reason=None): skip the given test function with an optional re
ason. Example: skip(reason="no way of currently testing this") skips the test.@pytest.mark.skipif(condition): skip the given test function if eval(condition)
results in a True value.  Evaluation happens within the module global context. E
xample: skipif('sys.platform == "win32"') skips the test if we are on the win32
platform. see http://pytest.org/latest/skipping.html@pytest.mark.xfail(condition, reason=None, run=True, raises=None, strict=False):mark the test function as an expected failure if eval(condition) has a True val
ue. Optionally specify a reason for better reporting and run=False if you don't
even want to execute the test function. If only specific exception(s) are expect
ed, you can list them in raises, and if the test fails in other ways, it will bereported as a true failure. See http://pytest.org/latest/skipping.html@pytest.mark.parametrize(argnames, argvalues): call a test function multiple tim
es passing in different arguments in turn. argvalues generally needs to be a lis
t of values if argnames specifies only one name or a list of tuples of values ifargnames specifies multiple names. Example: @parametrize('arg1', [1,2]) would l
ead to two calls of the decorated test function, one with arg1=1 and another wit
h arg1=2.see http://pytest.org/latest/parametrize.html for more info and example
s.@pytest.mark.usefixtures(fixturename1, fixturename2, ...): mark tests as needingall of the specified fixtures. see http://pytest.org/latest/fixture.html#usefix
tures@pytest.mark.tryfirst: mark a hook implementation function such that the plugin
machinery will try to call it first/as early as possible.@pytest.mark.trylast: mark a hook implementation function such that the plugin m
achinery will try to call it last/as late as possible.

最上面两个就是刚才写入到pytest.ini的配置了

四、禁用xpass

设置xfail_strict = true可以让那些标记为@pytest.mark.xfail但实际通过的测试用例被报告为失败

什么叫标记为@pytest.mark.xfail但实际通过,这个比较绕脑,看以下案例

# content of test_xpass.py
import pytestdef test_hello():print("hello world!")assert 1@pytest.mark.xfail()
def test_momo1():a = "hello"b = "hello world"assert a == b@pytest.mark.xfail()
def test_momo2():a = "hello"b = "hello world"assert a != bif __name__ == "__main__":pytest.main(["-v", "test_xpass.py"])

测试结果

collecting ... collected 3 itemstest_xpass.py::test_hello PASSED    [ 33%]
test_xpass.py::test_momo1 xfail     [ 66%]
test_xpass.py::test_momo2 XPASS     [100%]=============== 1 passed, 1 xfailed, 1 xpassed in 0.27 seconds ================

test_momo1和test_momo2这2个用例一个是a == b一个是a != b,两个都标记失败了,我们希望两个用例不用执行全部显示xfail。实际上最后一个却显示xpass.为了让两个都显示xfail,那就加个配置
xfail_strict = true

# pytest.ini
[pytest]markers =webtest:  Run the webtest casehello: Run the hello casexfail_strict = true

再次运行,结果就变成

collecting ... collected 3 itemstest_xpass.py::test_hello PASSED        [ 33%]
test_xpass.py::test_momo1 xfail         [ 66%]
test_xpass.py::test_momo2 FAILED        [100%]================================== FAILURES ===================================
_________________________________ test_momo2 __________________________________
[XPASS(strict)] 
================ 1 failed, 1 passed, 1 xfailed in 0.05 seconds ================

这样标记为xpasx的就被强制性变成failed的结果

五、配置文件如何放

一般一个工程下方一个pytest.ini文件就可以了,放到顶层文件夹下

请添加图片描述

六、addopts

addopts参数可以更改默认命令行选项,这个当我们在cmd输入指令去执行用例的时候,会用到,比如我想测试完生成报告,指令比较长

$ pytest -v —rerun 1 —html=report.html —self-contained-html

 每次输入这么多,不太好记住,于是可以加到pytest.ini里

# pytest.ini
[pytest]markers =webtest:  Run the webtest casehello: Run the hello casexfail_strict = trueaddopts = -v --rerun 1 --html=report.html --self-contained-html

这样我下次打开cmd,直接输入pytest,它就能默认带上这些参数了

最后感谢每一个认真阅读我文章的人,礼尚往来总是要有的,虽然不是什么很值钱的东西,如果你用得到的话可以直接拿走:

【软件测试技术交流(资料分享)】:320231853(备注C)icon-default.png?t=N5K3http://qm.qq.com/cgi-bin/qm/qr?_wv=1027&k=wvdQ7J4AdIhjWqHqp-aGkIYde-wLap1P&authKey=4Zvhap%2BH44eyRbqYp4nbq21NHkZHo8kyPWSvyY50ssokKTX0rEYT15ZCaKuJE0Zc&noverify=0&group_code=320231853生命不息,奋斗不止。每一份努力都不会被辜负,只要坚持不懈,终究会有回报。珍惜时间,追求梦想。不忘初心,砥砺前行。你的未来,由你掌握!

生命短暂,时间宝贵,我们无法预知未来会发生什么,但我们可以掌握当下。珍惜每一天,努力奋斗,让自己变得更加强大和优秀。坚定信念,执着追求,成功终将属于你!

只有不断地挑战自己,才能不断地超越自己。坚持追求梦想,勇敢前行,你就会发现奋斗的过程是如此美好而值得。相信自己,你一定可以做到!

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

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Android Binder通信原理(三):service注册

源码基于:Android R 0. 前言 上一文中详细分析了servicemanger 的启动流程,我们知道 servicemanager 作为 binder 机制中的一个特殊service,利用service map管理所有service,也是所有binder 通信的入口。 本文着重分析 service …

智能文档图像处理技术应用与实践

写在前面智能文档处理面临的技术难题智能文档处理的研究领域● 文档图像分析与预处理● 手写板反光擦除● 版面分析与文档还原 写在最后 写在前面 VALSE 2023 无锡视觉与学习青年学者研讨会近期在无锡国际博览中心举办,由江南大学和无锡新吴区联合承办。本次会议旨…

netty学习(1):多个客户端与服务器通信

1. 基于前面一节netty学习(1):1个客户端与服务器通信 只需要把服务器的handler改造一下即可,通过ChannelGroup 找到所有的客户端channel,发送消息即可。 package server;import io.netty.channel.*; import io.netty.channel.gr…

陪诊小程序系统|陪诊软件开发|陪诊系统功能和特点

随着医疗服务的逐步改善和完善,越来越多的人群开始走向医院就诊,而其中不少人往往需要有人陪同前往,这就导致了许多矛盾与问题的发生,比如长时间等待、找不到合适的陪诊人员等。因此为人们提供一种方便快捷的陪诊服务成为了一种新…

成本降低60%至70%?中国展现顶级电池技术,锂电就是下一个铅酸

在3月份,宁德时代宣布加速推进钠离子电池产业化,以降低成本并提供差异化产品和技术,帮助客户提升产品竞争力和占据更大市场份额。孚能科技已在上半年开始批量生产钠离子电池,而拓邦股份也在最近的国际电池技术展上发布了自家的钠离…

vue下基于elementui自定义表单-后端数据设计篇

vue下基于elementui自定义表单-后端篇 自定义表单目前数据表单设计是基于数据量不大的信息单据场景,因为不考虑数据量带来的影响。 数据表有: 1.表单模版表,2.表单实例表,3.表单实例项明细表,4表单审批设计绑定表 以FormJson存…

【动态规划】LeetCode 583. 两个字符串的删除操作 Java

583. 两个字符串的删除操作 我的代码,错误代码,只考虑到了字母出现的次数,没有考虑到两个字符串中字母出现的顺序 class Solution {public int minDistance(String word1, String word2) {int[] arr1 new int[26];int[] arr2 new int[26];…

【数据结构】——常见排序算法(演示图+代码+算法分析)

目录 1. 常见排序算法 1.2 稳定性 2. 常见排序算法的实现 2.1 插入排序 2.1.1基本思想 2.1.2代码 2.1.4算法分析 2.2 希尔排序 2.2.1基本思想 2.2.2代码 2.2.3演示图 2.2.4算法分析 2.3 选择排序 2.3.1基本思想 2.3.2代码 2.3.3演示图 2.3.4算法分析 2.4 堆排…

npm启动,node.js版本过高

“dev_t”: “set NODE_OPTIONS”–openssl-legacy-provider" & npm run dev\n"

Quiz 12: Regular Expressions | Python for Everybody 配套练习_解题记录

文章目录 Python for Everybody课程简介Regular Expressions单选题(1-8)操作题Regular Expressions Python for Everybody 课程简介 Python for Everybody 零基础程序设计(Python 入门) This course aims to teach everyone the …

使用Nginx的反向代理来访问服务器例子——Nginx笔记

因为网站上的视频加载过慢,想使用nginx服务器实现HLS视频播放服务。顺便记录一下通过Nginx的方向代理来访问服务器。这里在原先的项目上进行改造。原先的项目已经部署在公网,使用tomcat服务器,可以直接用地址进行访问。 1.这里使用的8080端口…

springboot详细整合mybatisplus

SpringBoot详细整合mybatisPlus 文章目录 SpringBoot详细整合mybatisPlus一、引入mybatis_plus依赖二、修改mybatis_plus的yml配置三、添加mybatis_plus的其他配置以及包扫描四,修改mybatis的配置(这一步根据实际情况修改) 无奈,一…