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



原文地址:https://www.cnblogs.com/Uni-Hoang/p/13199266.html