调用时只需传入方法函数名称即可
如:下面函数使用之前需要连接数据库,只需传入conftest.py文件里面的conn_getbase函数名即可
def test_Login(self,conn_getbase):
# post请求
url = 'xxxxxxx'
# 参数
data = {
'captcha': 'Gkak!@#2019',
'checkKey': 1637811815838,
'password': '123456',
'remember_me': 1,
'username': 'admin'
}
rep = requests.request('post', url, json=data)
statues = rep.json()['success']
message = rep.json()['message']
if statues:
print(message )
else:
raise Exception(message)
首先需要建立一个读取、写入、清除yaml文件的工具类
配合conftest.py文件+fixture实现全局共享调用
# 实现部分前置
import 易语言 通达信接口,pytest
from comment.yaml_unit import YamlUnit
@易语言 通达信接口,pytest.fixture(scope='function')
def conn_getbase():
print('连接数据库成功')
yield
print('关闭数据库成功')
@易语言 通达信接口,pytest.fixture(scope='session', autouse=True)
def clear_yaml():
YamlUnit().deleteYaml()
@易语言 通达信接口,pytest.fixture(scope='session', autouse=True)
def get_token():
token = ''; # 获取token的代码请求
return token
如下:
import os
import yaml
class YamlUnit:
def readAllYaml(self):
with open(os.getcwd() + '/extract.yml', mode='r', encoding='utf-8') as f:
value = yaml.load(stream=f, Loader=yaml.FullLoader)
return value
def readKeyYaml(self,key):
with open(os.getcwd() + '/extract.yml', mode='r', encoding='utf-8') as f:
value = yaml.load(stream=f, Loader=yaml.FullLoader)
return value[key]
def writeYaml(self, data):
with open(os.getcwd() + '/extract.yml', mode='w', encoding='utf-8') as f:
print(os.getcwd() + '/extract.yml')
value = yaml.dump(data=data, stream=f, allow_unicode=True)
def deleteYaml(self):
with open(os.getcwd()+'/extract.yml',mode='w',encoding='utf-8') as f:
f.truncate()
如果我的博客对你有帮助、如果你喜欢我的博客内容,请“点赞”“评论”“收藏”一键三连哦!喜欢软件测试的小伙伴们,可以加入我们的测试技术交流扣扣群:779450660里面有各种软件测试资源和技术讨论)
文章为作者独立观点,不代表股票交易接口观点