pytest: 助你更好编程¶
pytest 框架可以轻松编写小型测试,同时支持应用程序和软件库的复杂功能性测试。
一个简单的例子:
# content of test_sample.py
def inc(x):
return x + 1
def test_answer():
assert inc(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 inc(3) == 5
E assert 4 == 5
E + where 4 = inc(3)
test_sample.py:6: AssertionError
========================= 1 failed in 0.12 seconds =========================
由于 pytest 有详细的断言说明,所以只简单使用 assert 语句。有关更多示例,请参阅 入门
特性¶
错误/请求¶
请使用 GitHub问题追踪 来提交错误或请求特性。