一、介绍
本篇文章是介绍的是Python 世界中最火的第三方单元测试框架:pytest。
它有如下主要特性:
- assert 断言失败时输出详细信息(再也不用去记忆
self.assert*
名称了) - 自动发现测试模块和函数
- 模块化夹具用以管理各类测试资源
- 对
unittest
完全兼容,对nose
基本兼容 - 非常丰富的插件体系,有超过 315 款第三方插件,社区繁荣
和前面介绍 unittest
和 nose
一样,我们将从如下几个方面介绍 pytest
的特性。
二、用例编写
同 nose
一样,pytest
支持函数、测试类形式的测试用例。最大的不同点是,你可以尽情地使用 assert
语句进行断言,丝毫不用担心它会在 nose
或 unittest
中产生的缺失详细上下文信息的问题。
比如下面的测试示例中,故意使得 test_upper
中断言不通过:
import pytest
def test_upper():
assert 'foo'.upper() == 'FOO1'
class TestClass:
def test_one(self):
x = "this"
assert "h" in x
def test_two(self):
x = "hello"
with pytest.raises(TypeError):
x + []
而当使用 pytest
去执行用例时,它会输出详细的(且是多种颜色)上下文信息:
=================================== test session starts ===================================
platform darwin -- Python 3.7.1, pytest-4.0.1, py-1.7.0, pluggy-0.8.0
rootdir: /Users/prodesire/projects/tests, inifile:
plugins: cov-2.6.0
collected 3 items
test.py F.. [100%]
======================================== FAILURES =========================================
_______________________________________ test_upper ________________________________________
def test_upper():
> assert 'foo'.upper() == 'FOO1'
E AssertionError: assert 'FOO' == 'FOO1'
E - FOO
E + FOO1
E ? +
test.py:4: AssertionError
=========================== 1 failed, 2 passed in 0.08 seconds ============================
不难看到,pytest
既输出了测试代码上下文,也输出了被测变量值的信息。相比于 nose
和 unittest
,pytest
允许用户使用更简单的方式编写测试用例,又能得到一个更丰富和友好的测试结果。
三、用例发现和执行
unittest
和 nose
所支持的用例发现和执行能力,pytest
均支持。 pytest
支持用例自动(递归)发现:
- 默认发现当前目录下所有符合
test_*.py
或*_test.py
的测试用例文件中ÿ