前言

上篇文章相信大家已经了解了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条呢?综合以上的代码结构和我们的执行结果对比,我们应该能发现这样的规律

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

Pytest运行指定测试用例

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

运行指定目录下的所有用例

我们指定运行测试用例目录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 ===========================
# 这样就会只搜索和指定指定目录下面所有的用

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

我们指定运行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 ===========================
# 运行指定文件下的所有用例

运行指定文件中的测试类

我们指定运行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 ===========================
# 运行指定的测试类中的所有测试用

运行指定的测试用例函数

我们指定运行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之收集用例规则与运行指定用例的更多相关文章

  1. pytest 运行指定用例

    pytest运行指定用例 随着软件功能的增加,模块越来越多,也意味用例越来越多,为了节约执行时间,快速得到测试报告与结果,在工作中可以通过运行指定用例,达到快速执行用例 例子目录 spec_sub1_ ...

  2. windows ping 某个网段,不能运行指定的软件

    windows ping 某个网段,不能运行指定的软件 :begin @echo OFF color 0a Title Net Test Tool by:HRuinger Mode con cols= ...

  3. unittest 运行slenium(四)---通过指定用例的形式运行用例

    一: 说明 跟数据驱动唯一的区别为用例数据获取时,及运行方式不同. 其它都基本相同,可参考https://www.cnblogs.com/xiaodingdong/p/11753220.html 二: ...

  4. pytest的断言、跳过、运行的按需要处理

    def test_one(): assert 1==1 assert 1!=2 assert {'name':'linda','age':19}=={'name':'linda','age':190} ...

  5. pytest文档59-运行未提交git的用例(pytest-picked)

    前言 我们每天写完自动化用例后都会提交到 git 仓库,随着用例的增多,为了保证仓库代码的干净,当有用例新增的时候,我们希望只运行新增的未提交 git 仓库的用例. pytest-picked 插件可 ...

  6. pytest文档50-命令行参数--durations统计用例运行时间

    前言 写完一个项目的自动化用例之后,发现有些用例运行较慢,影响整体的用例运行速度,于是领导说找出运行慢的那几个用例优化下. --durations 参数可以统计出每个用例运行的时间,对用例的时间做个排 ...

  7. runas/cpau/lsrunase使用小结(以管理员运行指定程序)

    企业环境中,为了安全起见一般都没有赋予域用户或者企业的PC客户端用户管理员权限. 但偶尔会有个别的程序一定需要管理员身份才能执行,如财务某些程序或专业的应用程序.那么如何不赋予用户管理员权限及密码但又 ...

  8. WINDOWS登录系统之前(欢迎界面)运行指定程序脚本服务

    方法一:创建可在系统登录之前运行的服务 PS:需要用到两个程序—Srvany和Instsrv,点击 http://pan.baidu.com/share/link?shareid=4111024491 ...

  9. maven test 运行 指定类或方法 打包 mvn clean assembly:assembly

    >mvn test -Dtest=[ClassName] 运行测试类中指定的方法:(这个需要maven-surefire-plugin:2.7.3以上版本才能支持)   >mvn test ...

随机推荐

  1. springboot+activemq中引入重发机制

    一.简介 在使用activemq消息中间件进行消息队列传输时,总会由于各种原因导致消息失败. 一个经典的场景是一个生成者向Queue中发消息,里面包含了一组邮件地址和邮件内容.而消费者从Queue中将 ...

  2. Java NIO Buffer缓冲区

    原文链接:http://tutorials.jenkov.com/java-nio/buffers.html Java NIO Buffers用于和NIO Channel交互.正如你已经知道的,我们从 ...

  3. 使用jmeter 进行接口的性能测试

    1.启动jmeter:在bin下以管理员身份运行jmeter.bat,启动jmeter 2. 创建测试计划: 默认启动jmeter时会加载一个测试技术模板,保存测试计划:修改名称为Apitest,点击 ...

  4. CSS实现Tab布局

    一.布局方式 1.内容与tab分离 <div class="container"> <div class="tab-content"> ...

  5. Java 精简Jre jar打包成exe

    #开始 最近几天都在忙一个事情,那就是尝试精简jre,我想不明白为什么甲骨文官方不出exe打包工具... 网络上精简jre的文章很多,但是原创的似乎没几个,绝大多数都是转发同一个博客, 这里借鉴了不少 ...

  6. LeetCode Javascript实现 258. Add Digits 104. Maximum Depth of Binary Tree 226. Invert Binary Tree

    258. Add Digits Digit root 数根问题 /** * @param {number} num * @return {number} */ var addDigits = func ...

  7. 系列博文-Three.js入门指南(张雯莉)-网格 setInterval方法 requestAnimationFrame方法 使用stat.js记录FPS

    第6章 动画 在本章之前,所有画面都是静止的,本章将介绍如果使用Three.js进行动态画面的渲染.此外,将会介绍一个Three.js作者写的另外一个库,用来观测每秒帧数(FPS). CSS3动画那么 ...

  8. Python中使用MongoEngine1

    pymongo来操作MongoDB数据库,但是直接把对于数据库的操作代码都写在脚本中,这会让应用的代码耦合性太强,而且不利于代码的优化管理 一般应用都是使用MVC框架来设计的,为了更好地维持MVC结构 ...

  9. Python 时间模块

    模块分类 标准库 python解释器自带的, sys, os等 开源模块 比如selenium中的测试框架 自定义模块 比如自己封装的测试框架 时间格式 时间戳 当前时间减去1970年1月1日00:0 ...

  10. 绕过token

    网站搭好了,下一步的目标就是直奔后台.因为一般前端在未登录的情况下只有查的功能.咱们的目标是增删改. 看到有添加功能时,先别着急的直接黑盒测试.先看看有没有防护 ######## 查看源码,搜索tok ...