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

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

这篇文章主要介绍了python中pytest收集用例规则与运行指定用例详解,天会讲解一下pytest是如何收集我们写好的用例?我们又有哪些方式来运行单个用例或者批量运行用例呢,需要的朋友可以参考下

前言

上篇文章相信大家已经了解了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 目录/文件::函数名

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持"软件测试pytest"。

相关文章
|
12天前
|
算法 Python
请解释Python中的关联规则挖掘以及如何使用Sklearn库实现它。
使用Python的mlxtend库,可以通过Apriori算法进行关联规则挖掘。首先导入TransactionEncoder和apriori等模块,然后准备数据集(如购买行为列表)。对数据集编码并转换后,应用Apriori算法找到频繁项集(设置最小支持度)。最后,生成关联规则并计算置信度(设定最小置信度阈值)。通过调整这些参数可以优化结果。
34 9
|
3天前
|
机器学习/深度学习 算法 数据挖掘
【Python机器学习专栏】关联规则学习:Apriori算法详解
【4月更文挑战第30天】Apriori算法是一种用于关联规则学习的经典算法,尤其适用于购物篮分析,以发现商品间的购买关联。该算法基于支持度和置信度指标,通过迭代生成频繁项集并提取满足阈值的规则。Python中可借助mlxtend库实现Apriori,例如处理购物篮数据,设置支持度和置信度阈值,找出相关规则。
|
4天前
|
测试技术 Python
python运行集成测试
【4月更文挑战第22天】
7 1
|
4天前
|
Linux Python Windows
python安装pytest
【4月更文挑战第22天】
18 5
|
5天前
|
运维 监控 Serverless
Serverless 应用引擎产品使用之阿里函数计算中在自定义环境下用debian10运行django,用官方层的python3.9,配置好环境变量后发现自定义层的django找不到了如何解决
阿里云Serverless 应用引擎(SAE)提供了完整的微服务应用生命周期管理能力,包括应用部署、服务治理、开发运维、资源管理等功能,并通过扩展功能支持多环境管理、API Gateway、事件驱动等高级应用场景,帮助企业快速构建、部署、运维和扩展微服务架构,实现Serverless化的应用部署与运维模式。以下是对SAE产品使用合集的概述,包括应用管理、服务治理、开发运维、资源管理等方面。
14 3
|
5天前
|
XML 测试技术 持续交付
python运行集成测试
【4月更文挑战第21天】
13 2
|
8天前
|
测试技术 持续交付 Python
Python测试架构pytest
【4月更文挑战第19天】pytest 是一个强大且灵活的 Python 测试框架,它可以帮助你编写高效且可维护的测试。通过遵循上述基本架构指南,你可以开始使用 pytest 来提高你的 Python 项目的质量和可靠性。
6 2
|
9天前
|
算法 数据可视化 搜索推荐
数据分享|Python用Apriori算法关联规则分析亚马逊购买书籍关联推荐客户和网络图可视化
数据分享|Python用Apriori算法关联规则分析亚马逊购买书籍关联推荐客户和网络图可视化
31 11
|
11天前
|
前端开发 测试技术 C++
Python自动化测试面试:unittest、pytest与Selenium详解
【4月更文挑战第19天】本文聚焦Python自动化测试面试,重点讨论unittest、pytest和Selenium三大框架。unittest涉及断言、TestSuite和覆盖率报告;易错点包括测试代码冗余和异常处理。pytest涵盖fixtures、参数化测试和插件系统,要注意避免过度依赖unittest特性。Selenium的核心是WebDriver操作、等待策略和测试报告生成,强调智能等待和元素定位策略。掌握这些关键点将有助于提升面试表现。
22 0
|
15天前
|
机器学习/深度学习 数据可视化 算法
PYTHON在线零售数据关联规则挖掘APRIORI算法数据可视化
PYTHON在线零售数据关联规则挖掘APRIORI算法数据可视化
28 4