pytest快速入门1-安装和开始

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.py
def 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,并指出错误的位置。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐