导入方式

  1. from autoTestScheme import case

请注意:需要让用例继承该方法

autoTestScheme 前置条件

  1. auto_setup_01 # request等未注册前
  2. auto_setup_02 # request等注册后

用例编写规范

autoTestScheme采用pytest编写规范,

pytest编写规范:

  1. 1.文件名需要已test开头
  2. 2.类名需要已Test开头
  3. 3.方法名需要已test开头

pytest的一些前置后置条件

  1. 模块级(setup_module/teardown_module)开始于模块始末,全局的
  2. 函数级(setup_function/teardown_function)只对函数用例生效(不在类中)
  3. 类级(setup_class/teardown_class)只在类中前后运行一次(在类中)
  4. 方法级(setup_method/teardown_method)开始于方法始末(在类中)
  5. 类里面的(setup/teardown)运行在调用方法的前后

autoTestScheme对于用例额外的定义

  1. # 1. 需要已类的形式进行测试,如下
  2. # 2.方法内需要定义入参,data_conversion, data
  3. # 3.使用data_conversion 获取数据源
  4. from autoTestScheme import case
  5. class Test(case.Base):
  6. def test_01(self, data_conversion):
  7. ...

data_conversion类说明

data_conversion返回的是一个字典,内容为data内对应数据,他包含的方法如下:

  1. class NewDict(dict):
  2. def get(self, *args):
  3. if len(args) <= 1:
  4. return super().get(args[0])
  5. else:
  6. result = []
  7. for i in args:
  8. result.append(super().get(i))
  9. return tuple(result)
  10. def merge(self, _dict: dict):
  11. self.replace_dict(self, _dict)
  12. def append_tmp(self, dpTmp):
  13. _id = self.get('id')
  14. tmp.tmp.append(_id, dpTmp)
  15. def replace_dict(self, _json1: dict, _json2: dict):
  16. for k in list(_json2.keys()):
  17. v = _json2[k]
  18. if k not in list(_json1.keys()) or type(v) != dict:
  19. _json1[k] = v
  20. elif type(v) == dict:
  21. _json1[k] = self.replace_dict(_json1[k], _json2[k])
  22. return _json1

通用

在case内可使用如下一些方法

  1. self.settings.run.current_tag # 当前用例的标签
  2. self.settings.run.current_env # 标签对应环境

case内的方法介绍及使用

  1. class Base(Parse):
  2. logger = logger # 日志使用,见日志模块
  3. settings: conf.BaseDynaconf = conf.settings # 读取配置使用,见Dynaconf
  4. faker = Faker(locale='zh_CN') # 常用库,详见官方文档https://faker.readthedocs.io/en/stable/
  5. def check_response(self, response, outs):
  6. ...
  7. # 比较response与outs的区别,比较内容如下(outs < response)
  8. # 1.根据key去做对比,如果outs内的key在response内不存在则会报错,如果response内key在outs不存在则只会发出警告
  9. # 2.遍历比较,一直到最底层为字符串
  10. # 3.需要注意值的类型会做比较
  11. def check_response_by_sql(self, response, outs):
  12. # 与check_response的区别在于会将response/outs的key都进行大驼峰转换
  13. def get_unique_identification(self):
  14. """
  15. 获取唯一标识,多线程可用,用于性能测试使用
  16. """
  17. def convert_uppercase(self, string):
  18. """
  19. 将字符串转换成大写
  20. """
  21. @property
  22. def not_repeat_string(self):
  23. """
  24. 获取不重复字符串
  25. @return:
  26. """
  27. return '{}_{}_{}'.format(''.join(random.sample('zyxwvutsrqponmlkjihgfedcba', 4)),
  28. ''.join(random.sample('zyxwvutsrqponmlkjihgfedcba', 4)),
  29. str(float(time.time())))
  30. @classmethod
  31. def current_subtle_unix(self):
  32. # 当前微妙时间
  33. def generate_email(self, domain='163.com'):
  34. """
  35. 获取一个随机邮箱号
  36. :param domain:域名,默认163邮箱
  37. :return:
  38. """
  39. @property
  40. @classmethod
  41. def current_unix(self):
  42. # 当前时间
  43. @property
  44. def current_subtle_unix_str(self):
  45. # 当前微妙时间
  46. # 等待1微妙防止重复
  47. @property
  48. def current_subtle_str_unix(self):
  49. # 当前微妙时间(字符串类型)
  50. # 等待1微妙防止重复
  51. def get_age_unix(self, num, is_positive=False):
  52. '''
  53. 获取一个年龄超过n岁的时间
  54. is_positive 为false,取当前时间往前n年的时间
  55. is_positive 为true,取当前时间往后n年的时间
  56. '''
  57. @property
  58. def yesterday_start_unix(self):
  59. # 昨天开始时间
  60. def get_start_unix(self, day):
  61. '''
  62. 获取n天前的开始时间
  63. @param day: n,提前n天
  64. @return:unix时间戳
  65. '''
  66. def get_start_date(self, day, format='%Y-%m-%d %H:%M:%S'):
  67. '''
  68. 获取n天前的开始时间
  69. @param day: n,提前n天
  70. @param format: 时间格式
  71. @return:字符串时间
  72. '''
  73. def get_end_unix(self, day):
  74. '''
  75. 获取n天前的结束时间
  76. @param day: n,提前n天
  77. @return:unix时间戳
  78. '''
  79. def get_end_date(self, day, format='%Y-%m-%d %H:%M:%S'):
  80. '''
  81. 获取n天前的结束时间
  82. @param day: n,提前n天
  83. @param format: 时间格式
  84. @return:字符串时间
  85. '''
  86. @property
  87. def yesterday_end_unix(self):
  88. # 昨天结束时间
  89. @property
  90. def today_start_unix(self):
  91. # 今天开始时间
  92. @property
  93. def today_end_unix(self):
  94. # 今天结束时间
  95. @property
  96. def tomorrow_start_unix(self):
  97. # 明天开始时间戳
  98. @property
  99. def tomorrow_end_unix(self):
  100. # 明天结束时间
  101. def strp_date_by_unix(self, date, format='%Y-%m-%d'):
  102. '''
  103. 将字符串时间或datetime时间转换为unix时间
  104. @param date:字符串时间
  105. @param format:字符串时间格式,默认%Y-%m-%d,其他参考值%Y-%m-%d %H:%M:%S.%f
  106. @return:
  107. '''
  108. def current_local_date_str(self, format='%Y-%m-%d %H:%M:%S'):
  109. '''
  110. 获取当前时间
  111. @param format: 返回格式,默认%Y-%m-%d %H:%M:%S
  112. @return:
  113. '''
  114. def strp_unix_by_date(self, date, format='%Y-%m-%d %H:%M:%S'):
  115. '''
  116. 将unix时间转换为字符串时间
  117. @param date:unix时间
  118. @param format:字符串时间格式,默认%Y-%m-%d,其他参考值%Y-%m-%d %H:%M:%S
  119. @return:
  120. '''
  121. def _sum(self, *args):
  122. '''
  123. 求和,此函数解决float相加或相减
  124. @param args:
  125. @return:
  126. '''