pytest笔记: pytest单元测试框架

第一步:安装 和查看版本

pycharm settings 查看

第二步: 编写test_example.py

python 复制代码
def  inc(x):
    return  x+1
def test_answer():
    assert inc(4) ==5

第三步:在当前路径下执行pytest 命令

python 复制代码
PS E:\data\web测试\Selenium3自动化测试实战------基于Python语言\mycode\pytest_example>      pytest
======================================================================== test session starts ========================================================================
platform win32 -- Python 3.10.9, pytest-7.1.2, pluggy-1.0.0
rootdir: E:\data\web测试\Selenium3自动化测试实战------基于Python语言\mycode\pytest_example
plugins: anyio-3.5.0
collected 1 item                                                                                                                                                     

test_sample.py .                                                                                                                                               [100%]

========================================================================= 1 passed in 0.07s =========================================================================
PS E:\data\web测试\Selenium3自动化测试实战------基于Python语言\mycode\pytest_example>      pytest --version
pytest 7.1.2
PS E:\data\web测试\Selenium3自动化测试实战------基于Python语言\mycode\pytest_example>

test_sample.py 后面带了个"." 表示通过

python 复制代码
def inc(x):
    return x + 1


def test_answer():
    ret = inc(3)
    assert ret == 111

如果不符合预期显示结果如下

python 复制代码
PS E:\data\web测试\Selenium3自动化测试实战------基于Python语言\mycode\pytest_example> pytest
======================================================================== test session starts ========================================================================
platform win32 -- Python 3.10.9, pytest-7.1.2, pluggy-1.0.0
rootdir: E:\data\web测试\Selenium3自动化测试实战------基于Python语言\mycode\pytest_example
plugins: anyio-3.5.0
collected 1 item                                                                                                                                                     

test_sample.py F                                                                                                                                               [100%]

============================================================================= FAILURES ==============================================================================
____________________________________________________________________________ test_answer ____________________________________________________________________________

    def test_answer():
        ret = inc(3)
>       assert ret == 1111
E       assert 4 == 1111

test_sample.py:7: AssertionError
====================================================================== short test summary info ======================================================================
FAILED test_sample.py::test_answer - assert 4 == 1111
========================================================================= 1 failed in 0.40s =========================================================================
PS E:\data\web测试\Selenium3自动化测试实战------基于Python语言\mycode\pytest_example>
相关推荐
大明者省6 分钟前
CentOS 与 Ubuntu Python 部署差异
笔记·python·ubuntu·centos
容沁风8 分钟前
本地用pptx和大模型生产PPT课件
python·大模型·pptx
bbaydnog11 分钟前
FreeRTOS学习笔记 17:资源管理与临界区保护——优先级反转、死锁,90%的RTOS bug都跟它有关
笔记·学习·bug
fanged11 分钟前
Datasheet学习5(STM32)(TODO)
笔记
codefan※17 分钟前
pytorch安装流程
人工智能·pytorch·python
nnsix22 分钟前
设计模式 - 迭代器模式 笔记
笔记·设计模式·迭代器模式
不羁的木木23 分钟前
Form Kit(卡片开发服务)学习笔记03-卡片UI开发与数据更新
笔记·学习·ui
不羁的木木26 分钟前
Form Kit(卡片开发服务)学习笔记02-环境搭建与基础配置
笔记·学习·harmonyos
土狗TuGou32 分钟前
SQL内功笔记 · 第5篇:SQL逻辑执行顺序
数据库·笔记·后端·sql·mysql
Zklys34 分钟前
Cmake的学习笔记step1
c++·笔记·学习