python中的单元测试,主要有3种方式:

1.unittest

2.nose(nose2)

3.pytest

关于unittest的使用,非常简单,继承TeseCase:

https://docs.python.org/2/library/unittest.html#unittest.TestCase.setUp

setUp()

Method called to prepare the test fixture. This is called immediately before calling the test method; other than AssertionError or SkipTest, any exception raised by this method will be considered an error rather than a test failure. The default implementation does nothing.

tearDown()

Method called immediately after the test method has been called and the result recorded. This is called even if the test method raised an exception, so the implementation in subclasses may need to be particularly careful about checking internal state. Any exception, other thanAssertionError or SkipTest, raised by this method will be considered an additional error rather than a test failure (thus increasing the total number of reported errors). This method will only be called if the setUp() succeeds, regardless of the outcome of the test method. The default implementation does nothing.

setUpClass()

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):...

See Class and Module Fixtures for more details.

New in version 2.7.

tearDownClass()

A class method called after tests in an individual class have run. tearDownClass is called with the class as the only argument and must be decorated as a classmethod():

@classmethod
def tearDownClass(cls):...

The TestCase class provides several assert methods to check for and report failures. The following table lists the most commonly used methods (see the tables below for more assert methods):

Method Checks that New in
assertEqual(a, b) 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 2.7
assertIsNot(a, b) a is not b 2.7
assertIsNone(x) x is None 2.7
assertIsNotNone(x) x is not None 2.7
assertIn(a, b) a in b 2.7
assertNotIn(a, b) a not in b 2.7
assertIsInstance(a, b) isinstance(a, b) 2.7
assertNotIsInstance(a, b) not isinstance(a, b) 2.7

有些数据需要mock的情况下,unittest.mock库提供解决办法。

如果要获取详情,则可以这样:

you can run tests with more detailed information by passing in the verbosity argument:

if __name__ == '__main__':unittest.main(verbosity=2)

nose  pip install nose 或者 pip  install nose2安装,它会自动寻找unittest.TestCase的派生类。

python经典的单元测试框架:pytest

https://docs.pytest.org/en/latest/

后期重点使用pytest来进行单元测试,内容很多,也不在赘述,直接看官方文档。

编写pytest测试样例的规则

  • 测试文件以test_开头(以_test结尾也可以)
  • 测试类以Test开头,并且不能带有 init 方法
  • 测试函数以test_开头
  • 断言使用基本的assert即可

简易教程:http://www.testclass.net/pytest/

单元测试用代码覆盖率来衡量,代码覆盖率是由特定的测试套件覆盖被测试源代码的程度来度量的。一般使用coverage.py度量覆盖。

pip  install coverage

nose2代码覆盖率通过cov-core来度量,可以以html格式在浏览器输出。

pytest使用pytest-cov来度量代码覆盖率。

python单元测试相关推荐

  1. Python单元测试--使用装饰器实现测试跳过和预期故障

    Python单元测试unittest中提供了一下四种装饰器实现测试跳过和预期故障.(使用Python 2.7.13) 请查考Python手册中: https://docs.python.org/dev ...

  2. Python单元测试去哪儿了?

    如果您正在编写库或应用程序,则单元测试文件会放在哪里? 将测试文件与主应用程序代码分开是很好的选择,但是将它们放在应用程序根目录内的" tests"子目录中是很尴尬的,因为这样会使 ...

  3. Python单元测试框架Pyunit 的使用

    Python单元测试框架Pyunit 使用示例: 1 import unittest 2 3 class Person: 4 def age(self): 5 return 34 6 def name ...

  4. Python单元测试框架之pytest 3 -- fixtures

    From: https://www.cnblogs.com/fnng/p/4769020.html Python单元测试框架之pytest -- fixtures 2015-08-29 13:05 b ...

  5. Python单元测试最佳实践

    Are you ready? ↓↓↓ 今天的课程为< Python单元测试>,内容共分为三个部分:单元测试的概念.工具与方法.Coverage 统计单元测试覆盖率的工具和Mock 简化单元 ...

  6. python单元测试工具_Python的单元测试工具——unittest小结

    简介 unittest是Python的内建模块,是Python单元测试的事实标准,也叫PyUnit.使用unittest之前,先了解如下几个概念: test case:测试用例,可以通过创建unite ...

  7. Python 单元测试详解

    作者:yukkizhang,腾讯 CSIG 测试工程师 本文直接从常用的 Python 单元测试框架出发,分别对几种框架进行了简单的介绍和小结,然后介绍了 Mock 的框架,以及测试报告生成方式,并以 ...

  8. python单元测试mock_Python单元测试的Mock是怎么回事

    单元测试 什么是单元测试, 维基百科上是这么定义的: unit testing is a method by which individual units of source code, sets o ...

  9. python单元测试示范卷_Python单元测试--Unittest

    原标题:Python单元测试--Unittest 什么是单元测试 单元测试是用来对最小可测试单元(如一个函数.一个类或者一个模块)进行检查和验证.尤其是在对代码进行修改或重构后,可以快速准确的定位出现 ...

  10. python单元测试_python单元测试

    http://pyunit.sourceforge.net/ 目录 Python 单元测试框架 .................................................... ...

最新文章

  1. Alipay秘钥问题
  2. 论文简述 | DOT:面向视觉SLAM的动态目标跟踪
  3. Simplivity存储家族推新:满足所有闪存需求和更好的灾难恢复
  4. java开发可重用代码包工具包_[Java教程]彻底搞懂Java开发工具包(JDK)安装及环境变量配置...
  5. Google Megastore介绍
  6. iPhone程序运行流程浅谈
  7. 前沿 | 加州理工大学什么是Imitation Learning(模仿学习)
  8. HDU-Keywords Search(AC自动机)
  9. 以TikTok为切入的海外流量打法?
  10. 在Windows Server 2016 Core上运行SQL Server容器
  11. Android调用手机摄像头
  12. Lattice FPGA 开发工具Diamond使用流程总结——安装
  13. 6.1 QDir目录操作
  14. android开发简历,轻松拿到了阿里Android高级开发工程师的offer
  15. 微信小程序开发教程——1.0.1appid注册和开发者工具下载
  16. “TOP面对面” 技术AMA系列第一期:揭开TOP技术团队的神秘面纱
  17. Spring Cloud中使用jib进行docker部署
  18. 全国大江大河实时水情数据下载
  19. 使用Huginn批量订阅微信公众号
  20. 对 IIC 总线的理解、调用函数以及常见面试问题

热门文章

  1. 红色警报 (25 分)【测试点分析】【两种解法】
  2. DevC++怎么更改背景颜色
  3. 解题报告+优化——试题 基础练习 矩形面积交——16行代码AC
  4. 完美解决Centos不能ping百度的问题
  5. docker容器的本地局域网yum源优化
  6. 割平面法只能求解纯整数规划吗_【DMC第四讲】整数规划
  7. java linux 权限管理_权限管理java实现(源于Linux中的权限管理算法)
  8. 电脑测速软件_iPerf3 搭建局域网内部测速环境
  9. url在python_Python中url标签使用详解
  10. python eel 多线程_利用Eel使JavaScript调用Python程序