pytest简单使用和生成测试报告

目录

  1. 基本使用

1--安装

2--pytest书写规则

3--[为pycharm设置 以 pytest的方式运行](#为pycharm设置 以 pytest的方式运行)

4--setup和teardown

5--setup_class和teardown

  1. pytest生成测试报告

基本使用

复制代码
  #### 安装

  1. pytest文档地址
     1. [pytest documentation](https://docs.pytest.org/en/stable/ "pytest documentation")![](https://i-blog.csdnimg.cn/direct/41cc9d72349d45cd9b0a62e92f451bf0.png)
  2. pip install pytest
  3. 点击pycharm左边的控制台按钮
     1. 输入pip install pytest
  4. ![](https://i-blog.csdnimg.cn/direct/167e94988fca4fc0a041c57ce54738b7.png)
  5. 出现下面的情况就算成功了
     1. ![](https://i-blog.csdnimg.cn/direct/0413557f00a0425d8415ba70b984a580.png)
复制代码
  #### pytest书写规则

  1. 测试的文件名必须以test开头,或者结尾
     1. test_12312_demo.py
     2. asd_112_demo_test.pyy
  2. 测试类必须以Test开头
     1. TestLoginApi
复制代码
  #### 为pycharm设置 以 pytest的方式运行

  1. 使用快捷键 ctrl + alt +s 呼出pycharm设置面板
     1. 找tools
        1. python integrated tools
           1. 找到Default test Runner
              1. 设置pytest
                 1. 最后点击ok按钮
  2. ![](https://i-blog.csdnimg.cn/direct/18e4683b364f40e3908f82e215cd6afe.png)
  1. 编写一个简单的例子
    1.
    复制代码
    class TestDemo:
    
        # 定义一个测试用例
        def test_demo_001(self):
            print("这是一个测试test_demo_001")
    
        def test_demo_002(self):
            print("test_demo_002")
    1. 测试结果如下
    2. 命令行执行
      1. pytest -s .\test_demo.py
复制代码
  #### setup和teardown

  1.

     ```
     def setup(self):
         print("前置处理")
     ```

  2.

     ```
     def teardown(self):
         print("后置处理")
     ```

  3. 在执行的过程中![](https://i-blog.csdnimg.cn/direct/d71f729a2bb34abf8bd319324397cb64.png)
  4. 发现这些函数并没有执行
     1. 这个的原因是因为setup 和 teardown在pytest 8.0 以后的版本已经废弃了
     2. 可以使用setup_method和 teardown_method
        1.

           ```
           def setup_method(self):
               print("前置处理\n")

           def teardown_method(self):
               print("后置处理")
           ```

        2. ![](https://i-blog.csdnimg.cn/direct/9432b81651b548b3867a8d5fb96fe5bf.png)
     3. 运行结果![](https://i-blog.csdnimg.cn/direct/dc04f8727ed648f1a184dfa489fad2a7.png)
     4. 可以看到在每个方法执行前后都会执行
  5. pytest8.0以前的版本使用setup和teardown。pytest8.0以后的版本使用setup_method和teardown_method
复制代码
  #### setup_class和teardown

  1.
     >
     > ```
     > def setup_class(self):
     >     print("--------类级别的前置处理器---------")
     >
     > def teardown_class(self):
     >     print("--------类级别的后置处理器---------")
     > ```

  2. ![](https://i-blog.csdnimg.cn/direct/3c738f8cee134dd1b80129cdcfcf6a8e.png)
  3. 可以看到只会运行一次

pytest生成测试报告

  1. 安装测试报告插件
    1. pip install pytest-html
    2. 出现下面的情况就算安装完成了
  2. 使用指令生成测试报告
    1. pytest --html=.\reports.html .\test_demo.py
    2. pytest --html=生成报告的路径 执行测试用例的路径
    3. 打开测试报告
      1. 找到reports.html文件
        1. 鼠标右键
          1. Open in
            1. Brower
              1. Chrome
              2. 这里可以选择你安装的浏览器去打开
              3. 我这里安装了Chrome,就选择了Chrome
    4. 可以看到生成的测试报告
    5. 可以看到这些结果展示还是很直观 的
相关推荐
视觉AI8 小时前
如何查看 Linux 下正在运行的 Python 程序是哪一个
linux·人工智能·python
weixin_441455268 小时前
说说Java有哪些集合类
java·开发语言
李趣趣8 小时前
C#中关于ContextMenuStrip批量添加Item的问题
开发语言·c#
张人玉9 小时前
C# 串口通讯中 SerialPort 类的关键参数和使用方法
开发语言·c#·串口通讯
白山云北诗9 小时前
网站被攻击了怎么办?如何进行DDoS防御?
开发语言·网络安全·php·ddos·防ddos·防cc
程序定小飞9 小时前
基于springboot的作业管理系统设计与实现
java·开发语言·spring boot·后端·spring
Jonathan Star9 小时前
NestJS 是基于 Node.js 的渐进式后端框架,核心特点包括 **依赖注入、模块化架构、装饰器驱动、TypeScript 优先、与主流工具集成** 等
开发语言·javascript·node.js
晓庆的故事簿9 小时前
windows下载和使用minio,结合java和vue上传文件
java·开发语言
猫头虎9 小时前
永久免费白嫖多个域名,一键托管Cloudflare,免费申请SSL加密证书,轻松建站、搭建线路伪装
服务器·开发语言·网络·数据库·python·网络协议·ssl
沙虫一号9 小时前
线上python问题排查思路
后端·python