美文网首页
python 18数据驱动自动化接口测试进阶

python 18数据驱动自动化接口测试进阶

作者: 6c0fe9142f09 | 来源:发表于2018-08-27 17:24 被阅读36次

数据驱动自动化接口测试进阶

上一章我们实现的接口自动化只能对单个yaml文件生效,如果我们有一堆yaml文件,就抓瞎了,所以诞生了这篇进阶篇

思路
  • 前置数据
- 很多yaml文件(login.yaml、regiser.yaml)
- 模板文件
  • 通过yaml文件和模板文件生成.py类
  • 通过unittest.defaultTestLoader.discover加载所有的测试类
  • 执行所有的用例
1.测试数据
  • login_case.yaml
-
  url : http://118.24.3.40/api/user/login  # 访问url
  method : post  # 请求方式
  detail : 登录接口-成功用例  # 用例描述
  header:
  cookie:
  is_json : False
  data:  # 请求数据
    username : niuhanyang
    passwd : aA123456
  check:  # 检查点
      - sign
      - userId

-
  url : http://118.24.3.40/api/user/login  # 访问url
  method : post  # 请求方式
  detail : 登录接口-失败用例  # 用例描述
  header:
  cookie:
  is_json : False
  data:  # 请求数据
    username : niuhanyang
    passwd : aA1234567
  check:  # 检查点
      - 用户名/密码错误
      - error_code
  • regiser_case.yaml
-
  url : http://118.24.3.40/api/user/login  # 访问url
  method : post  # 请求方式
  detail : 注册接口-成功用例  # 用例描述
  header:
  cookie:
  is_json : False
  data:  # 请求数据
    username : niuhanyang
    passwd : aA123456
  check:  # 检查点
      - sign
      - userId

-
  url : http://118.24.3.40/api/user/login  # 访问url
  method : post  # 请求方式
  detail : 注册接口-失败用例  # 用例描述
  header:
  cookie:
  is_json : False
  data:  # 请求数据
    username : niuhanyang
    passwd : aA1234567
  check:  # 检查点
      - 用户名/密码错误
      - error_code
2.模板文件
  • case_template(在之前的基础上去除执行器,并且将测试类名和文件名都进行%s替换,方便我们后续处理)
import unittest
import ddt
import requests

@ddt.ddt
class %s(unittest.TestCase):
    @ddt.file_data(r'%s') # 导入yaml用例文件
    def test_interface(self,**test_data):
        url = test_data.get('url',None)
        method = test_data.get('method',None)
        detail = test_data.get('detail',None)
        self._testMethodDoc = detail    # ※添加用例描述
        header = test_data.get('header',None)
        cookie = test_data.get('cookie',None)
        is_json = test_data.get("is_json",None)
        data = test_data.get('data',None)
        check = test_data.get('check',None)

        if method:
            method = str(method).upper()
            if method == "GET":
                res = requests.get(url=url,params=data,headers=header,cookies=cookie)
            elif method == "POST":
                if is_json:
                    res = requests.post(url=url,json=data,headers=header,cookies=cookie)
                else:
                    res = requests.post(url=url,data=data,headers=header,cookies=cookie)
            else:
                res = "CASE WRONG,CASEDATA:"+url+method+detail+header+cookie+is_json+data
        else:
            res = "CASE WRONG,CASEDATA:"+url+method+detail+header+cookie+is_json+data

        # 比较接口执行结果
        for c in check:
            self.assertIn(c,res.text,detail+'--测试执行失败,'+c+'检查失败')
3.将模板文件和yaml数据处理成测试类
  • 读取cases文件夹下所有以.yaml结尾的文件
  • 获取文件名
  • 读取模板文件
  • 将文件名和文件地址传入到模板文件中
  • 保存为数据文件.py结尾的文件
def create_py():
    all_file = glob.glob('../cases/*.yaml')
    all_class_name = []
    for file in all_file:
        file_name = file.split('%s'%os.sep).pop().replace('.yaml','')
        all_class_name.append(file_name)

    with open('../data/case_template',encoding='utf-8') as f:
        template_str = f.read()

    for class_name in all_class_name:
        with open(os.path.join('../cases/',class_name+'.py'),'w',encoding='utf-8') as fw:
            template_coding = template_str % (class_name.capitalize(), os.path.join('../cases/',class_name+'.yaml'))
            fw.write(template_coding)

create_py()
4.执行用例
  • 通过unittest.defaultTestLoader.discover('../cases/', "*.py")读取所有的测试类文件
  • 将所有的case添加到suit中
  • 执行器执行
def run_all_case():
    test_suit = unittest.TestSuite()
    all_case = unittest.defaultTestLoader.discover('../cases/', "*.py")
    [test_suit.addTest(case) for case in all_case]
    print("加载%s条用例" % test_suit.countTestCases())
    runner = BeautifulReport.BeautifulReport(test_suit)
    runner.report(description="测试用例描述", filename="report.html")
5.开始执行吧
create_py()
run_all_case()
6.report.html

相关文章

网友评论

      本文标题:python 18数据驱动自动化接口测试进阶

      本文链接:https://www.haomeiwen.com/subject/dadtwftx.html