Python中Unittest框架的具体使用

yipeiwu_com5年前Python基础

Unittest

1.环境

Unittest为Python内嵌的测试框架,不需要特殊配置,仅需要在File---settings---Tools----Python Intergrated Tools下配置框架为Unittest

2.编写规范

  • 需要导入 import unittest
  • 测试类必须继承unittest.TestCase
  • 测试方法以 test_开头
  • 模块和类名没有要求

3.Unittest介绍

import unittest

def add(x, y):
 return x + y

def sub(x,y):
 return x - y

class UnittestDemo(unittest.TestCase):

 def setUp(self):
  print("I an setup")

 def test_add(self):
  print("my first unit test demo----add")
  self.assertEqual(add(10,1),11,"assert equal")

 def test_sub(self):
  print("my first unit test demo----sub")
  self.assertEqual(sub(10, 1), 9, "assert equal2")

 @unittest.skip("i don't want to run this case.")
 def test_sub1(self):
  print("my first unit test demo----sub1")
  self.assertEqual(sub(10, 1), 9, "assert equal3")

 def tearDown(self):
  print("I an tearDown")

setUp及TearDown

方法级别的,类里面的每个方法调用一次

 def setUp(self):
  print("I an setup")

setUpClass及tearDownClass

类级别的,类里面的所有方法仅执行一次

  @classmethod
 def setUpClass(cls):
  print("I an setUpClass") 

setUpModule,tearDownModule

模块级别的,执行模块里面类中的所有方法仅执行一次

4.Unittest使用

import unittest

from unittest_task import UnittestDemo
from HTMLTestRunner import HTMLTestRunner


suite=unittest.TestSuite()#创建用例集合
tests = [UnittestDemo("test_add"), UnittestDemo("test_sub"),UnittestDemo("test_sub1")]
suite.addTests(tests)


with open('HTMLReport.html', 'wb') as fi:
 runner = HTMLTestRunner(stream=fi,
       title = 'MathFunc Test Report',
       description='generated by HTMLTestRunner.',
       verbosity=2
       )
 runner.run(suite)

TestCase

多个测试用例集合在一起,就是TestSuite

TestSuite

多个测试用例集合在一起,就是TestSuite

TestLoader

用来加载TestCase到TestSuite中的

TestRunner

执行测试用例的,测试的结果会保存到TestResult实例中,包括运行了多少测试用例,成功了多少,失败了多少等信息

skip装饰器

跳过某个用例不执行

@unittest.skip("i don't want to run this case.")

无条件跳过

 unittest.skip(reason)

当condition为True时跳过

unittest.skipIf(condition, reason)

当condition为False时跳过

unittest.skipUnless(condition, reason)

self.skipTest('do not run this.')

5.Unittest生成报告

5.1 Unittest生成txt格式报告

if __name__ == '__main__':
 suite = unittest.TestSuite()
 
 tests = [TestMathFunc("test_add"), TestMathFunc("test_minus"), TestMathFunc("test_divide")]
 suite.addTests(tests)
 
 with open('UnittestTextReport.txt', 'a') as f:
  runner = unittest.TextTestRunner(stream=f, verbosity=2)
  runner.run(suite)
 

运行该文件,就会发现目录下生成了'UnittestTextReport.txt,所有的执行报告均输出到了此文件中。

verbosity参数可以控制执行结果的输出,0 是简单报告、1 是一般报告、2 是详细报告

5.2 Unittest生成html格式报告

with open('HTMLReport.html', 'wb') as fi:
 runner = HTMLTestRunner(stream=fi,
       title = 'MathFunc Test Report',
       description='generated by HTMLTestRunner.',
       verbosity=2
       )
 runner.run(suite)

输出测试报告为HTML格式,unittest中htmltestrunner导出测试报告,可以修改htmltestrunner中的测试代码

其中,的python3中用HTMLTestRunner.py报ImportError: No module named 'StringIO'如何解决见:

/post/168573.htm

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

python中关于for循环的碎碎念

为什么要挑战自己在代码里不写for loop?因为这样可以迫使你去使用比较高级、地道的语法或库。文中以python为例子,讲了不少大家其实在别人的代码里都见过、但自己很少用的语法。 这是...

python存储16bit和32bit图像的实例

笔记:python中存储16bit和32bit图像的方法。 说明:主要是利用scipy库和pillow库,比较其中的不同。 ''' 测试16bit和32bit图像的python存储方...

Python中的map()函数和reduce()函数的用法

Python中的map()函数和reduce()函数的用法

Python内建了map()和reduce()函数。 如果你读过Google的那篇大名鼎鼎的论文“MapReduce: Simplified Data Processing on Lar...

Python实现的基于优先等级分配糖果问题算法示例

Python实现的基于优先等级分配糖果问题算法示例

本文实例讲述了Python实现的基于优先等级分配糖果问题算法。分享给大家供大家参考,具体如下: 问题: 有n个人,每个人有一定的优先等级,等级高的人要比身边等级低得人得到的多,每个人都不...

详解Python自建logging模块

简单使用 最开始,我们用最短的代码体验一下logging的基本功能。 import logging logger = logging.getLogger() logging.basi...