Selion Automation Test Implementation three (data-driven model)

Source: Internet
Author: User

The Selion Framework provides 2 of the main data-driven methods, respectively, Excel-based data driven and Yaml file-based data-driven methods, both of which have pros and cons, the following code in detail to explain the use of both methods and in the last two data-driven mode of use scenarios.

One: Excel-based data-driven approach. Because Excel facilitates the processing and management of large amounts of test data, in many automated test frameworks, Excel stores test data as a common method, especially for testing processes that are based on a large amount of data. Selion is a great way to provide support for Excel, and the perfect combination of testng Dataprovider, the basic use of the following:

Let's take the following Excel data table as an example:

* Note: Excel data format is: First behavior data header, starting from the second line as the first row of the test data, the first column is the key value of each row, the row must exist cannot be deleted, even if you do not have to keep the column, starting from the second column for the real test data.

1. Encapsulate Excel's sheet as a Java entity object, the basic code structure is as follows:

 Public classMydatasheet {PrivateString Testcaseid; PrivateString Country; PrivateString currency;  Public voidSettestcaseid (String testcaseid) { This. Testcaseid =Testcaseid; }   Public voidSetcountry (String country) { This. Country =Country; }   Public voidsetcurrency (String currency) { This. Currency =currency; }   PublicString Gettestcaseid () {returnTestcaseid; }   PublicCountry Getcountry () {returnCountry.getcountry ( This. Country); }   PublicInfo getcurrency () {returnInfo.getinstance ( This. Currency); }   PublicString toString () {return"[Testcaseid:" + Gettestcaseid (). ToString () + ", Country:" + getcountry (). ToString () + ", Currency:" + getcurrency (). ToString () + "]"; }}

* Note: When encapsulating the Java entity class of Exle worksheet, the class name is persisted with the sheet page of Excel, and the name and order of the member variables of the defined class are consistent with the name and order of the headers defined in Excle.

2. Read all of the test data in the sheet page one at a time as follows:

@DataProvider (name = "Ireadexcelsheets")publicthrows  Exception {  =    New Simpleexceldataprovider ("Src/test/resources/testdata/mydatafile.xls");   New Mydatasheet ();   return dataprovider.getallexcelrows (DataRow);}

3. The following code is used in the test method:

@Test (Dataprovider = "Ireadexcelsheets")publicvoid  Testcreateaccount (mydatasheet DataRow) {  // // the corresponding test data is obtained through the GetXXX method }

4. One way to read a specific line in Excel's sheet page is as follows:

@DataProvider (name = "Ireadexcelsheets") PublicObject[][] Myexcelsheetreader ()throwsException {simpleexceldataprovider Dataprovider=NewSimpleexceldataprovider ("Src/test/resources/testdata/mydatafile.xls"); Mydatasheet DataRow=NewMydatasheet ();//Mode 1: Read by the key value in front of the lineMydatasheet MyData =(Mydatasheet) Dataprovider.getsingleexcelrow (DataRow,C);//Mode 2: Read by line number (note the row number of the second row of data is 1, and so on)MyData = (mydatasheet) dataprovider.getsingleexcelrow (DataRow, 1); object[][] Data=NewObject[1][1]; data[0][0] =MyData; returndata;}

5. The method code for reading a specific line through the key value is as follows:

@DataProvider (name = "Ireadexcelsheets") PublicObject[][] Myexcelsheetreader ()throwsException {simpleexceldataprovider Dataprovider=NewSimpleexceldataprovider ("Src/test/resources/testdata/mydatafile.xls"); Mydatasheet DataRow=NewMydatasheet (); ArrayList<String> keylist =NewArraylist<string>(); Keylist.add (A); Keylist.add (C); Keylist.add ("D"); String[] Keyarray=(string[]) Keylist.toarray (); returndataprovider.getexcelrows (DataRow, Keyarray);}

6. How specific data is read by a specified range. (* Note: This method is my recommended use, the scope of which can be specified by the external configuration file, so as to achieve the use of configurable test data)

@DataProvider (name = "Ireadexcelsheets")publicthrows  Exception {  =    New Simpleexceldataprovider ("Src/test/resources/testdata/mydatafile.xls");   New Mydatasheet ();   return dataprovider.getexcelrows (DataRow, "1-3");}

* The test range specified in the form can be: a-b,x,y,m-n in this way, it is very convenient to flexibly specify the data that needs to be run each time in the test.

The above is the basic code and way to read Excel data in Selion.

Two: The data-driven way of YAML. YAML is an XML-like file that is the way data is stored in a structured session and is recommended in selion for storing test data. For more information about YAML files, please refer to http://www.yaml.org/start.html

1. The simplest way to use the test data file is as follows:

// The simplest test data file - string1- string2- string3-String4

1.1. The basic reading and testing code is as follows:

//Dataprovide's Code@DataProvider (name = "Yamldataprovider") PublicObject[][] Simpledataprovider ()throwsException {returnYamldataprovider.getalldata (NewYamlresource ("Src/test/resources/trialyamldatafile.yaml"));}//The test method code is as follows:@Test (Dataprovider = "Yamldataprovider") Public Static voidSinglemethod (String testdata) {//Testing Logic}

2. Test how objects are used, similar to the first Test class in Excel:

 Public classmyobject{PrivateString Testcaseid; PrivateString Country;  PublicOtherdatasheet () {} PublicString Gettestcaseid () {returnTestcaseid; }   Public voidSettestcaseid (String testcaseid) { This. Testcaseid =Testcaseid; }   PublicString getcountry () {returnCountry; }   Public voidSetcountry (String country) { This. Country =Country; }   PublicString toString () {return"[Testcaseid:" + Gettestcaseid (). ToString () + ", Country:" + getcountry (). ToString () + ", Currency:]"; }}

The 2.1 test data format is as follows:

// package. Class name specifies the test class, Key:value format specifies the data in the class -!!  Package . Myobject    testcaseid:testcaseid1    country:country1-!!  Package . Myobject    testcaseid:testcaseid2    country:country2

2.2 Data reading methods and test class code are as follows:

//Dataprovider class@DataProvider (name = "Yamldataprovider") PublicObject[][] Simpledataprovider ()throwsException {returnYamldataprovider.getalldata (NewYamlresource ("Src/test/resources/trialyamldatafile.yaml"));}//Test Class@Test (Dataprovider = "ObjectDataProvider") Public Static voidObjectconsumingmethod (MyObject MyObject) {//Testing Logic}

3. Test data based on the HASHMAP format

The format of YAML is as follows:

- test1:    testcaseid:testcaseid1    country:country1- test2:    testcaseid:testcaseid2    Country:country2

The 3.1 test classes are as follows:

@Test (Dataprovider = "Multidataprovider")publicstaticvoid Multiparametermethod (Linkedhashmap<object, object> testData) {  //  Testing Logic}

4. How to specify the test data according to the key in Yaml.

@DataProvider (name = "Dataprovider")publicthrows  Exception {  =  Getlistofrelevantkeys ();   return Yamldataprovider.getdatabykeys (    new yamlresource ("src/test/resources/ Multipledatayamldatafile.yaml "),    keyarray);}

5. How to read the test data according to the specified range:

@DataProvider (name = "Dataprovider")publicthrows  Exception {  = "1-3, 7, 10-12 ";   return Yamldataprovider.getdatabyindex (    new yamlresource ("src/test/resources/ Multipledatayamldatafile.yaml "),    indexes);}

6. Specify how multiple YAML files are used as data sources:

@DataProvider (name = "Multiplesources") Public StaticObject[][] Dataprovidergetmultiplearguments ()throwsException {List yamlresources=NewArrayList (); Yamlresources.add (NewYamlresource (PathName, userdocuments, USER.class)); Yamlresources.add (NewYamlresource (PathName, Citydocuments, city.class)); object[][] Data=NewYamldataprovider (). Getalldatamultipleargs (yamlresources); returndata;}//Test Class Code@Test Public voidmultisourcetest (user user, city) {//Testing Logic}

The above is the YAML-based data-driven approach in selion.

Three: comparison and usage scenarios of two data-driven methods.

1. Excel-based data-driven approach:

Advantages: Easy to manage test data, clear format, easy to read and external processing of data (you can write test code to detect test data or convert test data format through Excel macro function)

Cons: You cannot implement version control of test data in code management tools such as SVN.

Scenario: A workflow based on a large amount of test data requires the processing of a large amount of test data.

2. YAML-based data-driven approach:

Pros: Text files that can be version controlled.

Cons: Plain text files, data organization and readable line difference, especially when measured data is more.

Scenario: more suitable for test data, focus on workflow-based automated testing, the test system is focused on testing workflow, the test data table is a few scenarios suitable for that way.

Selion Automation Test Implementation three (data-driven model)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.