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. 可以看到这些结果展示还是很直观 的
相关推荐
我要精通C++7 分钟前
lua虚拟机的垃圾回收机制
java·开发语言
22jimmy9 分钟前
MyBatis动态sql
java·开发语言·mybatis
那我掉的头发算什么35 分钟前
【数据结构】双向链表
java·开发语言·数据结构·链表·intellij-idea·idea
拾光Ծ36 分钟前
【C++】STL之list模拟实现:关于链表容器的双向迭代器你知道多少?
开发语言·数据结构·c++·list·visual studio
666HZ66636 分钟前
Java Stream流
java·开发语言
数模加油站38 分钟前
最新R(4.4.1)及R-studio保姆级安装配置详细教程及常见问题解答
开发语言·windows·数学建模·r语言
编程指南针1 小时前
2026新选题-基于Python的老年病医疗数据分析系统的设计与实现(数据采集+可视化分析)
开发语言·python·病历分析·医疗病历分析
reasonsummer2 小时前
【办公类-116-01】20250929家长会PPT(Python快速批量制作16:9PPT相册,带文件名,照片横版和竖版)
java·数据库·python·powerpoint
拉姆哥的小屋2 小时前
基于提示学习的多模态情感分析系统:从MULT到PromptModel的华丽升级
python·深度学习·学习
暴力求解2 小时前
数据结构---栈和队列详解(上)
开发语言·数据结构·c++