【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数据驱动:

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数据驱动(DDT):

from ddt import ddt, data, file_data, unpack
import unittest


@ddt
class TestBaiduSearch(unittest.TestCase):

    # 参数化例3:yaml
    @file_data("../data/ddt_data_file.yaml")
    @unpack
    def test_baiduSearch03(self, **kwargs):
        ip = kwargs['ip']
        print(ip)


if __name__ == '__main__':
    unittest.main()

【Python + yaml】之yaml文件数据驱动(包括DDT驱动)_第1张图片

 

你可能感兴趣的:(【Python + yaml】之yaml文件数据驱动(包括DDT驱动))