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 小时前
代账数据的可移植性设计:开放接口、账套导出与 exit strategy 工程笔记(上海 5 家样本对照)
笔记·策略模式
枫彩7 小时前
A股数据源怎么选?用 Python 验收 AI 复盘的日期与明细
python·ai agent·股票数据·mcp
江湖人称菠萝包7 小时前
【Windows】《深入浅出Windows API程序设计:编程基础篇》笔记-Chapter8-子窗口控件
windows·笔记
2601_962382437 小时前
Python 代码不可不知的函数式编程技术
python·函数式编程·高阶函数·嵌套函数·头等函数
2601_962078037 小时前
Python办公自动化与数据分析实战 培训班2021年
python·数据分析·办公自动化·自动化办公·实战演练
M78佐菲7 小时前
HTML学习笔记
linux·笔记·学习·tcp/ip·html
用户938515635077 小时前
Text2SQL 实战:用 DeepSeek 大模型实现自然语言查询 SQLite 数据库
python·sqlite
2601_962099688 小时前
Python环境下中文分词实现与应用探索
python·自然语言处理·中文分词·jieba·开源社区
2601_966949658 小时前
批量获取多只股票五档盘口的极简方案:QuantDash Python SDK 实战指南
开发语言·python·数据分析·pandas·量化交易·股票数据·quantdash
用户019027581618 小时前
如何用 Python 监控 A 股涨停跌停与封板/炸板?
python