当前位置:网站首页>Unittest中的TestSuite和TestRunner
Unittest中的TestSuite和TestRunner
2022-07-04 12:49:00 【大田测试录】
1、Unittest中主要使用TestSuite添加测试用例到测试套件中,再使用Testrunner运行测试用例,并生成测试报告。TestSuite可以指定运行的测试用例,要比main函数运行测试用例要灵活一些。
2、TestSuite和TestRunner用法
unittest_testsuite_testrunner_demo.py
# 导包
import unittest
# 创建测试用例的类
class TestSuiteDemo1(unittest.TestCase):
def test01(self):
print("测试用例:大田03")
def test02(self):
print("测试用例:大田04")
class TestSuiteDemo2(unittest.TestCase):
def test01(self):
print("测试用例:大田01")
def test02(self):
print("测试用例:大田02")
if __name__ == '__main__':
#运行指定测试用例方法:
# 实例化测试套件
suite = unittest.TestSuite()
# 添加测试用例到测试套件当中
suite.addTest(TestSuiteDemo1('test01'))
# 实例化test_runner
runner = unittest.TextTestRunner()
# 使用runner运行测试套件
runner.run(suite)
运行结果:可以看到仅运行了自己想运行的测试用例
3、TestSuite添加测试用例的三种方法
1)suite.addTest:添加单个测试用例
可以看上面的例子即可
2)suite.addTests:添加多个测试用例
# 添加多个测试用例
if __name__ == '__main__':
#运行指定测试用例方法:
# 实例化测试套件
suite = unittest.TestSuite()
# 添加测试用例到测试套件当中:添加单个测试用例方法
# suite.addTest(TestSuiteDemo1('test01'))
#添加多个测试用例
suite.addTests([TestSuiteDemo2('test01'),TestSuiteDemo2('test02')])
# 实例化test_runner
runner = unittest.TextTestRunner()
# 使用runner运行测试套件
runner.run(suite)
运行结果:
3)TestLoader
unittest_testloader_demo.py
import unittest
def testXX(aa):
print("编外的测试用例")
class TestSuiteDemo1(unittest.TestCase):
def test01(self):
print("执行测试用例aaaa")
def test02(self):
print("执行测试用例bbbb")
class TestSuiteDemo2(unittest.TestCase):
def test01(self):
print("执行测试用例xxxx")
def test02(self):
print("执行测试用例yyyy")
class TestSuiteDemo3(unittest.TestCase):
def test01(self):
print("执行接口测试")
def test02(self):
print("执行单元测试")
def test03(self):
print("执行安全测试")
if __name__ == '__main__':
t1 = unittest.TestLoader()
#方法1
suite = t1.discover("./", "*.py") # 使用discover来寻找测试用例
#方法2
# suite = t1.loadTestsFromTestCase(TestSuiteDemo3)
runner = unittest.runner.TextTestRunner() # 实例化runner
runner.run(suite) # 使用runner执行测试套件
边栏推荐
猜你喜欢
205. 同构字符串
Getting started with the go language is simple: go implements the Caesar password
2022年山东省安全员C证考试题库及在线模拟考试
Introduction to reverse debugging PE structure resource table 07/07
近日小结(非技术文)
2022G3锅炉水处理考试题模拟考试题库及模拟考试
Oracle 被 Ventana Research 评为数字创新奖总冠军
Three schemes to improve the efficiency of MySQL deep paging query
The only core indicator of high-quality software architecture
sharding key type not supported
随机推荐
基于链表管理的单片机轮询程序框架
美国土安全部长:国内暴力极端主义是目前美面临的最大恐怖主义威胁之一
C#基础补充
.Net之延迟队列
The only core indicator of high-quality software architecture
C语言程序设计选题参考
MySQL45讲——学习极客时间MySQL实战45讲笔记—— 06 | 全局锁和表锁_给表加个字段怎么有这么多阻碍
【Antd】Antd 如何在 Form.Item 中有 Input.Gourp 时获取 Input.Gourp 的每一个 Input 的value
XML入门三
HAProxy高可用解决方案
[C question set] of VII
The Secretary of Homeland Security warned immigrants "not to embark on a dangerous journey"
OpenHarmony应用开发之如何创建DAYU200预览器
2022年起重机械指挥考试模拟100题模拟考试平台操作
golang fmt.printf()(转)
Fs4056 800mA charging IC domestic fast charging power IC
JVM series - stack and heap, method area day1-2
近日小结(非技术文)
Scrapy 框架学习
One of the solutions for unity not recognizing riders