zoukankan      html  css  js  c++  java
  • requests接口自动化-pytest框架

    pytest框架规则

    • 测试文件以test_开头或者以_test结尾
    • 测试类以Test开头,并且不能带有init方法
    • 测试函数以test_开头
    • 断言使用assert

    pytest框架运行用例

    运行单个文件

    运行多个文件

    运行整个目录

    import pytest
    
    if __name__=="__main__":
        # 运行单个文件,添加对应文件的路径,使用相对路径
        pytest.main(['../test_requests/test_assert.py'])  # ../  run_case目录与test_requests属于同于层级,先回到上层目录,在进入test_requests
        # 运行多个文件,添加对应文件的路径,使用列表形式
        pytest.main(['../test_requests/test_assert.py','../test_requests/test_assert1.py'])
        # 运行整个目录
        pytest.main('../test_requests')
    
    

    pytest动态关联,定义为全局变量

    把需要调用的值定义为全局变量,后面接口进行应用

    第一:如定义在类或者函数体外,在函数或者类中引用需要用到 global声明
    
    temp_t = "ceshi"
    
    def  tmp1():
    
        global temp_t
    
        temp_t =1
    
    print temp_t 
    
    结果:1
    
    第二:直接在函数或类中定义,需要先执行函数或者类,才能执行输出
    
    def a():
        global cc
        cc = "bb"
    
    def b():
        global cc
        cc = "kk"
    a()
    b()
    print(cc)结果:kk
    

    pytest生成报告

    生成html报告 '--html=../report/report.html'

    • pytest-html安装

    • 生成报告


      执行以下代码:
    import pytest
    
    if __name__=="__main__":
        # 生成hmtl报告,后面为路径和报告文件名称,'--html=../report/report.html'
        pytest.main(['../test_case/','--html=../report/report.html'])
    

    生成xml报告 '--junitxml=../report/report.xml'



    生成allure报告 '--alluredir','../report/reportallure/'

    首先安装pytest-allure
    再下载allure工具包
    1.下载安装包
    2.解压安装包

    3.配置环境变量
    把bin路径配置到环境变量

    4.生成报告,运行用例加上:'--alluredir','../report/reportallure/'

    5.进入报告目录

    6.运行生成报告命令

    (venv) D:Testpythonhogwarts_TD>cd report
    
    (venv) D:Testpythonhogwarts_TD
    eport>allure generate ./reportallure/ -o ./reporthtml/ --clean
    Report successfully generated to .
    eporthtml
    



  • 相关阅读:
    深入理解java虚拟机(7)---线程安全 & 锁优化
    深入理解java虚拟机(6)---内存模型与线程 & Volatile
    java注解框架
    敏捷软件开发---闲话敏捷
    深入理解java虚拟机(5)---字节码执行引擎
    龙应台写给儿子安德烈的信
    wojilu中的路由
    如何使用豆约翰博客备份专家批量下载新浪微博
    haha
    http://www.jsbreakouts.org/---各种html5框架实现打砖块游戏 breakout
  • 原文地址:https://www.cnblogs.com/Uni-Hoang/p/13199266.html
Copyright © 2011-2022 走看看