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>
相关推荐
0_0梅伊阁诗人32 分钟前
Flask
开发语言·数据库·python·flask
Katzelala3 小时前
[K8S学习笔记] Service和Ingress的关系
笔记·学习·kubernetes
HAH-HAH5 小时前
【Python 入门】(2)Python 语言基础(变量)
开发语言·python·学习·青少年编程·个人开发·变量·python 语法
递归不收敛5 小时前
PyCharm项目上传GitHub仓库(笔记)
笔记·pycharm·github
递归不收敛5 小时前
一、Java 基础入门:从 0 到 1 认识 Java(详细笔记)
java·开发语言·笔记
SunnyDays10115 小时前
Python 轻松实现替换或修改 PDF 文字
python·替换pdf文字·修改pdf·修改pdf文字
Just_Paranoid6 小时前
【Settings】恢复出厂设置密码校验
android·python·settings·sha256·hmac-sha256
xian_wwq6 小时前
【学习笔记】Https证书如何升级到国密
笔记·学习·证书
一又四分之一.7 小时前
hexo文章
笔记
西猫雷婶7 小时前
pytorch基本运算-Python控制流梯度运算
人工智能·pytorch·python·深度学习·神经网络·机器学习