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. 可以看到这些结果展示还是很直观 的
相关推荐
weixin_3077791314 分钟前
C++进程监视器与自动启动程序
开发语言·c++·算法
花妖大人21 分钟前
Python和LLM问题
python·llm
草莓熊Lotso34 分钟前
【C语言强化训练16天】--从基础到进阶的蜕变之旅:Day12
c语言·开发语言·c++·刷题
不喜欢学数学er44 分钟前
算法第五十三天:图论part04(第十一章)
开发语言·python·图论
你怎么知道我是队长1 小时前
python---构造函数、析构函数
开发语言·python
CF14年老兵1 小时前
深入浅出 Python 一等函数:一份友好的全面解析
后端·python·trae
heeheeai1 小时前
jvm对象内存占用
开发语言·jvm·内存分析
ALex_zry1 小时前
Golang云端编程深度指南:架构本质与高阶实践
开发语言·架构·golang
jumin18061 小时前
python采用jdbc连接oracle
python·oracle
Evand J1 小时前
【PSINS工具箱】MATLAB例程,二维平面上的组合导航,EKF融合速度、位置和IMU数据,4维观测量
开发语言·matlab·平面