python利用unittest测试框架组织测试用例的5种方法
利用unittest测试框架可以编写测试用例,执行方式分两大类:利用main方法和利用testsuite,其中利用测试套件来组织测试用例可以有4种写法。
在此之前,先了解几个概念
- TestCase:所有测试用例的基本类,给一个测试方法的名字,就会返回一个测试用例实例;
- TestSuite:组织测试用例的实例,支持测试用例的添加和删除,最终将传递给 testRunner进行测试执行;
- TextTestRunner:进行测试用例执行的实例,其中Text的意思是以文本形式显示测试结果。测试的结果会保存到TextTestResult实例中,包括运行了多少测试用例,成功了多少,失败了多少等信息;
- TestLoader:用来加载TestCase到TestSuite中的,其中有几个 loadTestsFrom__()方法,就是从各个地方寻找TestCase,创建它们的实例,然后add到TestSuite中,再返回一个TestSuite实例;
- defaultTestLoader.discover:将discover方法筛选出来的用例,循环添加到测试套件中,打印出的用例信息会递增
方法一:通过unittest.main()来执行测试用例的方式
import unittest class TestStringMethods(unittest.TestCase): def setUp(self):
unittest.TestCase.setUp(self) def tearDown(self):
unittest.TestCase.tearDown(self) def test_upper(self):
self.assertEqual('foo'.upper(), 'FOO') def test_isupper(self):
self.assertTrue('FOO'.isupper())
self.assertFalse('Foo'.isupper()) def test_split(self):
s = 'hello world'
self.assertEqual(s.split(), ['hello','world'])
with self.assertRaises(TypeError):
s.split(2) if __name__ == "__main__":
#import sys;sys.argv = ['', 'Test.testName']
unittest.main()
unittest.main()会去查找所有以test开头的方法,并把它们当做一个个测试用例
方法二:用测试套件
import unittest class TestStringMethods(unittest.TestCase): def setUp(self):
unittest.TestCase.setUp(self) def tearDown(self):
unittest.TestCase.tearDown(self) def test_upper(self):
self.assertEqual('foo'.upper(), 'FOO') def test_isupper(self):
self.assertTrue('FOO'.isupper())
self.assertFalse('Foo'.isupper()) def test_split(self):
s = 'hello world'
self.assertEqual(s.split(), ['hello','world'])
with self.assertRaises(TypeError):
s.split(2) def suite(): suite = unittest.TestSuite()
suite.addTest(TestStringMethods('test_upper'))
suite.addTest(TestStringMethods('test_isupper'))
unittest.TextTestRunner(verbosity=2).run(suite) if __name__ == "__main__": suite()
#import sys;sys.argv = ['', 'Test.testName']
# unittest.main()
方法三:用TestLoader
import unittest
class TestCase1(unittest.TestCase):
#def setUp(self):
#def tearDown(self):
def testCase1(self):
print 'aaa'
def testCase2(self):
print 'bbb' class TestCase2(unittest.TestCase):
#def setUp(self):
#def tearDown(self):
def testCase1(self):
print 'aaa1'
def testCase2(self):
print 'bbb1' if __name__ == "__main__":
#此用法可以同时测试多个类
suite1 = unittest.TestLoader().loadTestsFromTestCase(TestCase1)
suite2 = unittest.TestLoader().loadTestsFromTestCase(TestCase2)
suite = unittest.TestSuite([suite1, suite2])
unittest.TextTestRunner(verbosity=2).run(suite)
方法四:测试套件的一种变性,和方法二差不多
import unittest class TestStringMethods(unittest.TestCase): def setUp(self):
unittest.TestCase.setUp(self) def tearDown(self):
unittest.TestCase.tearDown(self) def test_upper(self):
self.assertEqual('foo'.upper(), 'FOO') def test_isupper(self):
self.assertTrue('FOO'.isupper())
self.assertFalse('Foo'.isupper()) def test_split(self):
s = 'hello world'
self.assertEqual(s.split(), ['hello','world'])
with self.assertRaises(TypeError):
s.split(2) def suite(): tests = ['test_upper','test_isupper']
suite = unittest.TestSuite(map(TestStringMethods,tests))
unittest.TextTestRunner(verbosity=2).run(suite) if __name__ == "__main__": suite()
#import sys;sys.argv = ['', 'Test.testName']
# unittest.main()
方法五:用discover
import unittest class TestStringMethods(unittest.TestCase): def setUp(self):
unittest.TestCase.setUp(self) def tearDown(self):
unittest.TestCase.tearDown(self) def test_upper(self):
self.assertEqual('foo'.upper(), 'FOO') def test_isupper(self):
self.assertTrue('FOO'.isupper())
self.assertFalse('Foo'.isupper()) def test_split(self):
s = 'hello world'
self.assertEqual(s.split(), ['hello','world'])
with self.assertRaises(TypeError):
s.split(2) def suite(): suite=unittest.TestSuite()
discover=unittest.defaultTestLoader.discover(casepath, pattern='test_*.py', top_level_dir=None)
#将discover方法筛选出来的用例,循环添加到测试套件中,打印出的用例信息会递增
for test_case in discover:
suite.addTests(test_case)
print(suite)
return suite if __name__ == "__main__": all_test_cases = suite()
unittest.TextTestRunner().run(all_test_cases)
#import sys;sys.argv = ['', 'Test.testName']
# unittest.main()
python利用unittest测试框架组织测试用例的5种方法的更多相关文章
- Python单元测试unittest测试框架
本文的主题是自动化测试框架的实现,在实现之前,先了解一下关于unittest模块的相关知识: Python中有一个自带的单元测试框架是unittest模块,用它来做单元测试,它里面封装好了一些校验返回 ...
- 基于python的unittest测试框架集成到jenkins(Mac)
1.jenkins部分 1.1 安装jenkins jenkins下载地址:https://jenkins.io/download/ 安装步骤,疯狂点击下一步 1.2 打开jenkins服务 在浏览器 ...
- 基于Python的接口自动化-unittest测试框架和ddt数据驱动
引言 在编写接口自动化用例时,我们一般针对一个接口建立一个.py文件,一条接口测试用例封装为一个函数(方法),但是在批量执行的过程中,如果其中一条出错,后面的用例就无法执行,还有在运行大量的接口测试用 ...
- Python接口测试实战3(下)- unittest测试框架
如有任何学习问题,可以添加作者微信:lockingfree 课程目录 Python接口测试实战1(上)- 接口测试理论 Python接口测试实战1(下)- 接口测试工具的使用 Python接口测试实战 ...
- Python 下的unittest测试框架
unittest测试框架,直接上图吧: data:数据:主要格式为CSV:读取方式:csv.reade: public:封装的模块:通用的模块单独封装,所需参数设置为变量: testcase:测试用例 ...
- <day002>Selenium基本操作+unittest测试框架
任务1:Selenium基本操作 from selenium import webdriver # 通用选择 from selenium.webdriver.common.by import By # ...
- python获取字母在字母表对应位置的几种方法及性能对比较
python获取字母在字母表对应位置的几种方法及性能对比较 某些情况下要求我们查出字母在字母表中的顺序,A = 1,B = 2 , C = 3, 以此类推,比如这道题目 https://project ...
- python实例编写(6)--引入unittest测试框架,构造测试集批量测试(以微信统一管理平台为例)
---恢复内容开始--- 一.python单元测试实例介绍 unittest框架又叫PyUnit框架,是python的单元测试框架. 先介绍一个普通的单元测试(不用unittest框架)的实例: 首先 ...
- python利用unittest进行测试用例执行的几种方式
利用python进行测试时,测试用例的加载方式有2种: 一种是通过unittest.main()来启动所需测试的测试模块: 一种是添加到testsuite集合中再加载所有的被测试对象,而tes ...
随机推荐
- Distributed3:SQL Server 分布式数据库性能测试
我在三台安装SQL Server 2012的服务器上搭建分布式数据库,把产品环境中一年近1.4亿条数据大致均匀地存储在这三台服务器中,每台Server 存储4个月的数据,物理机的系统配置基本相同:内存 ...
- 网络知识 ACL NAT IPv6
第1章 ACL 访问控制列表 访问控制表(Access Control List,ACL),又称存取控制串列,是使用以访问控制矩阵为基础的访问控制方法,每一个对象对应一个串列主体. 访问控制表描述每一 ...
- 2.5星|《AI进化论》:疑似基于PPT与公关稿整理汇编而成
AI进化论·解码人工智能商业场景与案例 全书是目前AI在一些热门领域的应用的介绍,包括各行业内AI可以实现的功能.现有相关公司的具体业务等.对各公司的介绍仅限于能实现什么业务,具体做的怎么样,有什么优 ...
- (1) Python 数据类型功能
1.int 将字符串转化为数字 a="123" print(type(a),a) b=int(a) print(type(b),b) num="0011" ...
- CHAPTER 38 Reading ‘the Book of Life’ The Human Genome Project 第38章 阅读生命之书 人体基因组计划
CHAPTER 38 Reading ‘the Book of Life’ The Human Genome Project 第38章 阅读生命之书 人体基因组计划 Humans have about ...
- openstack horizon开发第一天
horizon插件构造 创建一个dashboardmkdir opesntack_dashboard/dashboards/mydashboardpython manage.py startdash ...
- 【Docker】第五篇 Docker 数据管理
一.基本介绍 数据管理的原因:Docker中的容器一旦删除,容器本身的rootfs文件系统就会被删除,容器中的所有数据就会被删除.为了对一些需要持久化的数据,不随容器删除而删除,所以我们可以通过多个容 ...
- 下一个时代的发展架构竟然是它!FaaaaaaaaS到底是个啥?
欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文由腾讯云serverless团队发表于云+社区专栏 导读:2018年7月6 - 7日,一年一度的技术圈盛会ArchSummit全球架构师 ...
- Mysql Order By注入总结
何为order by 注入 本文讨论的内容指可控制的位置在order by子句后,如下order参数可控"select * from goods order by $_GET['order' ...
- pycharm连接服务器
python其他知识目录 1. pycharm当做xshell等远程工具,远程连接服务器步骤: 2.pycharm结合Linux服务器进行代码学习: 2.2使用pycharm远程在服务器上修改和执行代 ...