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 =========================
相关推荐
小张努力向上up17 小时前
pytest使用报错(以及解决pytest所谓的“抑制print输出”)
pytest
测试界清流3 天前
Python自动化测试框架unittest与pytest的区别
开发语言·python·pytest
回家吃月饼3 天前
python中unittest框架和pytest框架区别
开发语言·python·pytest
MJH8275 天前
接口测试流程及测试点!
自动化测试·软件测试·selenium·单元测试·pytest·接口测试·压力测试
糖果店的幽灵6 天前
基于Python的自动化测试框架-Pytest总结-第一弹基础
服务器·python·pytest
测试界吖吖6 天前
【pytest】html报告修改和汉化
自动化测试·软件测试·python·功能测试·程序人生·html·pytest
潇潇说测试7 天前
Python+requests+pytest+excel+allure 接口自动化测试实战
自动化测试·软件测试·python·功能测试·程序人生·excel·pytest
Anthony_路人甲9 天前
pytest中的极其重要固件(request)的理解
python·pytest
牛油菠蘿包9 天前
Pytest--安装与入门
python·pytest
JAVA开发区9 天前
Python测试框架 pytest : 从零开始的完全指南
开发语言·python·pytest