Pytest 测试框架

Pytest 是什么?

  • pytest 能够支持简单的单元测试和复杂的功能测试;
  • pytest 可以结合 Requests 实现接口测试; 结合 Selenium、Appium 实现自动化功能测试;
  • 使用 pytest 结合 Allure 集成到 Jenkins 中可以实现持续集成。
  • pytest 支持 315 种以上的插件;

 

为什么要选择 Pytest

  • 丰富的第三方插件
    • 报告
    • 多线程
    • 顺序控制
  • 简单灵活
  • 兼容 unittest
  • 定制化插件开发

Pytest 安装与准备

Pytest 环境安装

  • 前提:本地已配置完成Python环境
  • 第一种方式 pip install pytest
  • 第二种方式 PyCharm 直接安装

运行第一个脚本

# content of test_sample.py
def inc(x):    
    return x + 1  
def test_answer():    
    assert inc(3) == 5

 

 运行输入:

D:\Project\WebDemo>pytest test_demo.py
======================================== test session starts ========================================
platform win32 -- Python 3.8.2, pytest-6.1.2, py-1.9.0, pluggy-0.13.1
rootdir: D:\Project\WebDemo
collected 1 item                                                                                     

test_demo.py F                                                                                 [100%]

============================================= FAILURES ==============================================
____________________________________________ test_answer ____________________________________________

    def test_answer():
>       assert inc(3) == 5
E       assert 4 == 5
E        +  where 4 = inc(3)

test_demo.py:7: AssertionError
====================================== short test summary info ======================================
FAILED test_demo.py::test_answer - assert 4 == 5
========================================= 1 failed in 0.08s =========================================

 

posted @   lms21  阅读(76)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
点击右上角即可分享
微信分享提示