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. 可以看到这些结果展示还是很直观 的
相关推荐
a1117766 小时前
医院挂号预约系统(开源 Fastapi+vue2)
前端·vue.js·python·html5·fastapi
0思必得06 小时前
[Web自动化] Selenium处理iframe和frame
前端·爬虫·python·selenium·自动化·web自动化
virus59456 小时前
悟空CRM mybatis-3.5.3-mapper.dtd错误解决方案
java·开发语言·mybatis
初次见面我叫泰隆6 小时前
Qt——3、常用控件
开发语言·qt·客户端
无小道7 小时前
Qt——QWidget
开发语言·qt
时艰.7 小时前
Java 并发编程之 CAS 与 Atomic 原子操作类
java·开发语言
梵刹古音8 小时前
【C语言】 函数基础与定义
c语言·开发语言·算法
摘星编程8 小时前
OpenHarmony + RN:Calendar日期选择功能
python
梵刹古音8 小时前
【C语言】 结构化编程与选择结构
c语言·开发语言·嵌入式
Yvonne爱编码8 小时前
JAVA数据结构 DAY3-List接口
java·开发语言·windows·python