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

python 17 数据驱动自动化接口测试

作者: 6c0fe9142f09 | 来源:发表于2018-08-27 16:47 被阅读60次

数据驱动自动化接口测试

1.测试用例设计
  • 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.测试类书写
  • ddt
    ddt是python的数据驱动模块
@ddt.ddt  # 给测试类添加装饰器
@ddt.file_data  # 添加数据驱动数据,被修饰的方法可以通过**XXX获取参数
  • 添加用例描述
self._testMethodDoc = detail
  • demo

@ddt.ddt
class TestClass(unittest.TestCase):
    @ddt.file_data(r'../cases/login_case.yaml') # 导入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+'检查失败')
  • 通过添加测试类到testsuit执行
test_suit = unittest.TestSuite()
test_suit.addTest(unittest.makeSuite(TestClass))
report = BeautifulReport.BeautifulReport(test_suit)
report.report(description="接口测试报告",filename='report.html')

相关文章

网友评论

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

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