在SeLion框架中提供了2中主要的數(shù)據(jù)驅(qū)動方式,分別為基于Excel表的數(shù)據(jù)驅(qū)動和基于YAML文件的數(shù)據(jù)驅(qū)動方式,這兩種方式各有優(yōu)劣,下面通過代碼詳細講解這兩種方式的使用并在最后對兩種數(shù)據(jù)驅(qū)動方式的使用場景。

一:基于Excel的數(shù)據(jù)驅(qū)動方式
由于Excel便于對大量的測試數(shù)據(jù)的處理和管理,所以在很多自動化測試框架中Excel存儲測試數(shù)據(jù)是常用的方法,尤其基于大量數(shù)據(jù)的測試流程。SeLion中很好的提供了對excel的支持,并且完美的和testNg的dataprovider進行結(jié)合,基本的使用方法如下:
我們以下面的excel數(shù)據(jù)表格為例子進行講解:
excel的數(shù)據(jù)格式為:第一行為數(shù)據(jù)頭,從第二行開始為測試數(shù)據(jù)的第一行,第一列為每行的key值,該行必須存在不能刪除,即使你不用也要保留該列,從第二列開始為真正的測試數(shù)據(jù)。
1. 封裝excel的sheet為java的實體對象,基本代碼結(jié)構(gòu)如下:
public class MyDataSheet {
? private String testcaseid;
? private String country;
? private String currency;
? public void setTestcaseID(String testcaseid) {
? ? this.testcaseid = testcaseid;
? }
? public void setCountry(String country) {
? ? this.country = country;
? }
? public void setCurrency(String currency) {
? ? this.currency = currency;
? }
? public String getTestcaseID() {
? ? return testcaseid;
? }
? public Country getCountry() {
? ? return Country.getCountry(this.country);
? }
? public Info getCurrency() {
? ? return Info.getInstance(this.currency);
? }
? public String toString() {
? ? return "[testcaseid: " + getTestcaseID().toString() + ", country: "
? ? ? + getCountry().toString() + ", currency: " + getCurrency().toString() +
? ? ? "]";
? }
}
封裝exle的worksheet的java實體類的時候,該類名要和excel的sheet頁保持一直,定義的類的成員變量的名稱和順序要和excle中的定義的表頭的名稱和順序保持一致。
2. 一次讀取該sheet頁中所有的測試數(shù)據(jù)方法如下:
@DataProvider(name = "IReadExcelSheets")
public Object[][] myExcelsheetReader() throws Exception {
? SimpleExcelDataProvider dataProvider =
? ? new SimpleExcelDataProvider("src/test/resources/testdata/MyDataFile.xls");
? MyDataSheet dataRow = new MyDataSheet();
? return dataProvider.getAllExcelRows(dataRow);
}
3. 測試方法中的使用代碼如下:
@Test(dataProvider = "IReadExcelSheets")
public void testCreateAccount(MyDataSheet dataRow) {
? // Testing logic
// 通過getXXX方法得到對應(yīng)的測試數(shù)據(jù)
}
4. 一次讀取excel的sheet頁中特定行的方法如下:
@DataProvider(name = "IReadExcelSheets")
public Object[][] myExcelsheetReader() throws Exception {
? SimpleExcelDataProvider dataProvider =
? ? new SimpleExcelDataProvider("src/test/resources/testdata/MyDataFile.xls");
? MyDataSheet dataRow = new MyDataSheet();
//方式1:通過行前面的key值來讀取
? MyDataSheet myData =
? ? (MyDataSheet) dataProvider.getSingleExcelRow(dataRow, "c");
//方式2: 通過行號來讀取(注意第二行數(shù)據(jù)的行號為1,以此類推)
myData? = (MyDataSheet) dataProvider.getSingleExcelRow(dataRow, 1);
? Object[][] data = new Object[1][1];
? data[0][0] = myData;
? return data;
}
5. 通過key值讀取特定行的方法代碼如下:
@DataProvider(name = "IReadExcelSheets")
public Object[][] myExcelsheetReader() throws Exception {
? SimpleExcelDataProvider dataProvider =
? ? new SimpleExcelDataProvider("src/test/resources/testdata/MyDataFile.xls");
? MyDataSheet dataRow = new MyDataSheet();
? ArrayList<String> keyList = new ArrayList<String>();
? keyList.add("a");
? keyList.add("c");
? keyList.add("d");
? String[] keyArray = (String[]) keyList.toArray();
? return dataProvider.getExcelRows(dataRow, keyArray);
}
6. 通過指定范圍的方式讀取特定數(shù)據(jù)的方式。(*注意:該方式是我特別推薦的使用方式,其中的范圍可以通過外部配置文件指定,這樣就實現(xiàn)了可配置化的測試數(shù)據(jù)的使用方式)
@DataProvider(name = "IReadExcelSheets")
public Object[][] myExcelsheetReader() throws Exception {
? SimpleExcelDataProvider dataProvider =
? ? new SimpleExcelDataProvider("src/test/resources/testdata/MyDataFile.xls");
? MyDataSheet dataRow = new MyDataSheet();
? return dataProvider.getExcelRows(dataRow, "1-3");
}
*其中的測試范圍指定的形式可以為:a-b,x,y,m-n 通過這種方式可以實現(xiàn)非常方便的在測試中靈活指定每次需要運行的是數(shù)據(jù)。
以上是在SeLion中讀取excel數(shù)據(jù)的基本代碼和方式。
二:YAML的數(shù)據(jù)驅(qū)動方式
YAML是類似于XML的文件,是結(jié)構(gòu)話的數(shù)據(jù)存數(shù)方式,在SeLion中很推薦使用該方式存儲測試數(shù)據(jù)。關(guān)于YAML文件的詳細介紹請參考?http://www.yaml.org/start.html
1. 最簡單的使用方式,測試數(shù)據(jù)文件如下:
//最簡單的測試數(shù)據(jù)文件
- string1
- string2
- string3
- string4
1.1 . 基本的讀取和測試代碼如下:
//dataProvide的代碼
@DataProvider(name = "yamlDataProvider")
public Object[][] simpleDataProvider() throws Exception {
? return YamlDataProvider.getAllData(
? ? new YamlResource("src/test/resources/trialYamlDataFile.yaml"));
}
//測試方法代碼如下:
@Test(dataProvider = "yamlDataProvider")
public static void singlemethod(String testdata) {
? // Testing logic
}
2. 測試對象的使用方式,類似于excel中首先測試類如下:
public class Myobject{
? private String testcaseid;
? private String country;
? public OtherDataSheet() {}
? public String getTestcaseid() {
? ? return testcaseid;
? }
? public void setTestcaseid(String testcaseid) {
? ? this.testcaseid = testcaseid;
? }
? public String getCountry() {
? ? return country;
? }
? public void setCountry(String country) {
? ? this.country = country;
? }
? public String toString() {
? ? return "[testcaseid: " + getTestcaseid().toString() + ", country: "
? ? ? + getCountry().toString() + ", currency: ]";
? }
}
2.1 測試數(shù)據(jù)格式如下:
//包.類名 指定測試類, key:value格式指定類中的數(shù)據(jù)
- !!package.Myobject
? ? testcaseid : testcaseid1
? ? country : country1
- !!package.Myobject
? ? testcaseid: testcaseid2
? ? country: country2
2.2 數(shù)據(jù)讀取方式和測試類代碼如下:
//dataprovider類
@DataProvider(name = "yamlDataProvider")
public Object[][] simpleDataProvider() throws Exception {
? return YamlDataProvider.getAllData(
? ? new YamlResource("src/test/resources/trialYamlDataFile.yaml"));
}
//測試類
@Test(dataProvider = "objectDataProvider")
public static void objectConsumingMethod(MyObject myObject) {
? //Testing logic
}
3. 基于HashMap格式的測試數(shù)據(jù)
YAML的格式如下:
- test1 :
? ? testcaseid : testcaseid1
? ? country : country1
- test2 :
? ? testcaseid : testcaseid2
? ? country : country2
3.1 測試類如下:
@Test(dataProvider = "multiDataProvider")
public static void multiParameterMethod(LinkedHashMap<Object, Object> testData) {
? // Testing logic
}
4. 根據(jù)YAML中的key得到指定測試數(shù)據(jù)的方式.
@DataProvider(name = "DataProvider")
public Object[][] specificKeyDataProvider() throws Exception {
? String[] keyArray = getListOfRelevantKeys();
? return YamlDataProvider.getDataByKeys(
? ? new YamlResource("src/test/resources/multipleDataYamlDataFile.yaml"),
? ? keyArray);
}
5. 根據(jù)指定的范圍去讀測試數(shù)據(jù)的方法:
@DataProvider(name = "DataProvider")
public Object[][] notSimpleDataProvider() throws Exception {
? String indexes = "1-3,7, 10-12";
? return YamlDataProvider.getDataByIndex(
? ? new YamlResource("src/test/resources/multipleDataYamlDataFile.yaml"),
? ? indexes);
}
6. 同時指定多個YAML文件作為數(shù)據(jù)源的方式:
@DataProvider(name = "MultipleSources")
public static Object[][] dataProviderGetMultipleArguments() throws Exception {
? List yamlResources = new ArrayList();
? yamlResources.add(new YamlResource(pathName, userDocuments, USER.class));
? yamlResources.add(new YamlResource(pathName, cityDocuments, CITY.class));
? Object[][] data = new YamlDataProvider().getAllDataMultipleArgs(yamlResources);
? return data;
}
//測試類代碼
@Test
public void multiSourceTest(USER user, CITY city) {
? // Testing logic
}
以上就是SeLion中基于YAML的數(shù)據(jù)驅(qū)動方式。
三:兩種數(shù)據(jù)驅(qū)動方式的對比和使用場景
1. 基于Excel的數(shù)據(jù)驅(qū)動方式:
優(yōu)點:便于管理測試數(shù)據(jù),格式清晰明確,便于閱讀和數(shù)據(jù)外部處理(可以通過excel中宏功能編寫測試代碼檢測測試數(shù)據(jù)或者轉(zhuǎn)換測試數(shù)據(jù)格式)
缺點:無法在SVN中這樣的代碼管理工具中實現(xiàn)測試數(shù)據(jù)的版本控制。
場景:基于大量測試數(shù)據(jù)的工作流程,需要對大量的測試數(shù)據(jù)進行處理。
2. 基于YAML的數(shù)據(jù)驅(qū)動方式:
優(yōu)點:文本文件,可以實現(xiàn)版本控制。
缺點:純文本文件,數(shù)據(jù)組織性和可讀行差,尤其當測數(shù)據(jù)比較多的時候。
場景:更適合測試數(shù)據(jù)較少,注重基于工作流的自動化測試中,測試系統(tǒng)重點是測試工作流,測試數(shù)據(jù)表少的場景很適合該種方式
創(chuàng)建了一個測試交流群,如果對軟件測試、接口測試、自動化測試、面試經(jīng)驗交流感興趣可以加測試交流群:829792258,還會有同行一起技術(shù)交流