Python unittest discover批量执行代码实例
作者:多测师_郑sir 时间:2023-08-14 14:05:37
代码如下
import unittest
dir = "D:\\work_doc\\pycharm2\\python_Basics" #自动化用例所存放的路径
suit = unittest.defaultTestLoader.discover(dir,pattern="XFS*.py",top_level_dir=None) #匹配出需要执行的py文件
runner = unittest.TextTestRunner() #TextTestRunner类实例化,目的是为了调用TextTestRunner下的run()方法
runner.run(suit) #调用TextTestRunner下的run()方法执行用例
import unittest
def run_all_cases(file_path,file):
discover = unittest.defaultTestLoader.discover(file_path,pattern=file)
return discover
if __name__ == '__main__':
runner = unittest.TextTestRunner()
runner.run(run_all_cases(
"D:\work_doc\pycharm2\python_selenium",
"test*.py"))
解释
1.discover方法加载测试用例
discover 加载到的用例是一个 list 集合,需要重新写入到一个 list 对象 testcase 里,这样就可以用 unittest 里面的 TextTestRunner 这里类的 run 方法去执行。
TestLoader 该类根据各种标准加载测试用例,并将它们返回给测试套件。正常情况下,不需要创建这个类的实例。unittest 提供了可以共享的 defaultTestLoader 类,可以使用其子类和方法创建实例,discover() 就是其中之一。
2.discover方法里面有三个参数
-case_dir:这个是待执行用例的目录。
-pattern:这个是匹配脚本名称的规则,test*.py 意思是匹配test开头的所有脚本。
-top_level_dir:这个是顶层目录的名称,一般默认等于 None 就行了。
来源:https://www.cnblogs.com/ZhengYing0813/p/11696820.html
标签:Python,unittest,discover,批量
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
关于SQL Update的四种常见写法
2024-01-25 09:40:07
Go语言实现socket实例
2024-02-04 20:17:30
Python办公自动化从Excel中计算整理数据并写入Word
2021-12-19 03:22:35
![](https://img.aspxhome.com/file/2023/9/97409_0s.png)
django2+uwsgi+nginx上线部署到服务器Ubuntu16.04
2022-01-15 17:03:09
![](https://img.aspxhome.com/file/2023/8/97678_0s.png)
Python pygame 项目实战事件监听
2023-05-31 21:33:20
![](https://img.aspxhome.com/file/2023/8/112908_0s.png)
Python中常用的内置函数
2023-01-06 08:15:30
![](https://img.aspxhome.com/file/2023/8/79748_0s.png)
深入理解Python3 内置函数大全
2022-06-27 22:11:36
![](https://img.aspxhome.com/file/2023/1/87591_0s.png)
python获取字符串中的email
2021-04-23 21:23:29
![](https://img.aspxhome.com/file/2023/7/86677_0s.png)
python面试题小结附答案实例代码
2021-05-04 13:13:08
浅谈Python面向对象编程oop思想心得
2021-10-25 16:45:29
表格头固定而列可滚动的效果
2020-08-11 21:28:41
Python 在字符串中加入变量的实例讲解
2023-01-27 10:51:21
python 环境安装及编辑器配置方法小结
2021-09-13 02:39:25
![](https://img.aspxhome.com/file/2023/2/102992_0s.png)
SQL对数据进行按月统计或对数据进行按星期统计的实例代码
2024-01-28 08:41:47
![](https://img.aspxhome.com/file/2023/8/116288_0s.png)
MySQL 8忘记密码的最佳处理方式浅析
2024-01-28 17:44:32
![](https://img.aspxhome.com/file/2023/4/102374_0s.png)
在python中计算ssim的方法(与Matlab结果一致)
2023-08-19 03:33:21
Python pytorch实现绘制一维热力图
2022-04-03 21:09:14
![](https://img.aspxhome.com/file/2023/0/97270_0s.png)
一文带你深入探索Golang操作mongodb的方法
2024-05-09 09:46:14
InnoDB数据库死锁问题处理
2024-01-19 13:30:59
MySQL性能优化
2024-01-18 11:14:00