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>
相关推荐
一条破秋裤21 分钟前
一份多光谱数据分析
笔记·数据挖掘·数据分析
随心点儿27 分钟前
使用python 将多个docx文件合并为一个word
开发语言·python·多个word合并为一个
zstar-_30 分钟前
【算法笔记】6.LeetCode-Hot100-链表专项
笔记·算法·leetcode
不学无术の码农30 分钟前
《Effective Python》第十三章 测试与调试——使用 Mock 测试具有复杂依赖的代码
开发语言·python
sleepybear111337 分钟前
在Ubuntu上从零开始编译并运行Home Assistant源码并集成HACS与小米开源的Ha Xiaomi Home
python·智能家居·小米·home assistant·米家·ha xiaomi home
纪伊路上盛名在42 分钟前
(鱼书)深度学习入门1:python入门
人工智能·python·深度学习
夏末蝉未鸣011 小时前
python transformers笔记(TrainingArguments类)
python·自然语言处理·transformer
德育处主任Pro1 小时前
「py数据分析」04如何将 Python 爬取的数据保存为 CSV 文件
数据库·python·数据分析
咸鱼鲸1 小时前
【PyTorch】PyTorch中数据准备工作(AI生成)
人工智能·pytorch·python
程序员Xu1 小时前
【OD机试题解法笔记】连续出牌数量
笔记·算法·深度优先