httprunner系列04——环境变量env和脚本函数debugtalk.py的使用

前言:你一向地在回忆过去,怎么拥抱未来~

 一、环境变量的使用
1.1 创建.env文件,将需要设置为环境变量的值,存储在env中

1.2 使用${ENV(变量名)}调用环境变量

二、脚本函数 debugtalk.py

ps:debugtalk.py文件在哪,哪里就是根路径

2.1  创建一debugtalk.py文件,编写好脚本
import requests

def get_token():
    get_params_value={
                               'grant_tpye': 'client_credential',
                               'appid': '***',
                               'secret': '*****'
                           }

    response =requests.get(url='https://api.weixin.qq.com/cgi-bin/token',
                           params=get_params_value)

    return response.json()['access_token']

if __name__=='__main__':
    print(get_token())
2.2  将脚本中的方法引用到其他处,格式${方法名},即能实现参数化
- config:
    name: 获取token操作
    base_url: https://api.weixin.qq.com
    variables:
      - token: ${get_token()}

注意:运行测试后,测试报告文件夹(reports)会生成在当前工作目录CWD 

原文地址:https://www.cnblogs.com/miaoxiaochao/p/13111731.html