Pytest参数选项在脚本中和命令行用法详解

        运行pytest可以指定目录和文件,如果不指定,pytest会搜索当前目录及其子目录中以test_开头或以_test结尾得测试函数。我们把pytest搜索测试文件和测试用例的过程称为测试搜索(test discovery)。只要遵循pytest的命名规则,pytest就能自动搜索所有待执行的测试用例。

1、测试文件命名规则,test_xxx.py或xxx_test.py

2、方法、测试函数命名规则,test_xxx

3、测试类命名规则,Testxxx,并且不能带有 init 方法

项目目录:

test_01/test_01.py代码如下:

#!/usr/bin/env python
# _*_coding:utf-8_*_
import pytestclass Test(object):def test_login_01(self):"""用例1"""print('执行用例test_login_01断言1')pytest.assume(1 == 1)print('执行用例test_login_01断言2')pytest.assume(2 == 2)def test_login_02(self):"""用例2"""print('执行用例test_login_02断言1')pytest.assume(3 == 3)print('执行用例test_login_02断言2')pytest.assume(True)

test_01/test_02.py代码如下:

#!/usr/bin/env python
# _*_coding:utf-8_*_
import pytestclass Test(object):def test_login_03(self):"""用例1"""print('执行用例test_login_03断言1')pytest.assume(1 == 1)print('执行用例test_login_03断言2')pytest.assume(2 == 2)def test_login_04(self):"""用例2"""print('执行用例test_login_04断言1')pytest.assume(3 == 3)print('执行用例test_login_04断言2')pytest.assume(True)

 

不加任何参数,控制台输出了执行用例所在的文件、用例执行结果(..)、文件百分比

命令行执行:pytest

#test_01.pyif __name__ == '__main__':pytest.main()C:\Users\admin\AppData\Local\Programs\Python\Python37\python.exe C:/Users/admin/Desktop/AutoTest/Test/test/test_01/test_01.py
============================= test session starts =============================
platform win32 -- Python 3.7.4, pytest-5.4.2, py-1.8.1, pluggy-0.13.1
rootdir: C:\Users\admin\Desktop\AutoTest\Test\test\test_01
plugins: assume-2.2.1, ordering-0.6
收集的测试用例:[<Function test_login_01>, <Function test_login_02>, <Function test_login_03>, <Function test_login_04>]
collected 4 itemstest_01.py ..                                                            [ 50%]
test_02.py ..                                                            [100%]============================== 4 passed in 0.08s ==============================Process finished with exit code 0
 

-v 说明:控制台可以输出用例更加详细的执行信息,比如用例所在的文件、类、用例名称、用例执行结果(PASSED)、用例百分比等。

命令行执行:pytest -v

#test_01.pyif __name__ == '__main__':pytest.main(['-v'])C:\Users\admin\AppData\Local\Programs\Python\Python37\python.exe C:/Users/admin/Desktop/AutoTest/Test/test/test_01/test_01.py
============================= test session starts =============================
platform win32 -- Python 3.7.4, pytest-5.4.2, py-1.8.1, pluggy-0.13.1 -- C:\Users\admin\AppData\Local\Programs\Python\Python37\python.exe
cachedir: .pytest_cache
rootdir: C:\Users\admin\Desktop\AutoTest\Test\test\test_01
plugins: assume-2.2.1, ordering-0.6
collecting ... 收集的测试用例:[<Function test_login_01>, <Function test_login_02>, <Function test_login_03>, <Function test_login_04>]
collected 4 itemstest_01.py::Test::test_login_01 PASSED                                   [ 25%]
test_01.py::Test::test_login_02 PASSED                                   [ 50%]
test_02.py::Test::test_login_03 PASSED                                   [ 75%]
test_02.py::Test::test_login_04 PASSED                                   [100%]============================== 4 passed in 0.08s ==============================Process finished with exit code 0

-k 说明:允许你使用表达式指定希望运行的测试用例,如果某测试用例名是唯一的,或者多个测试用例名的前缀和后缀相同,就可以使用表达式快速定位,如果你想执行的测试用例名字需包含login_01,就可以这么写pytest.main(['-v', '-k', 'login_01]),如果你想执行的测试用例名字需包含login_01和login_03,就可以这没写pytest.main(['-v', '-k', 'login_01 or login_03'])

控制台输出了选择了2个用例,未选择2个用例、通过了2个用例

collected 4 items / 2 deselected / 2 selected

======================= 2 passed, 2 deselected in 0.05s =======================

命令行执行:pytest -k 'login_01 or login_03'

#test_01.pyif __name__ == '__main__':pytest.main(['-v', '-k', 'login_01 or login_03'])C:\Users\admin\AppData\Local\Programs\Python\Python37\python.exe C:/Users/admin/Desktop/AutoTest/Test/test/test_01/test_01.py
============================= test session starts =============================
platform win32 -- Python 3.7.4, pytest-5.4.2, py-1.8.1, pluggy-0.13.1 -- C:\Users\admin\AppData\Local\Programs\Python\Python37\python.exe
cachedir: .pytest_cache
rootdir: C:\Users\admin\Desktop\AutoTest\Test\test\test_01
plugins: assume-2.2.1, ordering-0.6
collecting ... 收集的测试用例:[<Function test_login_01>, <Function test_login_02>, <Function test_login_03>, <Function test_login_04>]
collected 4 items / 2 deselected / 2 selectedtest_01.py::Test::test_login_01 PASSED                                   [ 50%]
test_02.py::Test::test_login_03 PASSED                                   [100%]======================= 2 passed, 2 deselected in 0.05s =======================Process finished with exit code 0

-m 说明:标记用于测试用例并分组,以便快速选中并运行,以test_login_01()和test_login_02()为例,希望同时选中并执行,需预先做好标记,假设标记名是run,则可使用@pytest.mark.run装饰器来做标记,调用执行用pytest.main(['-v', '-m', 'run'])

命令行执行:pytest -m run

#test_01.py#!/usr/bin/env python
# _*_coding:utf-8_*_
import pytestclass Test(object):@pytest.mark.rundef test_login_01(self):"""用例1"""print('执行用例test_login_01断言1')pytest.assume(1 == 1)print('执行用例test_login_01断言2')pytest.assume(2 == 2)@pytest.mark.rundef test_login_02(self):"""用例2"""print('执行用例test_login_02断言1')pytest.assume(3 == 3)print('执行用例test_login_02断言2')pytest.assume(True)if __name__ == '__main__':pytest.main(['-v', '-m', 'run'])C:\Users\admin\AppData\Local\Programs\Python\Python37\python.exe C:/Users/admin/Desktop/AutoTest/Test/test/test_01/test_01.py
============================= test session starts =============================
platform win32 -- Python 3.7.4, pytest-5.4.2, py-1.8.1, pluggy-0.13.1 -- C:\Users\admin\AppData\Local\Programs\Python\Python37\python.exe
cachedir: .pytest_cache
rootdir: C:\Users\admin\Desktop\AutoTest\Test\test\test_01
plugins: assume-2.2.1, ordering-0.6
collecting ... 收集的测试用例:[<Function test_login_01>, <Function test_login_02>, <Function test_login_03>, <Function test_login_04>]
collected 4 items / 2 deselected / 2 selectedtest_01.py::Test::test_login_01 PASSED                                   [ 50%]
test_01.py::Test::test_login_02 PASSED                                   [100%]======================= 2 passed, 2 deselected in 0.07s =======================Process finished with exit code 0

-s 说明:该选项允许测试运行时输出任何符合标准的输出流信息,例如代码里面的print。正常情况下,所有输出都会被捕获,测试失败时,pytest会做出判断并输出失败报告。

命令行执行:pytest -s

#test_01.pyif __name__ == '__main__':pytest.main(['-v', '-s'])C:\Users\admin\AppData\Local\Programs\Python\Python37\python.exe C:/Users/admin/Desktop/AutoTest/Test/test/test_01/test_01.py
============================= test session starts =============================
platform win32 -- Python 3.7.4, pytest-5.4.2, py-1.8.1, pluggy-0.13.1 -- C:\Users\admin\AppData\Local\Programs\Python\Python37\python.exe
cachedir: .pytest_cache
rootdir: C:\Users\admin\Desktop\AutoTest\Test\test\test_01
plugins: assume-2.2.1, ordering-0.6
collecting ... 收集的测试用例:[<Function test_login_01>, <Function test_login_02>, <Function test_login_03>, <Function test_login_04>]
collected 4 itemstest_01.py::Test::test_login_01 执行用例test_login_01断言1
执行用例test_login_01断言2
PASSED
test_01.py::Test::test_login_02 执行用例test_login_02断言1
执行用例test_login_02断言2
PASSED
test_02.py::Test::test_login_03 执行用例test_login_03断言1
执行用例test_login_03断言2
PASSED
test_02.py::Test::test_login_04 执行用例test_login_04断言1
执行用例test_login_04断言2
PASSED============================== 4 passed in 0.07s ==============================Process finished with exit code 0

-x 说明:正常情况下,pytest会运行每一个搜索到的测试用例,如果某个测试函数被断言失败,或者触发了外部异常,则该测试用例的运行就会停止,pytest将其标记为失败后会继续运行下一个测试用例。通常这就是我们期望的运行模式,但是在debug时,我们希望遇到失败时立即停止整个会话,这时-x就派上用场了。

命令行执行:pytest -x

#test_01.py#!/usr/bin/env python
# _*_coding:utf-8_*_
import pytestclass Test(object):def test_login_01(self):"""用例1"""print('执行用例test_login_01断言1')pytest.assume(1 == 0)print('执行用例test_login_01断言2')pytest.assume(2 == 2)def test_login_02(self):"""用例2"""print('执行用例test_login_02断言1')pytest.assume(3 == 3)print('执行用例test_login_02断言2')pytest.assume(True)if __name__ == '__main__':pytest.main(['-v', '-x'])C:\Users\admin\AppData\Local\Programs\Python\Python37\python.exe C:/Users/admin/Desktop/AutoTest/Test/test/test_01/test_01.py
============================= test session starts =============================
platform win32 -- Python 3.7.4, pytest-5.4.2, py-1.8.1, pluggy-0.13.1 -- C:\Users\admin\AppData\Local\Programs\Python\Python37\python.exe
cachedir: .pytest_cache
rootdir: C:\Users\admin\Desktop\AutoTest\Test\test\test_01
plugins: assume-2.2.1, ordering-0.6
collecting ... 收集的测试用例:[<Function test_login_01>, <Function test_login_02>, <Function test_login_03>, <Function test_login_04>]
collected 4 itemstest_01.py::Test::test_login_01 FAILED                                   [ 25%]================================== FAILURES ===================================
_____________________________ Test.test_login_01 ______________________________tp = <class 'pytest_assume.plugin.FailedAssumption'>, value = None, tb = Nonedef reraise(tp, value, tb=None):try:if value is None:value = tp()if value.__traceback__ is not tb:
>               raise value.with_traceback(tb)
E               pytest_assume.plugin.FailedAssumption: 
E               1 Failed Assumptions:
E               
E               test_01.py:11: AssumptionFailure
E               >>	pytest.assume(1 == 0)
E               AssertionError: assert FalseC:\Users\admin\AppData\Roaming\Python\Python37\site-packages\six.py:702: FailedAssumption
---------------------------- Captured stdout call -----------------------------
执行用例test_login_01断言1
执行用例test_login_01断言2
=========================== short test summary info ===========================
FAILED test_01.py::Test::test_login_01 - pytest_assume.plugin.FailedAssumption: 
!!!!!!!!!!!!!!!!!!!!!!!!!! stopping after 1 failures !!!!!!!!!!!!!!!!!!!!!!!!!!
============================== 1 failed in 0.08s ==============================Process finished with exit code 0

--maxfail=num 说明:假设你允许pytest失败几次后再停止,则可以使用--maxfail选项,明确指定可以失败几次后停止整个会话。

命令行执行:pytest --maxfail=2

#test_01.py#!/usr/bin/env python
# _*_coding:utf-8_*_
import pytestclass Test(object):def test_login_01(self):"""用例1"""print('执行用例test_login_01断言1')pytest.assume(1 == 0)print('执行用例test_login_01断言2')pytest.assume(2 == 2)def test_login_02(self):"""用例2"""print('执行用例test_login_02断言1')pytest.assume(3 == 3)print('执行用例test_login_02断言2')pytest.assume(True)

test_01.py中有一个断言失败,最大失败次数设置成1 pytest.main(['-v', '--maxfail=1'])和pytest.main(['-v', '-x'])效果一样的,当遇到第一个失败时,就停止整个会话执行

#test_01.pyif __name__ == '__main__':pytest.main(['-v', '--maxfail=1'])C:\Users\admin\AppData\Local\Programs\Python\Python37\python.exe C:/Users/admin/Desktop/AutoTest/Test/test/test_01/test_01.py
============================= test session starts =============================
platform win32 -- Python 3.7.4, pytest-5.4.2, py-1.8.1, pluggy-0.13.1 -- C:\Users\admin\AppData\Local\Programs\Python\Python37\python.exe
cachedir: .pytest_cache
rootdir: C:\Users\admin\Desktop\AutoTest\Test\test\test_01
plugins: assume-2.2.1, ordering-0.6
collecting ... 收集的测试用例:[<Function test_login_01>, <Function test_login_02>, <Function test_login_03>, <Function test_login_04>]
collected 4 itemstest_01.py::Test::test_login_01 FAILED                                   [ 25%]================================== FAILURES ===================================
_____________________________ Test.test_login_01 ______________________________tp = <class 'pytest_assume.plugin.FailedAssumption'>, value = None, tb = Nonedef reraise(tp, value, tb=None):try:if value is None:value = tp()if value.__traceback__ is not tb:
>               raise value.with_traceback(tb)
E               pytest_assume.plugin.FailedAssumption: 
E               1 Failed Assumptions:
E               
E               test_01.py:11: AssumptionFailure
E               >>	pytest.assume(1 == 0)
E               AssertionError: assert FalseC:\Users\admin\AppData\Roaming\Python\Python37\site-packages\six.py:702: FailedAssumption
---------------------------- Captured stdout call -----------------------------
执行用例test_login_01断言1
执行用例test_login_01断言2
=========================== short test summary info ===========================
FAILED test_01.py::Test::test_login_01 - pytest_assume.plugin.FailedAssumption: 
!!!!!!!!!!!!!!!!!!!!!!!!!! stopping after 1 failures !!!!!!!!!!!!!!!!!!!!!!!!!!
============================== 1 failed in 0.06s ==============================Process finished with exit code 0

test_01.py中有一个断言失败,最大失败次数设置成2 pytest.main(['-v', '--maxfail=2']),当遇到第一个失败时,未满足2个失败的条件,所以会继续执行后续的用例。

#test_01.py#!/usr/bin/env python
# _*_coding:utf-8_*_
import pytestclass Test(object):def test_login_01(self):"""用例1"""print('执行用例test_login_01断言1')pytest.assume(1 == 0)print('执行用例test_login_01断言2')pytest.assume(2 == 2)def test_login_02(self):"""用例2"""print('执行用例test_login_02断言1')pytest.assume(3 == 3)print('执行用例test_login_02断言2')pytest.assume(True)if __name__ == '__main__':pytest.main(['-v', '--maxfail=2'])C:\Users\admin\AppData\Local\Programs\Python\Python37\python.exe C:/Users/admin/Desktop/AutoTest/Test/test/test_01/test_01.py
============================= test session starts =============================
platform win32 -- Python 3.7.4, pytest-5.4.2, py-1.8.1, pluggy-0.13.1 -- C:\Users\admin\AppData\Local\Programs\Python\Python37\python.exe
cachedir: .pytest_cache
rootdir: C:\Users\admin\Desktop\AutoTest\Test\test\test_01
plugins: assume-2.2.1, ordering-0.6
collecting ... 收集的测试用例:[<Function test_login_01>, <Function test_login_02>, <Function test_login_03>, <Function test_login_04>]
collected 4 itemstest_01.py::Test::test_login_01 FAILED                                   [ 25%]
test_01.py::Test::test_login_02 PASSED                                   [ 50%]
test_02.py::Test::test_login_03 PASSED                                   [ 75%]
test_02.py::Test::test_login_04 PASSED                                   [100%]================================== FAILURES ===================================
_____________________________ Test.test_login_01 ______________________________tp = <class 'pytest_assume.plugin.FailedAssumption'>, value = None, tb = Nonedef reraise(tp, value, tb=None):try:if value is None:value = tp()if value.__traceback__ is not tb:
>               raise value.with_traceback(tb)
E               pytest_assume.plugin.FailedAssumption: 
E               1 Failed Assumptions:
E               
E               test_01.py:11: AssumptionFailure
E               >>	pytest.assume(1 == 0)
E               AssertionError: assert FalseC:\Users\admin\AppData\Roaming\Python\Python37\site-packages\six.py:702: FailedAssumption
---------------------------- Captured stdout call -----------------------------
执行用例test_login_01断言1
执行用例test_login_01断言2
=========================== short test summary info ===========================
FAILED test_01.py::Test::test_login_01 - pytest_assume.plugin.FailedAssumption: 
========================= 1 failed, 3 passed in 0.09s =========================Process finished with exit code 0

 

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

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

相关文章

Win7下OpenCV3.2.0+VS2015配置

参考自&#xff1a;https://blog.csdn.net/qq_22812319/article/details/78335880 一、预备知识 1、下载opencv时&#xff0c;opencv与vs版本是有固定关系的。 2、vs一般使用release版本。 3、首先在下载的openCV解压目录opencv/build/x64/下查看文件夹名&#xff0c;如下图…

Pytest标记预期失败得测试用例@pytest.mark.xfail()

pytest除了测试函数中使用这个方法pytest.xfail()外&#xff0c;xfail还有一种使用方法。就是pytest.mark.xfail()标记预期会失败的用例&#xff0c;即期望测试用例是失败的&#xff0c;但是不会影响测试用例的的执行。 标记的用例运行后&#xff0c;断言失败&#xff0c;所以…

win7 vs2015配置qt5.11

1、下载vs&#xff0c;和qt。 qt百度网盘下载链接&#xff1a;https://pan.baidu.com/s/12bUt31_mXGvgc3tdJRkkkw 提取码&#xff1a;2swk 下载完成之后&#xff0c;直接进行安装&#xff0c;下一步。 安装的时候要根据自己的VS版本选择相应的msvc&#xff08;VC运行库&…

LNK2019 无法解析的外部符号 __imp_CommandLineToArgvW,该符号在函数 WinMain 中被引用

在VS2017中新建Qt GUI 工程&#xff0c;无法编译&#xff0c;报链接错误 LNK2019 无法解析的外部符号 __imp_CommandLineToArgvW&#xff0c;该符号在函数 WinMain 中被引用 经比对&#xff0c;是lib配置问题。如歌在Qt Creator中创建工程然后再在VS2017中打开&#xff0c;则…

原创 | 开源AI测试专题、Jmeter测试专题

开源 AI 测试专题震惊&#xff01;AI成功落地自动化测试AI 软件测试工具 Mabl 快速概览AI测试工具 Mabl - 测试用例自动自愈Sauce Labs 的持续 UI 自动化测试云疫情中诞生的 AI 测试工具&#xff1a;ai-webdriverAI 测试又多新花招&#xff1f;ai-webdriver-1.0.1 独家更新一眼…

Qt 二级菜单无法输入中文

一、qt中二级菜单无法输入中文 解决办法&#xff1a;写到其他地方再粘贴复制过来即可ok 二、qt界面设置后&#xff0c;vs中没有反应 解决方法&#xff1a;在vs中寻找 ui_projectname.h projectname即为你的项目名&#xff0c;查看其中是否已有你qt中已经声明的控件&#x…

Python深层解析json数据之JsonPath

我们在做接口自动化时&#xff0c;一般接口响应的都是json数据体&#xff0c;对响应数据进行提取使用或断言&#xff0c;当数据量很大或层级很深时&#xff0c;就会变得很麻烦&#xff0c;于是就可以用到jsonpath模块&#xff0c;解决json路径深取值难的问题。一、 jsonpath介绍…

VS2015+qt5.11入门(实现计算机的加法和登录操作)

1、在vs中新建qt项目 2、打开Form Files中的.ui文件。即打开qt creator界面 3、在界面中添加两个label&#xff0c;两个LineEdit&#xff0c;一个计算按钮。点击保存 控件右击-->改变对象名称可以直接改变控件名。&#xff08;会映射显示在vs中的ui_projectname.h中&…

Python中map()函数用法

map() 是python的内置函数&#xff0c;会根据提供的函数对指定序列做映射。对可迭代函数*iterables中的每个元素应用func方法&#xff0c;将结果作为迭代器对象返回。注意&#xff1a;map()函数返回的是一个新的迭代器对象&#xff0c;不会改变原有对象 map()用法 class map(ob…

任务管理器只有概要信息解决办法

在任务管理器最顶端左击两下即可ok 参考自&#xff1a;https://wenda.so.com/q/1515482566217850

抓包工具mitmproxy环境配置使用(一)

一、mitmproxy介绍mitmproxy是一款开源的抓包工具&#xff0c;支持SSL的HTTP代理&#xff0c;它可以用于调试HTTP通信&#xff0c;发起中间人攻击等&#xff0c;还可以配合自定义python脚本使用&#xff0c;不同于 fiddler 或 wireshark 等抓包工具&#xff0c;mitmproxy 不仅可…

cvtColor不是cv的成员

头文件的问题 #include <opencv2/opencv.hpp> 这个就可以 opencv 3导入文件&#xff1a; #include<opencv2/core/core.hpp> #include<opencv2/highgui/highgui.hpp> #include <opencv2/opencv.hpp>

Pytest fixture参数化params

unittest使用ddt来实现测试用例参数化、或parameterized实现测试用例参数化&#xff0c;pytest测试用例里面对应的参数可以用 parametrize 实现参数化&#xff0c;今天我们来了解下fixture参数化paramsfixture的参数可以解决大量重复代码工作,比如数据库的连接、查询、关闭等.同…

opencv videocapture读取视频cap.isOpened 输出总是false

利用qtopencv3c 读取视频文件&#xff0c;发现isOpened()总是输出false 一直以为是语句的问题&#xff0c;后来反应过来可能是打开视频格式&#xff0c;或者视频本身能否播放。 检查发现&#xff0c;是视频本身不能播放&#xff0c;换了个视频&#xff0c;就ok了

python3中选择文件对话框的格式打开和保存图片

tkinter.filedialog.asksaveasfilename() # 选择以什么文件名保存&#xff0c;返回文件名 tkinter.filedialog.askopenfilename() # 选择打开什么文件&#xff0c;返回文件名有时候我们想要以选择文件对话框的格式打开一张图片或者保存图片&#xff0c;我在尝试之后把我的…

Python测试开发django1.简介

Django是一种基于Python开发的开源的高级Web应用框架&#xff0c;使用Django&#xff0c;使你能够以最小的代价构建和维护高质量的Web应用。Django 本身基于 MVC 模型&#xff0c;即 Model&#xff08;模型&#xff09; View&#xff08;视图&#xff09; Controller&#xff0…

$ajax({}).done 和 $ajax({}) success 区别

jquery中1.5版本之后使用ajax中的done方法。 该方法和ajax中的success有类似的功效。首先必须是jquery中的1.5版本之后。 jQuery中各个事件执行顺序如下&#xff1a; 1.ajaxStart(全局事件) 2.beforeSend 3.ajaxSend(全局事件) 4.success 5.ajaxSuccess(全局事件) 6.er…

Python测试开发django2.环境部署项目创建

Django是由Python编写的Web框架&#xff0c;依赖Python环境&#xff0c;所以需要提前安装好Python环境。建议安装最新版本的Python3&#xff0c;Python 下载地址&#xff1a;https://www.python.org/downloads/1、Django官网https://www.djangoproject.com/2、Django版本Django…

excel如何输入毫秒级时间

1、选择单元格或单元格区域&#xff1b; 2、右击-设置单元格格式&#xff08;或按ctrl1&#xff09;&#xff1b; 3、数字选项卡-自定义-h:mm:ss.000 4、单元格就可以输入精度为千分之一秒的时间&#xff0c;如&#xff1a;20:15:21.451 参考自https://zhidao.baidu.com/questi…

Python测试开发django3.视图和URL配置

1、Django path&#xff08;&#xff09;方法Django路由系统中最重要的path()方法可以接收4个参数&#xff0c;其中2个是必须的&#xff1a;route和view&#xff0c;以及2个可选的参数&#xff1a;kwargs和namedef path(route, view, kwargsNone, nameNone):return re_path(rou…