Python几种比较常见的测试框架
python 几种常见的测试框架
- unittest
参考文档: https://docs.python.org/3/library/unittest.html
unittest笔记
The unittest unit testing framework was originally inspired by JUnit and has a similar flavor as major unit testing frameworks in other languages. It supports test automation, sharing of setup and shutdown code for tests, aggregation of tests into collections, and independence of the tests from the reporting framework.
unittest 和 JUnit类似,可以说是Python的标准单元测试框架,所以有时也被人称为 PyUnit。它使用起来和xUnit 家族其他成员类似。 用的人也比较多。兼容 python2 以及python3 。
个人比较喜欢用这个,主要之前用过JUnit,用这个上手就很快。而且属于python自动集成,不用额外的安装包,感觉是该有的都有了,用着方便。
- unittest2
参考文档: Https://pypi.python.org/pypi/unittest2
unittest2 is a backport of the new features added to the unittest testing framework in Python 2.7 and onwards.
unittest2 可以说是一个针对 unittest测试框架新特性的补丁。它很大程度上和unittest都类似。然后还添加了一些unittest没有的方法。
- pytest
参考文档:http://pytest.org/latest/
The pytest framework makes it easy to write small tests, yet scales to support complex functional testing for applications and libraries.
看了一下,pytest文档还是蛮详细的。比较关注的一点是,pytest 直接可以通过 @pytest.mark.parametrize 进行参数化,而unittest 则需要借助DDT。
- nose
参考文档: https://nose.readthedocs.org/en/latest/
基于Python的测试驱动开发实战 也有nose的用法: http://python.jobbole.com/81305/
nose extends unittest to make testing easier.
nose扩展了unittest,从而使得测试更容易。
一般可以用unittest方式写用例,写完之后用nose来执行。nose的测试收集方式还是很方便的。
还有一个特定就是,nose可以采用 @with_setup() 来定义方法的setup和teardown。
- doctest
参考文档:https://docs.python.org/3/library/doctest.html
Python 各种测试框架简介(一):doctest http://my.oschina.net/lionets/blog/268542
The doctest module searches for pieces of text that look like interactive Python sessions, and then executes those sessions to verify that they work exactly as shown.
doctest模块会搜索那些看起来像交互式会话的 Python 代码片段,然后尝试执行并验证结果。
doctest 中,如果要写测试用例,只需要在写在以 ”’ ”’包围的文档注释即可,也就是可以被doc这个属性引用到的地方。这点比较特别,跟其他单元测试框架都不一样。但是我觉得这样的话就注定了doctest不适合大型测试,因为做不到代码和测试的分离。
复制代码
import doctest
“””
This is the “example” module.
The example module supplies one function, factorial(). For example,
factorial(5)
120
“”“
def factorial(n):
“”“Return the factorial of n, an exact integer >= 0.
>>> [factorial(n) for n in range(6)]
[1, 1, 2, 6, 24, 120]
>>> factorial(30)
265252859812191058636308480000000
>>> factorial(-1)
Traceback (most recent call last):
...
ValueError: n must be >= 0
Factorials of floats are OK, but the float must be an exact integer:
>>> factorial(30.1)
Traceback (most recent call last):
...
ValueError: n must be exact integer
>>> factorial(30.0)
265252859812191058636308480000000
It must also not be ridiculously large:
>>> factorial(1e100)
Traceback (most recent call last):
...
OverflowError: n too large
"""
import math
if not n >= 0:
raise ValueError("n must be >= 0")
if math.floor(n) != n:
raise ValueError("n must be exact integer")
if n+1 == n: # catch a value like 1e300
raise OverflowError("n too large")
result = 1
factor = 2
while factor <= n:
result *= factor
factor += 1
return result
if name == “main“:
doctest.testmod(verbose=True)
复制代码
verbose 参数用于控制是否输出详细信息,默认为 False ,如果不写,那么运行时不会输出任何东西,除非测试 fail。
输出如下:
复制代码
Trying:
[factorial(n) for n in range(6)]
Expecting:
[1, 1, 2, 6, 24, 120]
ok
Trying:
factorial(30)
Expecting:
265252859812191058636308480000000
ok
Trying:
factorial(-1)
Expecting:
Traceback (most recent call last):
…
ValueError: n must be >= 0
ok
Trying:
factorial(30.1)
Expecting:
Traceback (most recent call last):
…
ValueError: n must be exact integer
ok
Trying:
factorial(30.0)
Expecting:
265252859812191058636308480000000
ok
Trying:
factorial(1e100)
Expecting:
Traceback (most recent call last):
…
OverflowError: n too large
ok
1 items had no tests:
main
1 items passed all tests:
6 tests in main.factorial
6 tests in 2 items.
6 passed and 0 failed.
Test passed.
复制代码
作者:微微微笑
出处:http://www.cnblogs.com/miniren/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.
相关文章