python中pytest收集用例规则与运行指定用例详解


Posted in Python onJune 27, 2019

前言

上篇文章相信大家已经了解了pytest在cmd下结合各种命令行参数如何运行测试用例,并输出我们想要看到的信息。那么今天会讲解一下pytest是如何收集我们写好的用例?我们又有哪些方式来运行单个用例或者批量运行用例呢?下面将为大家一一解答!

pytest收集用例原理分析

首先我们按照如下目录结构新建我们的项目

[pyttest搜索测试用例的规则]
|[测试用例目录1]
| |__init__.py
| |test_测试模块1.py
| |test_测试模块2.py
|[测试用例目录2]
| |__init__.py
| |test_测试用例1.py
| |测试用例.py
|test_测试模块.py
|测试用例2.py

代码实例

# test_测试模块1.py
def test_testFunc1():
print('\n我是一个测试用例! in test_testFunc1')
assert 1 == 1
def func1():
print('我不是一个测试用例')
assert 1 == 1
# test_测试模块2.py
class TestClass1(object):
def test_class_func1(self):
print('\n 我是一个类里面的测试用例 in test_class_func1')
assert 1 == 1
def class_func1(self):
print('我是类里面的一个普通函数!')
# test_测试用例1.py
class TestClass2(object):
def test_class_func2(self):
print('\n 我是一个类里面的测试用例 in test_class_func2',)
assert 1 == 1
def class_func2(self):
print('我是类里面的一个普通函数!')
def test_testFunc2():
print('\n我是一个测试用例 in test_testFunc2!')
assert 1 == 1
def func2():
print('我不是一个测试用例')
assert 1 == 1
# 测试用例.py
def test_testFunc3():
print('\n我是一个测试用例! in 测试用例.py')
assert 1 == 1
def func3():
print('我不是一个测试用例')
assert 1 == 1
# test_测试模块3.py
def test_testFunc4():
print('\n我是一个测试用例! in test_testFunc4')
assert 1 == 1
def func4():
print('我不是一个测试用例')
assert 1 == 1
class TestClass3(object):
def test_class_func3(self):
print('\n 我是一个类里面的测试用例 in test_class_func3')
assert 1 == 1
def class_func3(self):
print('我是类里面的一个普通函数!')
# 测试用例2.py
def test_testFunc5():
print('\n我是一个测试用例! in test_testFunc5')
assert 1 == 1
def func5():
print('我不是一个测试用例')
assert 1 == 1

下面我们使用cmd命令来执行一下这个项目,看一下究竟会有多少条用例是有效的用例?打开cmd 切换到项目的根目录执行命令 pytest -v

D:\pytest搜索测试用例规则>pytest -v
============================= test session starts =============================
platform win32 -- Python 3.6.4, pytest-3.8.0, py-1.6.0, pluggy-0.7.1 -- c:\python36\python.exe
cachedir: .pytest_cache
metadata: {'Python': '3.6.4', 'Platform': 'Windows-10-10.0.17134-SP0', 'Packages': {'pytest': '3.8.0', 'py': '1.6.0', 'pluggy': '0.7.1'}, 'Plugins': {'metadata': '1.8.0', 'html': '1.20.0', 'allure-adaptor': '1.7.10'}, 'JAVA_HOME': 'C:\\Program Files\\Java\\jdk1.8.0_181'}
rootdir: D:\pytest搜索测试用例规则, inifile:
plugins: metadata-1.8.0, html-1.20.0, allure-adaptor-1.7.10
collected 6 items

test_测试模块3.py::test_testFunc4 PASSED [ 16%]
test_测试模块3.py::TestClass3::test_class_func3 PASSED [ 33%]
测试用例目录1/test_测试模块1.py::test_testFunc1 PASSED [ 50%]
测试用例目录1/test_测试模块2.py::TestClass1::test_class_func1 PASSED [ 66%]
测试用例目录2/test_测试用例1.py::TestClass2::test_class_func2 PASSED [ 83%]
测试用例目录2/test_测试用例1.py::test_testFunc2 PASSED [100%]
========================== 6 passed in 0.59 seconds ===========================

运行结果可以看到一共有6条用例passed,且详细的列出了是哪6条,那么按照我们上面编写的用例其实并不止6条,那么为什么会只运行了6条呢?综合以上的代码结构和我们的执行结果对比,我们应该能发现这样的规律

pytets会从我们当前运行的目录开始查找所有目录,查找以test_开头的文件且文件中所有以test_开头的函数和以Test开头的类和类里面以test_开头的函数为测试用例。这就是为什么上面之运行了6条测试用例!

pytest运行指定测试用例

我们仍然使用上面的项目作为演示(cdm切换到项目的根目录)

1.运行指定目录下的所有用例
我们指定运行测试用例目录1里面的所有用例(pytest -v 测试用例目录1)

D:\pytest搜索测试用例规则>pytest -v 测试用例目录1
============================= test session starts =============================
platform win32 -- Python 3.6.4, pytest-3.8.0, py-1.6.0, pluggy-0.7.1 -- c:\python36\python.exe
cachedir: .pytest_cache
metadata: {'Python': '3.6.4', 'Platform': 'Windows-10-10.0.17134-SP0', 'Packages': {'pytest': '3.8.0', 'py': '1.6.0', 'pluggy': '0.7.1'}, 'Plugins': {'metadata': '1.8.0', 'html': '1.20.0', 'allure-adaptor': '1.7.10'}, 'JAVA_HOME': 'C:\\Program Files\\Java\\jdk1.8.0_181'}
rootdir: D:\pytest搜索测试用例规则, inifile:
plugins: metadata-1.8.0, html-1.20.0, allure-adaptor-1.7.10
collected 2 items
测试用例目录1/test_测试模块1.py::test_testFunc1 PASSED [ 50%]
测试用例目录1/test_测试模块2.py::TestClass1::test_class_func1 PASSED [100%]

========================== 2 passed in 0.05 seconds ===========================
# 这样就会只搜索和指定指定目录下面所有的用

2.运行指定文件中的所有用例

我们指定运行test_测试模块1.py(pytest -v 测试用例目录1/test_测试模块1.py )

D:\pytest搜索测试用例规则>pytest -v 测试用例目录1/test_测试模块1.py
============================= test session starts =============================
platform win32 -- Python 3.6.4, pytest-3.8.0, py-1.6.0, pluggy-0.7.1 -- c:\python36\python.exe
cachedir: .pytest_cache
metadata: {'Python': '3.6.4', 'Platform': 'Windows-10-10.0.17134-SP0', 'Packages': {'pytest': '3.8.0', 'py': '1.6.0', 'pluggy': '0.7.1'}, 'Plugins': {'metadata': '1.8.0', 'html': '1.20.0', 'allure-adaptor': '1.7.10'}, 'JAVA_HOME': 'C:\\Program Files\\Java\\jdk1.8.0_181'}
rootdir: D:\pytest搜索测试用例规则, inifile:
plugins: metadata-1.8.0, html-1.20.0, allure-adaptor-1.7.10
collected 1 item
测试用例目录1/test_测试模块1.py::test_testFunc1 PASSED [100%]

========================== 1 passed in 0.09 seconds ===========================
# 运行指定文件下的所有用例

3.运行指定文件中的测试类

我们指定运行test_测试模块2.py中的测试类Testclass1(pytest -v 测试用例目录1/test_测试模块2.py::TestClass1)

D:\pytest搜索测试用例规则>pytest -v 测试用例目录1/test_测试模块2.py::TestClass1
============================= test session starts =============================
platform win32 -- Python 3.6.4, pytest-3.8.0, py-1.6.0, pluggy-0.7.1 -- c:\python36\python.exe
cachedir: .pytest_cache
metadata: {'Python': '3.6.4', 'Platform': 'Windows-10-10.0.17134-SP0', 'Packages': {'pytest': '3.8.0', 'py': '1.6.0', 'pluggy': '0.7.1'}, 'Plugins': {'metadata': '1.8.0', 'html': '1.20.0', 'allure-adaptor': '1.7.10'}, 'JAVA_HOME': 'C:\\Program Files\\Java\\jdk1.8.0_181'}
rootdir: D:\pytest搜索测试用例规则, inifile:
plugins: metadata-1.8.0, html-1.20.0, allure-adaptor-1.7.10
collected 1 item
测试用例目录1/test_测试模块2.py::TestClass1::test_class_func1 PASSED [100%]

========================== 1 passed in 0.05 seconds ===========================
# 运行指定的测试类中的所有测试用

4.运行指定的测试用例函数

我们指定运行test_testFunc1(pytest -v 测试用例目录1/test_测试模块1.py::test_testFunc1)

D:\pytest搜索测试用例规则>pytest -v 测试用例目录1/test_测试模块1.py::test_testFunc1
============================= test session starts =============================
platform win32 -- Python 3.6.4, pytest-3.8.0, py-1.6.0, pluggy-0.7.1 -- c:\python36\python.exe
cachedir: .pytest_cache
metadata: {'Python': '3.6.4', 'Platform': 'Windows-10-10.0.17134-SP0', 'Packages': {'pytest': '3.8.0', 'py': '1.6.0', 'pluggy': '0.7.1'}, 'Plugins': {'metadata': '1.8.0', 'html': '1.20.0', 'allure-adaptor': '1.7.10'}, 'JAVA_HOME': 'C:\\Program Files\\Java\\jdk1.8.0_181'}
rootdir: D:\pytest搜索测试用例规则, inifile:
plugins: metadata-1.8.0, html-1.20.0, allure-adaptor-1.7.10
collected 1 item
测试用例目录1/test_测试模块1.py::test_testFunc1 PASSED [100%]

========================== 1 passed in 0.03 seconds ===========================

总结

收集用例规则:搜索所有以test_开头的测试文件,以Test开头的测试类,以test_开头的测试函数

执行用例规则:从-v 参数输出的执行信息我们就应该能发现,运行指定的目录下用例 使用命令 pytest 目录/目录 即可;运行指定文件使用 pytest 目录/文件 即可;运行指定类或者函数 使用命令 pytest 目录/文件::类名::函数名 或者 pytest 目录/文件::函数名

搜索用例规则也是我们命名用例文件,测试类,测试函数的规则;执行指定测试用例记住规则即可

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python对象的深拷贝和浅拷贝详解
Aug 25 Python
tensorflow入门之训练简单的神经网络方法
Feb 26 Python
解决Python pandas plot输出图形中显示中文乱码问题
Dec 12 Python
Python supervisor强大的进程管理工具的使用
Apr 24 Python
使用python获取(宜宾市地震信息)地震信息
Jun 20 Python
python 实现GUI(图形用户界面)编程详解
Jul 17 Python
python opencv调用笔记本摄像头
Aug 28 Python
python判断链表是否有环的实例代码
Jan 31 Python
Python库skimage绘制二值图像代码实例
Apr 10 Python
python利用蒙版抠图(使用PIL.Image和cv2)输出透明背景图
Aug 04 Python
python 星号(*)的多种用途
Sep 21 Python
pycharm 的Structure界面设置操作
Feb 05 Python
python取余运算符知识点详解
Jun 27 #Python
如何运行.ipynb文件的图文讲解
Jun 27 #Python
python的pytest框架之命令行参数详解(下)
Jun 27 #Python
python的pytest框架之命令行参数详解(上)
Jun 27 #Python
解决Pycharm后台indexing导致不能run的问题
Jun 27 #Python
解决pycharm运行程序出现卡住scanning files to index索引的问题
Jun 27 #Python
python如何解析配置文件并应用到项目中
Jun 27 #Python
You might like
thinkPHP实现将excel导入到数据库中的方法
2016/04/22 PHP
PHP微信开发之模板消息回复
2016/06/24 PHP
YII2框架中behavior行为的理解与使用方法示例
2020/03/13 PHP
jQuery源码分析-03构造jQuery对象-源码结构和核心函数
2011/11/14 Javascript
浏览器解析js生成的html出现样式问题的解决方法
2012/04/16 Javascript
js定时器(执行一次、重复执行)
2014/03/07 Javascript
JavaScript中的正则表达式简明总结
2014/04/04 Javascript
Node.js(安装,启动,测试)
2014/06/09 Javascript
一个js过滤空格的小函数
2014/10/10 Javascript
js实现透明度渐变效果的方法
2015/04/10 Javascript
JavaScript、tab切换完整版(自动切换、鼠标移入停止、移开运行)
2016/01/05 Javascript
js 获取站点应用名的简单实例
2016/08/18 Javascript
URL的参数中有加号传值变为空格的问题(URL特殊字符)
2016/11/04 Javascript
Angularjs实现分页和分页算法的示例代码
2016/12/23 Javascript
基于ionic实现下拉刷新功能
2018/05/10 Javascript
如何使用JavaScript实现栈与队列
2019/06/24 Javascript
使用JavaScript获取扫码枪扫描得到的条形码的思路代码详解
2020/06/10 Javascript
python使用PythonMagick将jpg图片转换成ico图片的方法
2015/03/26 Python
python实现数值积分的Simpson方法实例分析
2015/06/05 Python
Python绘制七段数码管实例代码
2017/12/20 Python
Python实现删除时保留特定文件夹和文件的示例
2018/04/27 Python
Python对象中__del__方法起作用的条件详解
2018/11/01 Python
解决python中画图时x,y轴名称出现中文乱码的问题
2019/01/29 Python
使用python分析统计自己微信朋友的信息
2019/07/19 Python
python实现简单图书管理系统
2019/11/22 Python
python之语音识别speech模块
2020/09/09 Python
python 实现学生信息管理系统的示例
2020/11/28 Python
美国NBA官方商店:NBA Store
2019/04/12 全球购物
介绍一下SQL Server里面的索引视图
2016/07/31 面试题
后勤人员岗位职责
2013/12/17 职场文书
怎样客观的做好自我评价
2013/12/28 职场文书
奥运会口号
2014/06/13 职场文书
有限责任公司股东合作协议书
2014/12/02 职场文书
2016十一国庆节慰问信
2015/12/01 职场文书
2016年社区中秋节活动总结
2016/04/05 职场文书
vue报错function () { [native code] },无法出现我们想要的内容 Unknown custom element
2022/04/11 Vue.js