您的位置:首页 > 教育 > 锐评 > 旧房改造室内装修设计公司_南宁站建好就够用_百度官方推广_公司做网站推广

旧房改造室内装修设计公司_南宁站建好就够用_百度官方推广_公司做网站推广

2025/2/23 4:27:34 来源:https://blog.csdn.net/weixin_46096113/article/details/144037787  浏览:    关键词:旧房改造室内装修设计公司_南宁站建好就够用_百度官方推广_公司做网站推广
旧房改造室内装修设计公司_南宁站建好就够用_百度官方推广_公司做网站推广

我们在日常工作中如果有一个登陆接口 在查看别的b接口 我们就要频繁的 进行登陆操作

比如登陆-加入购车

登陆-查看某个模块

登陆-加入购物车 等等

如果每次都要写登陆接口 这样会产生大量冗余的代码 所以我们就可以定一个全局变量 就不需要每次都写 直接调用就可以

举个例子

那我们要如何调用name这个变量呢?总不能每次写个方法都要定义一个变量吧?

所以为了方便,我们可以用global  

但是从接口层面上讲 是不建议这样做的

全局变量:先声明 后赋值 

我们可以用fixture 注意 这里一定要写在coftest文件中

写入方法:@pytest.fixture(scope='session')

这里scope是应用范围session这指的是作用域(fun、class、 modle、session)

然后我们就可以把我们登陆的代码直接就放在里面

末尾我们需要return来返回返回值然后我们再

我们上一步已经设置了全局变量(登陆的token),那接下来的每个接口都需要这个token值

我们回到平常的接口用例中进行传值

这里注意在conftest文件中设置的全局变脸 我们创建了一个方法my_token

然后我们把新的变量名字传给原来的token就好啦~

其实看原来的token传值 我们创建一个一样名字的变量赋值给它就好啦 这样我们就不需要来回改动原来的token值

然后我们就可以生成allure运行就好啦

附:conftest文件中也可以写日志哦~

代码如下

# 用例文件import requests
import jsonpath
import alluredef test_login(my_token):token_ret = my_tokenwith allure.step('第二步,加入购物车'):url = 'http://www.gggg.com/index.php?s=/save'params = {"application": "app","application_client_type": "weixin",'token': token_ret}data = {"goods_id": "4g","spec": [{"type": "3","value": "3"},],"stock": 1}ret = requests.post(url=url, params=params, data=data)# print(ret.text)mag_ret = jsonpath.jsonpath(ret.json(), '$.msg')[0]print('当前提取数据为:', mag_ret)assert mag_ret == '加入成功', '失败,返回的的是:{mag_ret}'
# 全局变量文件
import allure
import pytest
import requests
import jsonpath@pytest.fixture(scope='session')
def my_token():with allure.step('第一步,登陆操作'):url = "http://www.huhuhu.com/index.php?s=/login"params = {"application": "app","application_client_ty1pe": "weixin"}data = {'accounts': 'nihao','pwd': '1256','type': 'username'}# 发送请求ret = requests.post(url, params=params, data=data)# 获取我们的相应数据.msgmag_ret = jsonpath.jsonpath(ret.json(), '$.msg')[0]print("当前提取的数据为:", mag_ret)# 根据对应的.msg进行断言处理assert mag_ret == "登录成功", f'错误,我们当前返回的值是:{mag_ret}'# 获取tokentoken_ret = jsonpath.jsonpath(ret.json(), '$..token')[0]print("当前提取的数据为:", token_ret)return token_ret
# 运行文件
import subprocess
import pytestpy_args = ['-s',  # 禁用所有捕获和输出重定向'-v',  # 显示详细的信息# '--capture=sys',  # 输出调试信息,设置级别,打开实时输出(根据需要启用)'--alluredir=allure-results',  # 执行过程的数据存放在allure-results中# '--clean-alluredir'  # 如果需要清理alluredir,请确保此选项在--alluredir之前(但这不是pytest的标准选项,可能是自定义的)
]print('run pytest with args:', py_args)# 使用pytest.main()函数运行测试
if __name__ == "__main__":pytest.main(py_args)# 生成测试报告try:result = subprocess.run(['allure', 'generate', '-c', '-o', 'allure-report'], check=True)print("Allure report generated successfully.")except subprocess.CalledProcessError as e:print(f"Failed to generate Allure report: {e}")# 尝试合并报告(如果combine_allure是有效的)try:from allure_combine import combine_allure  # 确保在需要时才导入,避免在模块导入时出错combine_allure('./allure-report')except ImportError:print("combine_allure module is not available. Skipping report combining.")except Exception as e:print(f"An error occurred while combining reports: {e}")

好啦 就到这里啦

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com