Pytest 的小例子

一个简单的例子

下面代码保存到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 =========================

相关推荐

  1. Pytest 例子

    2023-12-06 11:14:04       58 阅读
  2. Python chatGPT API例子

    2023-12-06 11:14:04       45 阅读
  3. mysql 例子

    2023-12-06 11:14:04       28 阅读
  4. 2023-简单点-python多路复用例子

    2023-12-06 11:14:04       65 阅读
  5. 程序常用实用例子

    2023-12-06 11:14:04       56 阅读
  6. pandas行选择10个例子

    2023-12-06 11:14:04       33 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-06 11:14:04       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-06 11:14:04       100 阅读
  3. 在Django里面运行非项目文件

    2023-12-06 11:14:04       82 阅读
  4. Python语言-面向对象

    2023-12-06 11:14:04       91 阅读

热门阅读

  1. css基础

    2023-12-06 11:14:04       58 阅读
  2. 什么是供应链金融分账系统?

    2023-12-06 11:14:04       59 阅读
  3. vue2和vue3的区别

    2023-12-06 11:14:04       55 阅读
  4. oracle给用户授权查询权限

    2023-12-06 11:14:04       60 阅读
  5. MISRA C++ 2008 标准解析

    2023-12-06 11:14:04       50 阅读
  6. MX6ULL学习笔记(四)设备树的 OF 函数

    2023-12-06 11:14:04       41 阅读
  7. 函数式编程

    2023-12-06 11:14:04       56 阅读
  8. 关于打印机直连的分享

    2023-12-06 11:14:04       68 阅读