导入方式
from autoTestScheme import case
请注意:需要让用例继承该方法
autoTestScheme 前置条件
auto_setup_01 # request等未注册前
auto_setup_02 # request等注册后
用例编写规范
autoTestScheme采用pytest编写规范,
pytest编写规范:
1.文件名需要已test开头
2.类名需要已Test开头
3.方法名需要已test开头
pytest的一些前置后置条件
模块级(setup_module/teardown_module)开始于模块始末,全局的
函数级(setup_function/teardown_function)只对函数用例生效(不在类中)
类级(setup_class/teardown_class)只在类中前后运行一次(在类中)
方法级(setup_method/teardown_method)开始于方法始末(在类中)
类里面的(setup/teardown)运行在调用方法的前后
autoTestScheme对于用例额外的定义
# 1. 需要已类的形式进行测试,如下
# 2.方法内需要定义入参,data_conversion, data
# 3.使用data_conversion 获取数据源
from autoTestScheme import case
class Test(case.Base):
def test_01(self, data_conversion):
...
data_conversion类说明
data_conversion返回的是一个字典,内容为data内对应数据,他包含的方法如下:
class NewDict(dict):
def get(self, *args):
if len(args) <= 1:
return super().get(args[0])
else:
result = []
for i in args:
result.append(super().get(i))
return tuple(result)
def merge(self, _dict: dict):
self.replace_dict(self, _dict)
def append_tmp(self, dpTmp):
_id = self.get('id')
tmp.tmp.append(_id, dpTmp)
def replace_dict(self, _json1: dict, _json2: dict):
for k in list(_json2.keys()):
v = _json2[k]
if k not in list(_json1.keys()) or type(v) != dict:
_json1[k] = v
elif type(v) == dict:
_json1[k] = self.replace_dict(_json1[k], _json2[k])
return _json1
通用
在case内可使用如下一些方法
self.settings.run.current_tag # 当前用例的标签
self.settings.run.current_env # 标签对应环境
case内的方法介绍及使用
class Base(Parse):
logger = logger # 日志使用,见日志模块
settings: conf.BaseDynaconf = conf.settings # 读取配置使用,见Dynaconf
faker = Faker(locale='zh_CN') # 常用库,详见官方文档https://faker.readthedocs.io/en/stable/
def check_response(self, response, outs):
...
# 比较response与outs的区别,比较内容如下(outs < response)
# 1.根据key去做对比,如果outs内的key在response内不存在则会报错,如果response内key在outs不存在则只会发出警告
# 2.遍历比较,一直到最底层为字符串
# 3.需要注意值的类型会做比较
def check_response_by_sql(self, response, outs):
# 与check_response的区别在于会将response/outs的key都进行大驼峰转换
def get_unique_identification(self):
"""
获取唯一标识,多线程可用,用于性能测试使用
"""
def convert_uppercase(self, string):
"""
将字符串转换成大写
"""
@property
def not_repeat_string(self):
"""
获取不重复字符串
@return:
"""
return '{}_{}_{}'.format(''.join(random.sample('zyxwvutsrqponmlkjihgfedcba', 4)),
''.join(random.sample('zyxwvutsrqponmlkjihgfedcba', 4)),
str(float(time.time())))
@classmethod
def current_subtle_unix(self):
# 当前微妙时间
def generate_email(self, domain='163.com'):
"""
获取一个随机邮箱号
:param domain:域名,默认163邮箱
:return:
"""
@property
@classmethod
def current_unix(self):
# 当前时间
@property
def current_subtle_unix_str(self):
# 当前微妙时间
# 等待1微妙防止重复
@property
def current_subtle_str_unix(self):
# 当前微妙时间(字符串类型)
# 等待1微妙防止重复
def get_age_unix(self, num, is_positive=False):
'''
获取一个年龄超过n岁的时间
is_positive 为false,取当前时间往前n年的时间
is_positive 为true,取当前时间往后n年的时间
'''
@property
def yesterday_start_unix(self):
# 昨天开始时间
def get_start_unix(self, day):
'''
获取n天前的开始时间
@param day: n,提前n天
@return:unix时间戳
'''
def get_start_date(self, day, format='%Y-%m-%d %H:%M:%S'):
'''
获取n天前的开始时间
@param day: n,提前n天
@param format: 时间格式
@return:字符串时间
'''
def get_end_unix(self, day):
'''
获取n天前的结束时间
@param day: n,提前n天
@return:unix时间戳
'''
def get_end_date(self, day, format='%Y-%m-%d %H:%M:%S'):
'''
获取n天前的结束时间
@param day: n,提前n天
@param format: 时间格式
@return:字符串时间
'''
@property
def yesterday_end_unix(self):
# 昨天结束时间
@property
def today_start_unix(self):
# 今天开始时间
@property
def today_end_unix(self):
# 今天结束时间
@property
def tomorrow_start_unix(self):
# 明天开始时间戳
@property
def tomorrow_end_unix(self):
# 明天结束时间
def strp_date_by_unix(self, date, format='%Y-%m-%d'):
'''
将字符串时间或datetime时间转换为unix时间
@param date:字符串时间
@param format:字符串时间格式,默认%Y-%m-%d,其他参考值%Y-%m-%d %H:%M:%S.%f
@return:
'''
def current_local_date_str(self, format='%Y-%m-%d %H:%M:%S'):
'''
获取当前时间
@param format: 返回格式,默认%Y-%m-%d %H:%M:%S
@return:
'''
def strp_unix_by_date(self, date, format='%Y-%m-%d %H:%M:%S'):
'''
将unix时间转换为字符串时间
@param date:unix时间
@param format:字符串时间格式,默认%Y-%m-%d,其他参考值%Y-%m-%d %H:%M:%S
@return:
'''
def _sum(self, *args):
'''
求和,此函数解决float相加或相减
@param args:
@return:
'''