python中pytest收集用例规则与运行指定用例详解
作者:linux超 发布时间:2021-12-01 14:23:21
前言
上篇文章相信大家已经了解了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 目录/文件::函数名
搜索用例规则也是我们命名用例文件,测试类,测试函数的规则;执行指定测试用例记住规则即可
来源:https://www.cnblogs.com/linuxchao/p/linuxchao-pytest-3.html
猜你喜欢
- 01-初心缘由最近在研究语音识别方向,看了很多的语音识别的资料和文章,了解了一下语音识别的前世今生,其中包含了很多算法的演变,目前来说最流行
- 原来工作中曾经碰到过UL列表里一些异常的表现,加上昨天看到了http://bbs.blueidea.com/thread-2984871-1
- 最终效果如下图,右侧灰边看相对位置,版权所有谨防假冒:去年曾针对有时间先后的翻页记录了思考片段。之后没来得及调整一直是默认和插件并用,虽然难
- Array.prototype中定义了很多操作数组的方法,下面介绍ECMAScript3中的一些方法1.Array.join()方法该方法将
- 本文介绍python如何进行截图保存的几种方法,在测试过程中,是有必要截图,特别是遇到错误的时候进行截图。结合Python其它模块如time
- 本文实例为大家分享了python实现人民币转大写人民币的具体代码,供大家参考,具体内容如下直接上代码:# -*- coding: utf-8
- Pandas使用这些函数处理缺失值:isnull和notnull:检测是否是空值,可用于df和seriesdropna:丢弃、删除缺失值ax
- var getWindow = function(obj) { var&nbs
- 本文实例讲述了python中as用法。分享给大家供大家参考。具体分析如下:import some # some 为一个模组如果想要改变被导入
- <?php /********************************************** *&n
- 数据库镜像是将数据库事务处理从一个数据库移动到不同环境中的另一个数据库中。镜像的拷贝是一个备用的拷贝,不能直接访问,它只用在错误恢复的情况下
- 本文主要包括三大方面,大家仔细学习。1、导航栏中的表单导航栏中的表单不是使用 Bootstrap 表单 章节中所讲到的默认的 class,它
- php中-> 、=>、::、$this->四种符号在代码中很常见,使用很广泛。这篇文章主要介绍了php中-> 、=&
- 昨天还和裕波沟通我没有准备ppt,想以另一种互动的方式来交流。后来想想,每个人都准备了,我不准备也不太好意思,呵呵。今天在webrebuil
- 我们知道IE6是不支持透明的PNG的,这无疑限制了网页设计的发挥空间.然而整个互联网上解决这个IE6的透明PNG的方案也是多不胜数,从使用I
- 如果使用注释的方法得当的话,为你的CSS文件添加注释可以在开发过程中给予你和其他人很大的帮助。最常见的是为CSS样式规则添加提示信息,不过使
- 前言上一篇介绍了服务端流式RPC,客户端发送请求到服务器,拿到一个流去读取返回的消息序列。 客户端读取返回的流的数据。本篇将介绍客户端流式R
- documentFragment 是一個無父對象的document對象.他支持以下DOM2方法:appendChild, cloneNode
- 在 Go 语言中,map 是一种非常常见的数据类型,它可以用于快速地检索数据。Go 语言中的 map 与其他编程语言中的类似的数据类型相比,
- 日志日志是跟踪软件运行时所发生的事件的一种方法。软件开发者在代码中调用日志函数,表明发生了特定的事件。事件由描述性消息描述,该描述性消息可以