寫自動化測試代碼中,數據驅動傳遞參數比較方便一些,也便於后期維護,下面介紹兩種數據驅動:
下面是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文件轉換成json

