pytest是一个使构建简单和可扩展测试变得容易的框架。测试具有表现力和可读性-不需要样板代码。数分钟内即可开始为您的应用程序或库进行小型单元测试或复杂的功能测试。
安装pytest
1、在命令行中运行以下命令:
pip install -U pytest
2、检查您是否安装了正确的版本:
$ pytest --version
This is pytest version 5.x.y, imported from $PYTHON_PREFIX/lib/python3.6/site-packages/pytest/__init__.py
创建您的第一个测试
仅用四行代码创建一个简单的测试函数:
# content of test_sample.py
def func(x):
return x + 1
def test_answer():
assert func(3) == 5
而已。您现在可以执行测试功能:
$ pytest
=========================== test session starts ============================
platform linux -- Python 3.x.y, pytest-5.x.y, py-1.x.y, pluggy-0.x.y
cachedir: $PYTHON_PREFIX/.pytest_cache
rootdir: $REGENDOC_TMPDIR
collected 1 item
test_sample.py F [100%]
================================= FAILURES =================================
_______________________________ test_answer ________________________________
def test_answer():
> assert func(3) == 5
E assert 4 == 5
E + where 4 = func(3)
test_sample.py:6: AssertionError
============================ 1 failed in 0.12s =============================
该测试返回失败报告,因为func(3)没有返回5。
注意
您可以使用该assert语句来验证测试期望。pytest的Advanced assertion introspection将智能地报告assert表达式的中间值,因此您可以避免使用许多JUnit旧方法的名称。
运行多个测试
pytest将在当前目录及其子目录中运行所有格式为test _ *。py或* _test.py的文件。更一般而言,它遵循标准的测试发现规则。
断言某个异常被引发
使用引发助手来断言某些代码引发异常:
# content of test_sysexit.py
import pytest
def f():
raise SystemExit(1)
def test_mytest():
with pytest.raises(SystemExit):
f()
以“安静”报告模式执行测试功能:
$ pytest -q test_sysexit.py
. [100%]
1 passed in 0.12s
将多个测试分组到一个类中
开发多个测试后,您可能需要将它们分组到一个类中。pytest使创建包含多个测试的类变得容易:
# content of test_class.py
class TestClass:
def test_one(self):
x = "this"
assert "h" in x
def test_two(self):
x = "hello"
assert hasattr(x, "check")
pytest遵循其Python测试发现约定来发现所有测试,因此它会找到两个带test_前缀的函数。不需要对任何子类进行子类化,但是请确保为类加上前缀,Test否则将跳过该类。我们可以简单地通过传递其文件名来运行模块:
$ pytest -q test_class.py
.F [100%]
================================= FAILURES =================================
____________________________ TestClass.test_two ____________________________
self = <test_class.TestClass object at 0xdeadbeef>
def test_two(self):
x = "hello"
> assert hasattr(x, "check")
E AssertionError: assert False
E + where False = hasattr('hello', 'check')
test_class.py:8: AssertionError
1 failed, 1 passed in 0.12s
第一次测试通过,第二次失败。您可以轻松地在断言中看到中间值,以帮助您了解失败的原因。
请求唯一的临时目录进行功能测试
pytest提供内置的fixtures / function参数来请求任意资源,例如唯一的临时目录:
# content of test_tmpdir.py
def test_needsfiles(tmpdir):
print(tmpdir)
assert 0
tmpdir在测试功能签名中列出名称,并pytest在执行测试功能调用之前查找并调用夹具工厂以创建资源。在测试运行之前,pytest创建一个“每次测试调用唯一”临时目录:
$ pytest -q test_tmpdir.py
F [100%]
================================= FAILURES =================================
_____________________________ test_needsfiles ______________________________
tmpdir = local('PYTEST_TMPDIR/test_needsfiles0')
def test_needsfiles(tmpdir):
print(tmpdir)
> assert 0
E assert 0
test_tmpdir.py:3: AssertionError
--------------------------- Captured stdout call ---------------------------
PYTEST_TMPDIR/test_needsfiles0
1 failed in 0.12s
有关tmpdir处理的更多信息,请参见临时目录和文件。
使用以下命令找出存在哪种内置pytest固定装置:
pytest --fixtures # shows builtin and custom fixtures
请注意,_除非-v添加了该选项,否则此命令将忽略带有前导的灯具。