-
Notifications
You must be signed in to change notification settings - Fork 21
Understanding @DataLoader annotation
EasyTest provides its users with the facility to do Data Driven Testing in Java and uses JUnit classes behind the scene to achieve its objective. One major annotation in EasyTest is the DataLoader annotation. This is a class/method level annotation that can be used to tell the EasyTest framework how and from where to load the test data. This page describes this annotation in detail and explains with code samples how each of its attributes can be used.
Following is the list of attributes of the Dataloader annotation:
- filePaths – This attribute specifies list of files representing the input test data for the given test method.
- loaderType - The type of file that contains the data.
- Loader - The custom Loader class that will be used by EasyTest to load the test data
- writeData - Boolean identifying whether the data should be written back to the file or not. Default behavior is that the data will be written back to the file.
- appendData - Boolean identifying whether data specified in two different files for the same method should be appended or replaced. Default behavior is to replace the data present in one file from the other. @return whether data should be appended or not
The subsequent sections explain each attribute in detail.
Attribute Description
Filepaths attribute specifies the list of files containing the input test data for the given test method. Multiple files may be specified here. Note that currently EasyTest supports files of type CSV, EXCEL and XML. if users wish to load data from some other filetype, they can do so by specifying a custom loader. More details about this are specified in the description of the loader attribute below.
Javadoc
Java documentation for the filePaths attribute can be found here.
Attribute Input
This attribute accepts a String[] i.e. a String array of filenames.
Default value
Default value is an empty array. However it is necessary to specify a value here, otherwise an exception will occur.
The following is a code listing which demonstrates how this attribute may be used:
`
package easytech.demo;
import org.easetech.easytest.annotation.DataLoader;
import org.easetech.easytest.annotation.Param;
import org.easetech.easytest.runner.DataDrivenTestRunner;
import org.junit.Test;
import org.junit.runner.RunWith;
@RunWith(DataDrivenTestRunner.class)
@DataLoader(filePaths={"testData.csv"})
public class DataLoadderFilePathsDemo{
@Test
public void simplTestMethod(@Param(name="name")String name , @Param(name="age")int age , @Param(name="expectedOutput")int expectedOutput){
}
}
`
Code Description
- The DataLoadderFilePathsDemo class is annotated with the DataLoader annotation. The filePaths attribute is used with a value of “testData.csv”. This indicates that the input data for testing should be used from this file. In this example a csv file is used, but an excel/xml file may also be used.
- A method called simpleTestMethod is defined. This is the actual test method that needs to be invoked with different data sets. It accepts 3 parameters called name , age , expectedOutput. The values for these parameters will be obtained from the input CSV file.
Input CSV file
The following is the input CSV file used in this example:
simplTestMethod,name,age,expectedOutput
,Ravi,32,1
,Christiaan,29,2
,Anuj,31,0
The first column specifies the name of the method for which the test data is required, in this case "simpleTestMethod". Next in the same column, the input parameter names that are used in the test method are defined. In this case the parameters are : name , age , expectedOutput. All these are comma separated as required for a CSV file. The subsequent records define the actual data associated with the test method and for each parameter.
Output
When this code is run, the test method i.e. simpleTestMethod will be executed as many times as there are records in the input file. So in this case since there are 3 records in the input file, the test will be run 3 times.
Attribute Description
LoaderType attribute specifies the type of file that will have the input data. This annotation is not really required as EasyTest can look up the file extension to figure out the file type. LoaderType is present merely to give hint to EasyTest. It is also used to specify that the loader is Custom and not a standard loader.
Javadoc
Java documentation for the loaderType attribute can be found here.
Attribute Input
The input to this attribute is an enum constant of type LoaderType
So briefly, this attribute can have the following values:
-
LoaderType.CSV - Identifies that the type of file is a framework based CSV file.
-
LoaderType.CUSTOM - Identifies that the type of file is a user defined custom type.
-
LoaderType.EXCEL - Identifies that the type of file is a framework based EXCEL file.
-
LoaderType.NONE - Identifies that the user has not specified any specific loader type.
-
LoaderType.XML - Identifies that the type of file is a framework based XML file.
Default value
The default value for this attribute is LoaderType.NONE Note that this is an optional attribute, so NONE indicates that the user has not specified any loader type.
`
package easytech.demo;
import org.easetech.easytest.annotation.DataLoader;
import org.easetech.easytest.annotation.Param;
import org.easetech.easytest.loader.LoaderType;
import org.easetech.easytest.runner.DataDrivenTestRunner;
import org.junit.Test;
import org.junit.runner.RunWith;
@RunWith(DataDrivenTestRunner.class)
@DataLoader(filePaths={"testData.csv"},loaderType=LoaderType.CSV)
public class DataLoaderLoaderTypeDemo{
private Businesslogic businessLogic = new Businesslogic();
@Test
public void simplTestMethod(@Param(name="name")String name , @Param(name="age")int age , @Param(name="expectedOutput")int expectedOutput){
businessLogic.saveData(name, age, expectedOutput);
}
}
`
Code Description
- The DataLoaderLoaderTypeDemo class is annotated with the DataLoader annotation.
- The filePaths attribute is used to specify the path of the input CSV file.
- The loaderType attribute is specified with value as LoaderType.CSV. This indicates that the input file is a csv file.
- A method called simpleTestMethod is defined. This is the actual test method that needs to be invoked with different data sets. It accepts 3 parameters called name , age , expectedOutput which will be obtained from the input CSV file.
Input CSV file
The following is the input CSV file used in this example:
simplTestMethod,name,age,expectedOutput
,Ravi,32,1
,Christiaan,29,2
,Anuj,31,0
The first column specifies the name of the method for which the test data is required, in this case "simpleTestMethod". Next in the same column, the input parameter names that are used in the test method are defined. In this case the parameters are : name , age , expectedOutput. All these are comma separated as required for a CSV file. The subsequent records define the actual data associated with the test method and for each parameter.
Output
When this code is run, the test method i.e. simpleTestMethod will be executed as many times as there are records in the input file. So in this case since there are 3 records in the input file, the test will be run 3 times.
Attribute Description
Javadoc
Java documentation for the filePaths attribute can be found [here]
Attribute Input
Default value
Code Description
Input CSV file
Output
Attribute Description
This is a Boolean attribute identifying whether the data should be written back to the input file or not. Default value is true indicating that the data will be written back to the file. Note that data will be written back to the file only if the test method returns something. In case the test method returns void, then even though this attribute is set to true, nothing will be written back to the file. In case we do not need the data to be written back to the file, we need to set this flag to false.
Javadoc
The Javadoc for this attribute can be found here
Attribute Input
This attribute accepts a boolean value.
Default value
The default value of this attribute is true.
The following is a code listing which demonstrates how this attribute may be used: `
package easytech.demo;
import org.easetech.easytest.annotation.DataLoader;
import org.easetech.easytest.annotation.Param;
import org.easetech.easytest.runner.DataDrivenTestRunner;
import org.junit.Test;
import org.junit.runner.RunWith;
@RunWith(DataDrivenTestRunner.class)
@DataLoader(filePaths={"testData.csv"})
public class DataLoaderWriteDataDemo{
@Test
public int simplTestMethod(@Param(name="name")String name , @Param(name="age")int age , @Param(name="expectedOutput")int expectedOutput){
return 5;
}
}
`
Code Description
- The DataLoaderWriteDataDemo class is annotated with the DataLoader annotation. The filePaths attribute is used to specify the path of the input CSV file.
- Note that the writeData attribute is not really specified, since it defaults to true. So by default data will be written back to the input file. If we do not want this data to be written to the input file, we need to set this attribute to false as follows:
@DataLoader(filePaths={"testData.csv"},writeData=false) - A method called simpleTestMethod is defined. This is the actual test method that needs to be invoked with different data sets. It accepts 3 parameters called name , age , expectedOutput which will be obtained from the input CSV file. It returns an integer value. For demo purpose, this is hard-coded to 5. This value which is returned by this method will be written back to the input CSV file once the test is executed.
Input CSV file
The following is the input CSV file used in this example:
simplTestMethod,name,age,expectedOutput
,Ravi,32,1
,Christiaan,29,2
,Anuj,31,0
The first column specifies the name of the method for which the test data is required, in this case "simpleTestMethod". Next in the same column, the input parameter names that are used in the test method are defined. In this case the parameters are : name , age , expectedOutput. All these are comma separated as required for a CSV file. The subsequent records define the actual data associated with the test method and for each parameter.
Output
When this code is run, the test method i.e. simpleTestMethod will be executed as many times as there are records in the input file. Also, the input file will be modified as follows:
`
simplTestMethod,name,age,expectedOutput,ActualResult,Duration(ms)
"",Ravi,32,1,5,1.0
"",Christiaan,29,2,5,0.0
"",Anuj,31,0,5,0.0
` As can be seen, 2 additional columns are added to the input file:
- Actual result – which has the value returned by the simpleTestMethod
- Duration – This specifies the duration of each run of the simpleTestMethod
Attribute Description
Javadoc
Java documentation for the filePaths attribute can be found [here]
Attribute Input
Default value
Code Description
Input CSV file
Output
For any queries/issues/clarifications please contact anujkumar@easetech.org