笔记-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.     
总结

一般情况下上面的案例可以满足大部分测试要求了

最新文章

  1. java从基础知识(十)java多线程(上)
  2. python 内置函数!
  3. Java语言的个人理解
  4. JsonConvert 使用注意事项之 Serializable
  5. 如何禁止IE自动生成链接
  6. BZOJ 4665: 小w的喜糖
  7. 一个最小化的SpringBoot项目
  8. 在阿里云服务器ubuntu14.04运行netcore
  9. yii2源码学习笔记(二十)
  10. 深入浅出 - Android系统移植与平台开发(十)- Android编译系统与定制Android平台系统(瘋耔修改篇二)
  11. 编译XSIP过程中环境配置
  12. JavaScript核心参考
  13. Gulp-自动化编译sass和pug文件
  14. brew 安装指定版本命令行工具 tmux 多版本实现
  15. 5.2Python数据处理篇之Sympy系列(二)---Sympy的基本操作
  16. K-means算法的matlab程序(初步)
  17. 判断鼠标进入容器的方向小Demo
  18. 打印一个浮点值%f和%g
  19. P4: Programming Protocol-Independent Packet Processors
  20. hdu 5446(2015长春网络赛J题 Lucas定理+中国剩余定理)

热门文章

  1. tomcat的相关使用
  2. 对react vd 性能的理解
  3. 解压war包
  4. iOS获取/删除url中的参数
  5. C#获取农历的日期(转)
  6. python pip安装报错python setup.py egg_info failed with error code 1
  7. JS二维数组的写法以及注意事项
  8. 关于nutz跨服务器上传文件
  9. Shiro Demo:SpringBoot+Shiro+Druid+MyBatis
  10. 在Centos上面用yum不能安装redis的朋友看过来