pytest快速入门1安装和开始[Python基础]
pytest是python中较常用的测试框架,官方文档见:
https://docs.pytest.org/en/stable/contents.html#toc
安装命令:
pip install -U pytest
检查是否安装成功命令:
pytest --version
能查到版本号说明安装OK,否则嘿嘿。
创建第一个测试脚本test_sample.py:
# content of test_sample.pydef func(x):
return x + 1
def test_answer():
assert func(3) == 5
通过命令pytest执行,得到结果如下:
$ pytest=========================== test session starts ============================
platform linux -- Python 3.x.y, pytest-6.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
========================= short test summary info ==========================
FAILED test_sample.py::test_answer - assert 4 == 5
============================ 1 failed in 0.12s =============================
跟Junit测试框架类似都有assert的方法来对比实际结果和预期的结果是否一致,如果不一致则返回F,并指出错误的位置。
以上是 pytest快速入门1安装和开始[Python基础] 的全部内容, 来源链接: utcz.com/z/530334.html