zoukankan      html  css  js  c++  java
  • 【Python + yaml】之yaml文件数据驱动(包括DDT驱动)

    写自动化测试代码中,数据驱动传递参数比较方便一些,也便于后期维护,下面介绍两种数据驱动:

    下面是test.yaml文件:

    start_HRApp:
      ip: 127.0.0.1
      port: 4723
      implicitly_wait: 10
      caps:
        android:
          platformName: Android
          #模拟器
          platformVersion: 6.0
          deviceName: OPPO
          appPackage: com.csksc2b.invertory
          appActivity: com.csks.login.SplashAty
    #      noReset: True
    #      unicodeKeyboard: True
    #      resetKeyboard: True
    #      autoGrantPermissions: True
          automationName: uiautomator2
        ios:

    ①用于一般文件的yaml数据驱动:【个人推荐这个】,它不仅可以用在测试用例,也可以用在其他py文件中

    from appium import webdriver
    import yaml
    import os
    
    def des_caps():
    
        # 基础路径
        base_dir = os.path.dirname(os.path.dirname(__file__))
        # yaml路径
        yaml_path = base_dir + "/data/ddt_data_file.yaml"
        # 获取yaml的数据
        with open(yaml_path,'r',encoding='utf-8') as file:
            data = yaml.load(file)
        start = data['start_HRApp']
        Cap = start['caps']['android']
    
        driver = webdriver.Remote("http://"+ str(start['ip']) +":"+ str(start['port']) +"/wd/hub",Cap)
        driver.implicitly_wait(10)
    
        return driver

    ========================================================= 

    yaml文件

    case01:
      url: https://ascendas.17mine.cn/basic/pick/selectPage
      headers:
        Authorization: eyJhbGciOiJIUzI1NiJ9.eyJuZWVkRWRpdCI6LTEsImxvZ2luVGltZSI6MTU5MDYyOTYwMTU0NSwibG9naW5XYXkiOjEsInVzZXJOYW1lIjoi5byg55WFIiwidXNlcklkIjoiMTI1NDI5NDE4NzAzODM0NzI2NCIsImxvZ2luU291cmNlIjotMSwiYWNjb3VudCI6IjEzNjQyMDQwNjMxIiwiZXhwIjoxNTkwNjcyODAxfQ.qydhemA3sGfrBuHFWcTi8OdaOcm7hvIpgErtkQ2OVBo
      payload:
        pageNum: 1
        pageSize: 1
        user_id: 1254294187038347264
        userId: 1254294187038347264
        infos_id: 1207504682260500480
        infoId: 1207504682260500480

    或者自定义一个方法:

        def yamlData(self):
            '''获取yaml数据'''
            self.path = os.path.dirname(os.path.abspath(__file__))
            # yaml路径
            self.yamlPath = self.path + "/data/case_data.yaml"
            # 获取yaml数据
            with open(self.yamlPath, 'r', encoding='utf-8') as file:
                data = yaml.load(file)
            return data

    然后再引用:

        def test_request01(self):
            case01 = self.yamlData()['case01']
            url = case01['url']
            payload = case01['payload']
            headers = case01['headers']
    
            r = requests.post(url,params=payload,headers=headers).json()
            self.assertEqual(r['data']['records'][0]['stockOutName'],"0506测试仓库")

    ②用于测试用例中的yaml数据驱动(DDT):

    yaml文件:

    case02:
      url: https://www.v2ex.com/api/nodes/show.json
      payload:
        name: python

    import unittest
    import requests
    import os
    import yaml
    from ddt import ddt, data, file_data, unpack
    
    
    @ddt
    class TestResquest(unittest.TestCase):
        @file_data('./data/case_data.yaml')
        @unpack
        def test_request02(self,**kwargs):
            url = kwargs['url']
            payload = kwargs['payload']
            r = requests.get(url,params=payload).json()
            self.assertEqual(r['id'],901)

    但是DDT有一点不好的是不灵活,如果有两个case的yaml,想获取url,他会把两个URL一块执行再一个用例中

    case01:
      url: https://ascendas.17mine.cn/basic/assemble/selectPage
      headers:
        Authorization: eyJhbGciOiJIUzI1NiJ9.eyJuZWVkRWRpdCI6LTEsImxvZ2luVGltZSI6MTU5MTE0NDk2NTExOSwibG9naW5XYXkiOjEsInVzZXJOYW1lIjoi5byg55WFIiwidXNlcklkIjoiMTI1NDI5NDE4NzAzODM0NzI2NCIsImxvZ2luU291cmNlIjotMSwiYWNjb3VudCI6IjEzNjQyMDQwNjMxIiwiZXhwIjoxNTkxMTg4MTY1fQ.OiSBpkRJMZsABAlKhfo4P2cmZuqk6V63vDACZBY5Xs8
      payload:
        pageNum: 1
        pageSize: 1
    #    user_id: 1254294187038347264
    #    userId: 1254294187038347264
    #    infos_id: 1207504682260500480
        infoId: 1207504682260500480
    
    
    case02:
      url: https://www.v2ex.com/api/nodes/show.json
      payload:
        name: python

     如果想测试重复的用例可以适用这个。但是变化较多的字段的用例不适用。

    如下测试重复的用例:

    用例1:
      data1:
        - keys: "yaml01"
        - keys: "yaml02"
      data2:
        - keys: "yaml03"
        - keys: "yaml04"
    用例2:
      data1:
        - keys: "yaml05"
        - keys: "yaml06"
      data2:
        - keys: "yaml07"
        - keys: "yaml08"
    用例3:
      data1:
        - keys: "yaml09"
        - keys: "yaml10"
      data2:
        - keys: "yaml11"
        - keys: "yaml12"

    代码:

    import unittest
    import requests
    import os
    import yaml
    from ddt import ddt, data, file_data, unpack
    
    
    @ddt
    class TestResquest(unittest.TestCase):
        @file_data("../data/ddt_data_file.yaml")
        @unpack
        def test_baiduSearch03(self,**kwargs):
            keys = kwargs['data1'][1]['keys']
            print("第三组测试用例:",keys)
            self.baidu_search(keys)
            self.assertEqual(self.driver.title, keys + "_百度搜索", msg="标题不正确!")

    一个用例可以执行三遍

    优缺点:

    ①一般的yaml方法,可以适用于任何文件,只是写法有点繁琐,适用于多种用例,较灵活

    ②DDT的yaml方法,只能用于测试用例文件中,写法简单,适用于一种重复性用例,不灵活。

    拓展:

    YAML、YML在线编辑(校验)器

    把yaml文件转换成json

  • 相关阅读:
    python 的基础 学习 第六天 基础数据类型的操作方法 字典
    python 的基础 学习 第五天 基础数据类型的操作方法
    python 的基础 学习 第四天 基础数据类型
    ASP.NET MVC 入门8、ModelState与数据验证
    ASP.NET MVC 入门7、Hellper与数据的提交与绑定
    ASP.NET MVC 入门6、TempData
    ASP.NET MVC 入门5、View与ViewData
    ASP.NET MVC 入门4、Controller与Action
    ASP.NET MVC 入门3、Routing
    ASP.NET MVC 入门2、项目的目录结构与核心的DLL
  • 原文地址:https://www.cnblogs.com/Owen-ET/p/12103451.html
Copyright © 2011-2022 走看看