当前位置:网站首页>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执行测试套件边栏推荐
- Interviewer: what is the internal implementation of hash data type in redis?
- Web知识补充
- 【C 题集】of Ⅶ
- Fisher信息量检测对抗样本代码详解
- Understanding and difference between viewbinding and databinding
- C语言程序设计
- Fs4056 800mA charging IC domestic fast charging power IC
- 英视睿达冲刺科创板:年营收4.5亿 拟募资9.79亿
- One of the solutions for unity not recognizing riders
- 以房抵债能否排除强制执行
猜你喜欢

30:第三章:开发通行证服务:13:开发【更改/完善用户信息,接口】;(使用***BO类承接参数,并使用了参数校验)

2022g3 boiler water treatment examination question simulation examination question bank and simulation examination

Getting started with the go language is simple: go implements the Caesar password

華昊中天沖刺科創板:年虧2.8億擬募資15億 貝達藥業是股東

苹果5G芯片研发失败:继续依赖高通,还要担心被起诉?

【Antd】Antd 如何在 Form.Item 中有 Input.Gourp 时获取 Input.Gourp 的每一个 Input 的value

SCM polling program framework based on linked list management

. Net delay queue

markdown 语法之字体标红

嵌入式编程中五个必探的“潜在错误”
随机推荐
Haproxy high availability solution
golang fmt.printf()(转)
Ws2811 m is a special circuit for three channel LED drive and control, and the development of color light strip scheme
Secretary of Homeland Security of the United States: domestic violent extremism is one of the biggest terrorist threats facing the United States at present
205. 同构字符串
Redis —— How To Install Redis And Configuration(如何快速在 Ubuntu18.04 与 CentOS7.6 Linux 系统上安装 Redis)
MongoDB常用28条查询语句(转)
Node の MongoDB安装
读取 Excel 表数据
硬件基础知识-二极管基础
Node の MongoDB 安装
The Secretary of Homeland Security warned immigrants "not to embark on a dangerous journey"
Interviewer: what is the internal implementation of hash data type in redis?
中邮科技冲刺科创板:年营收20.58亿 邮政集团是大股东
苹果5G芯片研发失败:继续依赖高通,还要担心被起诉?
mac redis安装与使用,连接远程服务器 redis
Introduction to XML I
Redis - how to install redis and configuration (how to quickly install redis on ubuntu18.04 and centos7.6 Linux systems)
华昊中天冲刺科创板:年亏2.8亿拟募资15亿 贝达药业是股东
Fisher信息量检测对抗样本代码详解