python基础===利用unittest进行测试用例执行的几种方式
利用python进行测试时,测试用例的加载方式有2种:
一种是通过unittest.main()来启动所需测试的测试模块;
一种是添加到testsuite集合中再加载所有的被测试对象,而testsuit里存放的就是所需测试的用例,下面分别列出3种方法的具体使用方式:
1、通过unittest.main()来执行测试用例的方式:
| import unittest
class UCTestCase(unittest.TestCase): |
2、通过testsuit来执行测试用例的方式:
| import unittest # 执行测试的类 class UCTestCase(unittest.TestCase): def setUp(self): #测试前需执行的操作 ..... def tearDown(self): #测试用例执行完后所需执行的操作 ..... # 测试用例1 def testCreateFolder(self): #具体的测试脚本 ...... # 测试用例2 def testDeleteFolder(self): #具体的测试脚本 ...... if __name__ == "__main__": # 构造测试集 suite = unittest.TestSuite() suite.addTest(UC7TestCase("testCreateFolder")) suite.addTest(UC7TestCase("testDeleteFolder")) # 执行测试 runner = unittest.TextTestRunner() runner.run(suite) |
3、通过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) |
下面针对上述脚本中应用到的unittest模块下的几个成员进行简单的介绍,以便于理解上述代码:
TestCase:所有测试用例的基本类,给一个测试方法的名字,就会返回一个测试用例实例;
TestSuit:组织测试用例的实例,支持测试用例的添加和删除,最终将传递给 testRunner进行测试执行;
TextTestRunner:进行测试用例执行的实例,其中Text的意思是以文本形式显示测试结果。测试的结果会保存到TextTestResult实例中,包括运行了多少测试用例,成功了多少,失败了多少等信息;
TestLoader:用来加载TestCase到TestSuite中的,其中有几个 loadTestsFrom__()方法,就是从各个地方寻找TestCase,创建它们的实例,然后add到TestSuite中,再返回一个TestSuite实例;
本文转自:http://www.51testing.com/html/10/448910-3648852.html
# -*- coding:utf-8 -*-
import unittest class test(unittest.TestCase): def setUp(self):
print 'This is the setup msg' def tearDown(self):
print 'This is the teardown msg' def test1(self):
print 'This is the first testcase' def test2(self):
print 'This is the second testcase' def test3(self):
print 'This is the third testcase' 运行方法一:
if __name__ == '__main__':
unittest.main()
通过unittest.main()执行时,main会调用TextTestRunner中的run来执行 运行方法二:
suite = unittest.TestLoader().loadTestsFromTestCase(test)
unittest.TextTestRunner().run(suite)
通过创建一个测试套,将所有用例全部加载到测试套中,然后直接调用TextTestRunner方法能运行测试套(此时测试套中的用例执行还是无规则,不一定会按用例顺序进行执行) 运行方法三:
suite = unittest.TestSuite()
suite.addTest(test('test3'))
unittest.TextTestRunner().run(suite)
通过创建一个测试套,给测试套中增加一条测试用例,然后再调用TextTestRunner方法单独执行该测试用例 运行方法四:
suite = unittest.TestSuite()
suite.addTest(test('test3'))
suite.addTest(test('test1'))
unittest.TextTestRunner().run(suite)
通过创建一个测试套,给测试套中连续增加两条测试用例,然后再调用TextTestRunner方法执行该测试用例,此时会按照加载用例的顺序进行执行用例,但这种方法有点土,不强大(不建议使用) 运行方法五:
suite = unittest.TestSuite()
suite.addTests([test('test3'),test('test1'),test('test2')])
unittest.TextTestRunner().run(suite)
通过创建一个测试套,直接通过addTests()方法,添加一个用例列表进去,然后再调用TextTestRunner方法执行测试用例,此种方法能让unitest按照想要的执行顺序进行执行用例(对用例执行顺序有要求的此方法比较建议使用) 运行方法六:
def runTest(testcaseclass,testcase=[]):
suite = unittest.TestSuite()
for case in testcase:
suite.addTest(testcaseclass(case))
unittest.TextTestRunner().run(suite)
runTest(test,['test2','test3','test1']) 通过定义一个unitest运行方法,后面执行时直接调用该自定义方法,给上对应的赋值,程序就会按照想 要的执行程序进行执行(此种方法比较好,只要建立一个公共方法,后面在其它模块中直接调用,省去重复敲代码)
本文摘自:
http://www.cnblogs.com/51study/p/9284356.html
如果想使用其它的测试报告:
def suite():
now = time.strftime("%Y-%m-%d %H_%M_%S")
filename = './' + now + 'test_result.html'
fp = open(filename,'wb')
runner = HTMLTestRunner(stream = fp,
title = "快递查询接口测试报告",
description = "测试用例执行情况:") suite = unittest.TestSuite()
suite.addTest(ExpressInquiry("test001_type_valid"))
suite.addTest(ExpressInquiry("test002_type_invalid"))
suite.addTest(ExpressInquiry("test003_id_valid")) #unittest.TextTestRunner().run(suite) runner.run(suite)
fp.close() if __name__ == '__main__':
suite()
测试报告生成模版参考:
自动化测试===unittest和requests接口测试案例,测试快递查询api(二)
python基础===利用unittest进行测试用例执行的几种方式的更多相关文章
- python利用unittest进行测试用例执行的几种方式
利用python进行测试时,测试用例的加载方式有2种: 一种是通过unittest.main()来启动所需测试的测试模块: 一种是添加到testsuite集合中再加载所有的被测试对象,而tes ...
- 基于MVC4+EasyUI的Web开发框架经验总结(12)--利用Jquery处理数据交互的几种方式
在基于MVC4+EasyUI的Web开发框架里面,大量采用了Jquery的方法,对数据进行请求或者提交,方便页面和服务器后端进行数据的交互处理.本文主要介绍利用Jquery处理数据交互的几种方式,包括 ...
- iOS:延时执行的三种方式
延时执行的三种方式:performSelectorXXX方法.GCD中延时函数.创建定时器 第一种方式:NSObject分类当中的方法,延迟一段时间调用某一个方法 @interface NSObj ...
- (转)基于MVC4+EasyUI的Web开发框架经验总结(12)--利用Jquery处理数据交互的几种方式
http://www.cnblogs.com/wuhuacong/p/4085682.html 在基于MVC4+EasyUI的Web开发框架里面,大量采用了Jquery的方法,对数据进行请求或者提交, ...
- iOS中延时执行的几种方式的比较和汇总
本文列举了四种延时执行某函数的方法及其一些区别.假如延时1秒时间执行下面的方法. - (void)delayMethod { NSLog(@"execute"); } 1.perf ...
- IOS中延时执行的几种方式的比较
本文列举了四种延时执行某函数的方法及其一些区别.假如延时1秒时间执行下面的方法. - (void)delayMethod { NSLog(@"execute"); } 1.perf ...
- Python交换a,b两个数值的三种方式
# coding:utf-8 a = 1 b = 2 # 第一种方式 # t = a # 临时存放变量值 # a = b # b = t # 第二种方式 # a = a + b # a的值已经不是原始 ...
- shell script执行的几种方式
编写一个shell脚本test.sh,内容如下 a='测试执行方式' echo $a 方式1 使用路径的方式执行 chmod a+x test.sh ./test.sh 执行结果如下 当脚本执行之后, ...
- js中页面加载完成后执行的几种方式及执行顺序
1:使用jQuery的$(function){}; 2:使用jquery的$(document).ready(function(){});前两者本质上没有区别,第1种是第2种的简写方式.两个是docu ...
随机推荐
- Stream My Contest UVA - 11865(带权最小树形图+二分最小值最大化)
#include <iostream> #include <cstdio> #include <sstream> #include <cstring> ...
- THUSC2018滚粗记
THUSC2018滚粗记 前言 大家好,我是\(yyb\),我的博客里又多了一篇滚粗记, 我记得我原来在某篇滚粗记中曾经写过 \(yyb\)还会写很多很多次滚粗记才会有一篇不是滚粗记的东西. 看起来这 ...
- BZOJ4408 [Fjoi 2016]神秘数 【主席树】
题目链接 BZOJ4408 题解 假如我们已经求出一个集合所能凑出连续数的最大区间\([1,max]\),那么此时答案为\(max + 1\) 那么我们此时加入一个数\(x\),假若\(x > ...
- POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询)
POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询) 题意分析 注意一下懒惰标记,数据部分和更新时的数字都要是long long ,别的没什么大 ...
- 基础学习笔记之opencv(24):imwrite函数的使用
http://www.cnblogs.com/tornadomeet/archive/2012/12/26/2834336.html 前言 OpenCV中保存图片的函数在c++版本中变成了imwrit ...
- Lab颜色空间
原文:http://blog.csdn.net/carson2005/article/details/7200440 同RGB颜色空间相比,Lab是一种不常用的色彩空间.它是在1931年国际照明委员会 ...
- Codeforces Round #299 (Div. 2)A B C 水 dfs 二分
A. Tavas and Nafas time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- git untrack file
git update-index should do what you want This will tell git you want to start ignoring the changes t ...
- pdf 下载整理
pdf下载整理: using System; using System.Collections.Generic; using System.Linq; using System.Web; using ...
- 10.Android UiAutomator Junit 断言函数的使用
一.断言函数介绍 1.断言函数: 确定被测试的方法是否按照预期的效果正常工作 比如说: if (假设成立){ 通过测试 }else{ 报错并终止当前用例测试 } 2.断言函数用例结构: 一个完整的测试 ...