一个简单例子

下面代码保存test_pytest.py

一个简单例子
def inc(x):
    return x + 1


def test_answer():
    assert inc(3) == 5

def test_ask():
    assert inc(4) == 5

pytest 需要安装一下 

pip install pytest
(Venv) D:pythonwork>pip install pytest
Collecting pytest
  Downloading pytest-7.4.3-py3-none-any.whl (325 kB)
     |█████████████                   | 133 kB 100 kB/s eta 0:00:02
     |██████████████                  | 143 kB 100 kB/s eta 0:00:0
     |███████████████                 | 153 kB 100 kB/s eta 0:00:
     |████████████████                | 163 kB 100 kB/s eta 0:00
     |█████████████████               | 174 kB 100 kB/s eta 0:0
     |██████████████████              | 184 kB 100 kB/s eta 0:
     |███████████████████             | 194 kB 100 kB/s eta 0
     |████████████████████            | 204 kB 100 kB/s eta
     |█████████████████████           | 215 kB 100 kB/s eta
     |██████████████████████          | 225 kB 100 kB/s et
     |███████████████████████         | 235 kB 100 kB/s e
     |████████████████████████        | 245 kB 100 kB/s
     |█████████████████████████       | 256 kB 100 kB/s
     |██████████████████████████      | 266 kB 100 kB/
     |███████████████████████████     | 276 kB 100 kB
     |████████████████████████████    | 286 kB 100 k
     |█████████████████████████████   | 296 kB 100
     |██████████████████████████████  | 307 kB 100
     |███████████████████████████████ | 317 kB 10
     |████████████████████████████████| 325 kB 1
00 kB/s
Collecting tomli>=1.0.0; python_version < "3.11"
  Downloading tomli-2.0.1-py3-none-any.whl (12 kB)
Collecting colorama; sys_platform == "win32"
  Downloading colorama-0.4.6-py2.py3-none-any.whl (25 kB)
Collecting pluggy<2.0,>=0.12
  Downloading pluggy-1.3.0-py3-none-any.whl (18 kB)
Collecting exceptiongroup>=1.0.0rc8; python_version < "3.11"
  Downloading exceptiongroup-1.2.0-py3-none-any.whl (16 kB)
Collecting packaging
  Downloading packaging-23.2-py3-none-any.whl (53 kB)
     |██████████████████▌             | 30 kB 495 kB/s eta 0:
     |█████████████████████████       | 40 kB 114 kB/s
     |███████████████████████████████ | 51 kB 136
     |████████████████████████████████| 53 kB 11
1 kB/s
Collecting iniconfig
  Downloading iniconfig-2.0.0-py3-none-any.whl (5.9 kB)
Installing collected packages: tomli, colorama, pluggy, exceptiongroup, packagin
g, iniconfig, pytest
Successfully installed colorama-0.4.6 exceptiongroup-1.2.0 iniconfig-2.0.0 packa
ging-23.2 pluggy-1.3.0 pytest-7.4.3 tomli-2.0.1
WARNING: You are using pip version 20.2.3; however, version 23.3.1 is available.


pytest运行

F.  说明已经False失败和. true 一个通过一个失败

(Venv) D:pythonwork>pytest
============================= test session starts =============================
platform win32 -- Python 3.8.7, pytest-7.4.3, pluggy-1.3.0
rootdir: D:pythonwork
collected 2 items

test_python.py F.                                                        [100%]

================================== FAILURES ===================================
_________________________________ test_answer _________________________________

    def test_answer():
>       assert inc(3) == 5
E       assert 4 == 5
E        +  where 4 = inc(3)

test_python.py:6: AssertionError
=========================== short test summary info ===========================
FAILED test_python.py::test_answer - assert 4 == 5
========================= 1 failed, 1 passed in 0.15s =========================

原文地址:https://blog.csdn.net/keny88888/article/details/134694066

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任

如若转载,请注明出处:http://www.7code.cn/show_29532.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注