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. 可以看到这些结果展示还是很直观 的
相关推荐
lixzest1 小时前
快速梳理遗留项目
java·c++·python
xnglan2 小时前
使用爬虫获取游戏的iframe地址
开发语言·爬虫·python·学习
zhysunny2 小时前
04.建造者模式的终极手册:从快餐定制到航天飞船的组装哲学
java·开发语言·建造者模式
郝学胜-神的一滴2 小时前
建造者模式:构建复杂对象的优雅方式
开发语言·c++·程序人生·建造者模式
AAIshangyanxiu2 小时前
最新基于R语言结构方程模型分析与实践技术应用
开发语言·r语言·结构方程模型·生态统计学
cwn_3 小时前
Sequential 损失函数 反向传播 优化器 模型的使用修改保存加载
人工智能·pytorch·python·深度学习·机器学习
老鱼说AI3 小时前
Transformer Masked loss原理精讲及其PyTorch逐行实现
人工智能·pytorch·python·深度学习·transformer
lxmyzzs3 小时前
【已解决】YOLO11模型转wts时报错:PytorchStreamReader failed reading zip archive
人工智能·python·深度学习·神经网络·目标检测·计算机视觉·bug
Fly-ping4 小时前
【前端】JavaScript 的事件循环 (Event Loop)
开发语言·前端·javascript
范纹杉想快点毕业4 小时前
基于C语言的Zynq SOC FPGA嵌入式裸机设计和开发教程
c语言·开发语言·数据库·嵌入式硬件·qt·fpga开发·嵌入式实时数据库