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>
相关推荐
2501_941111511 小时前
高级爬虫技巧:处理JavaScript渲染(Selenium)
jvm·数据库·python
bitbrowser2 小时前
哪些指纹浏览器支持模拟SSL指纹
网络·python·网络协议·ssl
limenga1023 小时前
TensorFlow Keras:快速搭建神经网络模型
人工智能·python·深度学习·神经网络·机器学习·tensorflow
心软小念4 小时前
用Python requests库玩转接口自动化测试!测试工程师的实战秘籍
java·开发语言·python
sanggou5 小时前
【Python爬虫】手把手教你从零开始写爬虫,小白也能轻松学会!(附完整源码)
开发语言·爬虫·python
geng_zhaoying6 小时前
在VPython中使用向量计算3D物体移动
python·3d·vpython
AA陈超6 小时前
UE5笔记:GetWorld()->SpawnActorDeferred()
c++·笔记·学习·ue5·虚幻引擎
半tour费6 小时前
TextCNN-NPU移植与性能优化实战
python·深度学习·分类·cnn·华为云
普通网友6 小时前
使用Flask快速搭建轻量级Web应用
jvm·数据库·python
百锦再6 小时前
第17章 模式与匹配
开发语言·后端·python·rust·django·内存·抽象