笔记-unittest实战
笔记-unittest实战
1. 框架图

2. 用例
编写自己的测试用例类,继承于基类
class ApiTestCase(unittest.TestCase):
setUp方法会在每一个方法执行前执行
tearDown方法则是在每次方法执行后执行
@unittest.skip(‘跳过用例的测试,《原因》’)
测试方法名都以test_开头
测试方法执行顺序按测试方法名字典排序
简单的执行可以使用unittest.main()
import unittest
import requests
from proxypool.Api.proxy_api import run
from proxypool.Util.getconfig import config
from threading import Thread
from time import sleep
def before_test():
print('starting
webapi...')
thread = Thread(target=run)
thread.daemon = True
thread.start()
print('webapi is
running...')
class ApiTestCase(unittest.TestCase):
# 主页面测试
def test_webapi_mainpage(self):
url = 'http://localhost:8080/'
response =
requests.get(url)
self.assertEqual(response.status_code,
200, 'main page test failed.')
#print(response.text)
#
get页面测试
def test_webapi_get(self):
url = 'http://localhost:8080/get'
response =
requests.get(url)
self.assertEqual(response.status_code,
200, 'get page test failed.')
#
print(response.text)
#
delete页面测试
def test_webapi_delete(self):
url = 'http://localhost:8080/delete?proxy=5.6.4.3:453'
response =
requests.get(url)
self.assertEqual(response.status_code,
200, 'delete page test failed.')
#
print(response.text)
#
status页面测试
def test_webapi_status(self):
url = 'http://localhost:8080/status'
response =
requests.get(url)
self.assertEqual(response.status_code,
200, 'satus page test failed.')
#
print(response.text)
def start_test():
before_test()
print('test
start')
unittest.main()
print('test
end.')
if __name__ == '__main__':
start_test()
注意:setUp和tearDown解决了单个测试方法的环境准备及清理;
但有一些环境是所有测试方法共用的,放在setUp中很低效,在上例中为此定义了一个before_test()方法。
unittest提供了相应方法
setUpClass() 对应的是tearDownClass()
需要注意的是必需加上@classmethod修饰,否则报错
A class method called before tests in an
individual class are run. setUpClass is called with the class as the
only argument and must be decorated as a classmethod():
@classmethod
def setUpClass(cls):
...
重写代码:
# 删除before_test()方法,改为下列方法
class TestCase_WebApi(unittest.TestCase):
@classmethod
def setUpClass(cls):
print('starting
webapi...')
thread = Thread(target=run)
thread.daemon = True
thread.start()
print('webapi is
running...')
3.
test_suite
实例化类,添加测试方法,
实例化TextTestRunner,run()
手动添加用例,可以保证测试执行顺序。
import unittest
from test.test_webapi import TestCaseWebApi
def run_suite():
suite = unittest.TestSuite()
# 添加单个测试用例
suite.addTest(TestCaseWebApi('test_webapi_mainpage'))
# 添加多个测试用例
#
suite.addTests([TestCase_WebApi('test_webapi_get')])
# 声明
runner =
unittest.TextTestRunner()
runner.run(suite)
if __name__ == '__main__':
run_suite()
4.
TestLoader
测试方法,用例,套件,解决了测试方法的层级问题
但上面的手动添加用例还不够方便,能不能自动添加测试方法
class unittest.TestLoader
The TestLoader class is used to create test
suites from classes and modules. Normally, there is no need to create an
instance of this class; the unittest module
provides an instance that can be shared asunittest.defaultTestLoader. Using a
subclass or instance, however, allows customization of some configurable
properties.
testloader提供了多种查找及添加用例的方法,但一般用不到这么深入
有一个非常简单的方式:
def run_testloader():
print('use
TestLoader')
suite =
unittest.defaultTestLoader.discover('.', pattern='test_unittest_*.py', top_level_dir=None)
print(suite)
runner = unittest.TextTestRunner()
runner.run(suite)
if __name__ == '__main__':
# run_suite()
run_testloader()
unittest.defaultTestLoader是testloader的一个实例,一般用这个就可以了
discover有三个参数
第一个指定目录
第二个指定文件名匹配模式
第三个参数不明确,照写即可。
5.
总结
一般情况下上面的案例可以满足大部分测试要求了
笔记-unittest实战的更多相关文章
- AngularJS in Action读书笔记6(实战篇)——bug hunting
这一系列文章感觉写的不好,思维跨度很大,原本是由于与<Angularjs in action>有种相见恨晚而激发要写点读后感之类的文章,但是在翻译或是阐述的时候还是会心有余而力不足,零零总 ...
- Hadoop学习笔记(8) ——实战 做个倒排索引
Hadoop学习笔记(8) ——实战 做个倒排索引 倒排索引是文档检索系统中最常用数据结构.根据单词反过来查在文档中出现的频率,而不是根据文档来,所以称倒排索引(Inverted Index).结构如 ...
- flink学习笔记-flink实战
说明:本文为<Flink大数据项目实战>学习笔记,想通过视频系统学习Flink这个最火爆的大数据计算框架的同学,推荐学习课程: Flink大数据项目实战:http://t.cn/EJtKh ...
- 学习笔记——Maven实战(一)坐标规划
坐标是什么?为什么要规划? 坐标是Maven最基本的概念,它就像每个构件的身份证号码,有了它我们就可以在数以千万计的构件中定位任何一个我们感兴趣的构件.举个最简单的例子,如果没有坐标,使用JUnit的 ...
- 学习笔记——Maven实战(二)POM重构之增还是删
重构是广大开发者再熟悉不过的技术,在Martin Fowler的<重构——改善既有代码的设计>一书中,其定义为“重构(名词):对软件内部结构的一种调整,目的是在不改变软件之可察行为前提下, ...
- 学习笔记——Maven实战(三)多模块项目的POM重构
重复,还是重复 程序员应该有狗一般的嗅觉,要能嗅到重复这一最常见的坏味道,不管重复披着怎样的外衣,一旦发现,都应该毫不留情地彻底地将其干掉.不要因为POM不是产品代码而纵容重复在这里发酵,例如这样一段 ...
- 学习笔记——Maven实战(四)基于Maven的持续集成实践
Martin的<持续集成> 相信很多读者和我一样,最早接触到持续集成的概念是来自Martin的著名文章<持续集成>,该文最早发布于2000年9月,之后在2006年进行了一次修订 ...
- 学习笔记——Maven实战(五)自动化Web应用集成测试
自动化集成测试的角色 本专栏的上一篇文章讲述了Maven与持续集成的一些关系及具体实践,我们都知道,自动化测试是持续集成必不可少的一部分,基本上,没有自动化测试的持续集成,都很难称之为真正的持续集成. ...
- 学习笔记——Maven实战(六)Gradle,构建工具的未来?
Maven面临的挑战 软件行业新旧交替的速度之快往往令人咂舌,不用多少时间,你就会发现曾经大红大紫的技术已经成为了昨日黄花,当然,Maven也不会例外.虽然目前它基本上是Java构建的事实标准,但我们 ...
随机推荐
- Android 仿微信朋友圈拍小视频上传到服务器
这个接上一个写的实现拍小视频和传到服务器的 界面是这个样子滴. 我也知不知道怎么给图片搞小一点o(╯□╰)o 布局文件是这样的[认真脸] <?xml version="1.0&quo ...
- 光标显示样式 css 中 cursor 属性使用
记录一下 cursor 的各种样式,方便自己查找.之前用到不常用的每次去 百度 或 Google 找不如自己记录下好找些. cursor光标类型 auto default none context-m ...
- Linux教程之:Nginx [emerg]: bind() to 0.0.0.0:80 failed (98: Address already in use)
Nginx [emerg]: bind() to 0.0.0.0:80 failed (98: Address already in use) 使用命令关闭占用80端口的程序 sudo fuser - ...
- 数组:获取GET的键名
1.今天仓鼠遇到这个情况:通过$_GET获取参数,但是参数变成了键名形式 2.那仓鼠想要拿到这个键名,那就要:使用array_keys()获取数组中的所有键名,然后进行转换 代码如下: 结果: 以上 ...
- 了解Web及网络基础(二)
HTTP报文分为两种,HTTP请求报文跟HTTP响应报文. HTTP请求报文的结构如下: 其中,请求行中包括的内容有方法.URI和HTTP版本,请求首部字段.通用首部字段和实体首部字段隶属于HTTP首 ...
- IOS instancetype的使用好处
instancetype的类型表示上,跟id一样,可以表示任何对象类型 instancetype只能用在返回值类型上,不能像 id 一样用在参数类型上 instancetype 比 id 多一个好处 ...
- 数长方形有多少个?POJ(1693)
题目链接:http://poj.org/problem?id=1693 解题报告: 随机选两根横的,再找一下与这两根横线相交的竖线有多少根,m,那么就有(m-1)*m/2个长方形. #include ...
- create-react-app项目使用假数据
做新项目的时候,前端每次要等后端接口准备好再开始,就会延期,等后端接口准备好了,前端这边的项目又会相互紧张,如果前端跟后端同时进行,前期将框架,基础做好,定好接口文档,前端在后端没准备好接口的时候使用 ...
- maven parent version not found
需要把parent工程,也就是package是pom的那个工程先install一下 要是不行的话可以试下mvn -X clean install,-X表示强制从远程库更新dependency:再不行可 ...
- 利用python中的PIL进行矩阵与图像之间的转换
1.图像转换为矩阵 matrix = numpy.asarray(image) 2.矩阵转换为图像 image = Image.fromarray(matrix)