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 =========================
相关推荐
把头塞进显示器2 天前
电商平台-测试报告
python·selenium·pytest
昔我往昔7 天前
pytest日志问题排查记录
python·pytest
黄昏恋慕黎明10 天前
博客论坛技术迭代
python·pytest
2601_9620782310 天前
Python接口自动化流程(pytest)
python·pytest·接口自动化·测试框架·断言
杨女士YRJ10 天前
python+pytest+01
python·pytest
2601_9620782311 天前
Python接口自动化框架:pytest
python·pytest·接口自动化·测试框架·restfulapi
2601_9620781911 天前
Appium+Python+pytest自动化测试框架详解
自动化测试·python·appium·pytest·移动应用
2601_9622845013 天前
安卓APP UI自动化测试:Python + UiAutomator2 + pytest + pytest-html
python·pytest·uiautomator2·ui自动化测试·安卓app
2601_9623008113 天前
基于pytest的接口自动化测试:使用requests模块的实战项目与接口关联方法详解
接口自动化测试·测试用例·pytest·接口关联·requests模块
2601_9623008113 天前
Python + Requests + Pytest + Excel + Allure:接口自动化测试项目实战
python·excel·pytest·allure·requests