栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python 测试框架-unittest

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python 测试框架-unittest

1、了解unittest
https://www.cnblogs.com/miki-peng/p/12501341.html

2、assert方法 检查
assertEqual(a, b,msg=None) a ==b
assertNotEqual(a, b) a !=b
assertTrue(x) bool(x) is True
assertFalse(x) Bool(x) is False
assertIs(a, b) a is b
assertIsNot(a, b) a is not b
assertIsNone(x) x is None
assertIsNotNone(x) x is not None
assertIn(a, b) a in b
assertNotIn(a, b) a not in b
assertIsInstance(a, b) isinstance(a,b)
assertNotIsInstance(a, b) not isinstance(a,b)

3、示例

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import math,os
import sys
reload(sys)
sys.setdefaultencoding('utf8')
import unittest
import HTMLTestRunner
sys.path.append(os.path.abspath('gen-py/'))

class MytestCase(unittest.TestCase):
    def setUp(self):
        '''
        setUp 每条用例执行之前都会执行
        ip:服务ip,  port:服务端口
        '''
        print "test start ******"
        self.age = 18

    def tearDown(self):
        '''每条用例执行之后都会执行:  关闭服务链接'''
        print "test stop ******"

    #unittest中测试用例方法名都是以test开头的
    def test_rb(self):
        '''
        case用例
        '''
        self.assertEqual(self.age, 18)
        
    #屏蔽case直接在测试用例方法名前加个下划线即可
    def _test_pickup(self):
        '''
        case用例
        '''

if __name__ == '__main__':
    
    suite = unittest.TestSuite()   #unittest.TestSuite()类来表示一个测试用例集,把需要执行的用例类或模块存到一起
    #执行所有case
    suite.addTests(unittest.TestLoader().loadTestsFromTestCase(MytestCase))
    #执行单个case
    suite.addTest(MytestCase("test_rb"))

    #case执行结果生成前端页面展示
    fp = file('./index.html', 'wb')
    # HTMLTestRunner 是 Python 标准库的 unittest 框架的一个扩展,它可以生成一个直观清晰的 HTML 测试报告,使用的前提就是要下载 HTMLTestRunner.py,下载完后放在python的安装目录下的scripts目录下即可
    runner = HTMLTestRunner.HTMLTestRunner(  
        stream=fp,
        title='MytestCase test report',
        description='MytestCase test report'
    )
    
    # 跑失败的case, 重试3次
    for i in range(3):  
        result = runner.run(suite)    #使用启动器去执行测试套件里的用例
        if 0 == len(result.errors) and 0 == len(result.failures): 
            break
        retryCases = []              #存储失败的case
        for failure in result.failures:
            failCase = failure[0]
            retryCases.append(failCase)   #fail的case重新去执行
        for error in result.errors:
            errorCase = error[0]
            retryCases.append(errorCase)  #error的case重新去执行
            
        suite = unittest.TestSuite()
        suite.addTests(retryCases)

    if len(result.errors) > 0 or len(result.failures) > 0:
        sys.exit(-1)

4、安装HTMLTestRunner:
下载HTMLTestRunner.py 和 test_HTMLTestRunner.py从如下link
http://tungwaiyip.info/software/HTMLTestRunner.html

sudo cp HTMLTestRunner.py /Library/Python/2.7/site-packages
sudo cp test_HTMLTestRunner.py /Library/Python/2.7/site-package

此时, import HTMLTestRunner 应该不会报错,可在python console里面try

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/674685.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号