当前位置:网站首页>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执行测试套件
边栏推荐
猜你喜欢
2022危险化学品经营单位主要负责人练习题及模拟考试
Node の MongoDB安装
Oracle 被 Ventana Research 评为数字创新奖总冠军
Redis —— How To Install Redis And Configuration(如何快速在 Ubuntu18.04 与 CentOS7.6 Linux 系统上安装 Redis)
英视睿达冲刺科创板:年营收4.5亿 拟募资9.79亿
中邮科技冲刺科创板:年营收20.58亿 邮政集团是大股东
免费、好用、强大的轻量级笔记软件评测:Drafts、Apple 备忘录、Flomo、Keep、FlowUs、Agenda、SideNote、Workflowy
Flet tutorial 03 basic introduction to filledbutton (tutorial includes source code) (tutorial includes source code)
華昊中天沖刺科創板:年虧2.8億擬募資15億 貝達藥業是股東
Commvault 和 Oracle 合作,在 Oracle 云上提供 Metallic数据管理即服务
随机推荐
Lick the dog until the last one has nothing (state machine)
C foundation in-depth study I
1200. Minimum absolute difference
字节面试算法题
Animation and transition effects
FS7867S是一款应用于数字系统供电电源电压监控的电压检测芯片
Commvault 和 Oracle 合作,在 Oracle 云上提供 Metallic数据管理即服务
担心“断气” 德国正修改《能源安全法》
#yyds干货盘点# 解决名企真题:连续最大和
Xilinx/system-controller-c/boardui/ unable to connect to the development board, the solution of jamming after arbitrary operation
C basic supplement
The Secretary of Homeland Security warned immigrants "not to embark on a dangerous journey"
Secretary of Homeland Security of the United States: domestic violent extremism is one of the biggest terrorist threats facing the United States at present
2022G3锅炉水处理考试题模拟考试题库及模拟考试
MySQL45讲——学习极客时间MySQL实战45讲笔记—— 06 | 全局锁和表锁_给表加个字段怎么有这么多阻碍
程序员的焦虑
golang fmt.printf()(转)
E-week finance | Q1 the number of active people in the insurance industry was 86.8867 million, and the licenses of 19 Payment institutions were cancelled
go语言中的文件创建,写入,读取,删除(转)
Fs7867s is a voltage detection chip used for power supply voltage monitoring of digital system