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;如下图…

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运行库&…

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

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

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中&…

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

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

Pytest fixture参数化params

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

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…

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…

Python测试开发django4.templates模板配置

【上一篇】我们讲了Python测试开发django3.视图和URL配置今天详细介绍下 Django 模板的应用&#xff0c;模板是一个文本&#xff0c;用于分离文档的表现形式和内容。我们已经知道创建项目用django-admin startproject helloworld&#xff0c;一个项目下可以有多个应用&#xff…

QImage QPixmap Mat区别

cvMat可以通过自定义函数转换为QImage Qimage通过fromImage函数 可以转换为QPixmap 绘图设备是指继承QPainterDevice的子类。Qt一共提供了四个这样的类&#xff0c;分别是QPixmap、QBitmap、QImage和 QPicture。其中&#xff0c;QPixmap专门为图像在屏幕上的显示做了优化&…

Python测试开发django5.templates模板变量传参

上一篇&#xff0c;我们学习了Python测试开发django4.templates模板配置templates模板中html文件是一个静态页面&#xff0c;写四的&#xff0c;如果有时我们想动态的传入一些不同的参数&#xff0c;想实现在一个固定的html样式&#xff0c;这就可以用django的模板变量传参来解…

git入门一

1、下载git并安装 &#xff08;github网站中 start 收藏 follow 关注 watch &#xff08;关注项进度&#xff09;查看进度&#xff09; 2、启动git。一般用git bash 3、改变git bash界面 4、在本地建立git bash仓库 到本地文件夹下&#xff0c;右击建立 git bash here。 5、…

Python测试开发django5.urls.py参数name与a标签的引用

上一篇&#xff0c;我们学习了Python测试开发django5.templates模板变量传参如果我们有2个页面home.html和demo.html&#xff0c;两个页面是独立的没有关系的&#xff0c;现在需要从home.html页&#xff0c;点某个超链按钮&#xff0c;跳转到demo.html页&#xff0c;home.html页…

git入门二

exit 退出git bash 命令窗口 1、刚安装成功界面&#xff1a; 2、查看配置信息&#xff1a; 3、配置用户名和邮箱 此时再看git config --list。已经有用户名和邮箱的信息 4、把当前目录初始化为仓库并提交 5、将远程和本地连接 &#xff08;1&#xff09;在https//github.com 上…

opencv1-加载、修改、保存图像

我的实践&#xff1a; #include<opencv2\opencv.hpp> #include<iostream> using namespace cv; using namespace std; int main() {Mat src imread("E:\\vs2015\\opencvstudy\\2.jpg", 1);if (src.empty()){cout << "could not load the i…

接口自动化实战设计思路,想法及疑问(一)

各位粉丝朋友们大家好&#xff0c;最近在学习研究接口自动化测试时&#xff0c;在设计思路和实践过程中&#xff0c;碰到了很多问题&#xff0c;再不断的优化和调整&#xff0c;这过程中产生了很多疑问和不解&#xff0c;并与很多测试的朋友进行交流想法&#xff0c;但是各自想…