Skip to content

Understanding @DataLoader annotation

resh123 edited this page Jul 23, 2014 · 57 revisions

Introduction

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.

Dataloader attributes

Following is the list of attributes of the Dataloader annotation:

  1. filePaths – This attribute specifies list of files representing the input test data for the given test method.
  2. loaderType - The type of file that contains the data.
  3. Loader - The custom Loader class that will be used by EasyTest to load the test data
  4. 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.
  5. 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.

filePaths

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.

Sample Usage

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


  1. 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.
  2. 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.

loaderType

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:

  1. LoaderType.CSV - Identifies that the type of file is a framework based CSV file.

  2. LoaderType.CUSTOM - Identifies that the type of file is a user defined custom type.

  3. LoaderType.EXCEL - Identifies that the type of file is a framework based EXCEL file.

  4. LoaderType.NONE - Identifies that the user has not specified any specific loader type.

  5. 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.

Sample Usage

`

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


  1. The DataLoaderLoaderTypeDemo class is annotated with the DataLoader annotation.
  2. The filePaths attribute is used to specify the path of the input CSV file.
  3. The loaderType attribute is specified with value as LoaderType.CSV. This indicates that the input file is a csv file.
  4. 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.

loader

Attribute Description

This attribute specifies the custom loader that will be used by EasyTest to load the test data. As mentioned previously, EasyTest currently supports loading data from CSV,XML and Excel files. If you want data to be loaded from some other source, then you can use define your own custom loader class and specify the name of the class as the value of this attribute.


Javadoc


Java documentation for the loader attribute can be found here

Attribute Input


Name of the class that will be used to load the custom data.

Default value


The default value of this attribute is org.easetech.easytest.loader.EmptyLoader.class. Note that this attribute is optional. So if not specified, it defaults to EmptyLoader.class

Sample Usage

`

package easytech.demo;

import org.easetech.easytest.annotation.DataLoader;
import org.easetech.easytest.runner.DataDrivenTestRunner;
import org.junit.Test;
import org.junit.runner.RunWith;

@RunWith(DataDrivenTestRunner.class)

public class DataLoaderCustomLoaderDemo{
 
	@Test
	@DataLoader(loader = CustomObjectDataLoader.class)
	public void testGetItemsWithCustomLoader(ItemId itemid){
 	System.out.println("Item id="+itemid.getValue());
	}
}

`

Code Description


  1. The DataLoaderCustomLoaderDemo is annotated with the loader attribute. It is assigned the name of the class which will load the data for this test.
  2. The testGetItemsWithCustomLoader method accepts one parameter which is a type of a class called Itemid.
  3. The CustomObjectDataLoader class specifies how to read data from the ItemId class and invoke this test multiple times for all the item ids

**Input **


Output


writeData

Attribute Description

This is a Boolean attribute which specifies whether the data returned by a test method should be written back to the input file. It defaults to true indicating that the data should 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 which indicates that the output of the test method should be written back to the input file.

Sample Usage

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


  1. The DataLoaderWriteDataDemo class is annotated with the DataLoader annotation. The filePaths attribute is used to specify the path of the input CSV file.
  2. 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)

  1. 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. So this value 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:

  1. Actual result – which has the value returned by the simpleTestMethod
  2. Duration – This specifies the duration of each run of the simpleTestMethod

appendData

Attribute Description

This attribute specifies whether data specified in two different files for the same method should be appended or replaced. If this flag is set to true, then data in the first file will be appended with the data in the second file before the test is run. So the test will be executed for both sets of data. Default value of this flag is false which indicates that data should be replaced. So the data in the first file will be replaced by the data in the 2nd file before the test is run. So the test will be executed only for the data in the 2nd file.


Javadoc


Java documentation for the appendData attribute can be found here

Attribute Input


This attribute accepts a boolean value

Default value


The default value of this attribute is false which indicates that data from the second file should not be appended to the data in the first file. Note that this attribute is optional.

Sample Usage

Code Description


`

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","testData1.csv"},appendData=true)
 public class DataLoaderAppendDataDemo{
	 
	
     @Test
     public void simplTestMethod(@Param(name="name")String name , @Param(name="age")int age , @Param(name="expectedOutput")int expectedOutput){
    	 
     }
 }

`

Code Description


  1. The DataLoadderFilePathsDemo class is annotated with the DataLoader annotation. The filePaths attribute is used to specify the path of the input CSV files .In this case 2 files are specified, testData.csv and testData1.csv
  2. The appendData attribute is used and is set to a value of true in order to specify that the data from both the input files should be used.
  3. 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.

Input CSV file


This example uses 2 CSV files as input.

testData.csv is as follows:

`

simplTestMethod,name,age,expectedOutput
                        	,Ravi,32,1
                        	,Christiaan,29,2
                        	,Anuj,31,0

`

testData1.csv is as follows:

`

simplTestMethod,name,age,expectedOutput
                        	,Ravi2,32,1
                        	,Christiaan2,29,2
                        	,Anuj2,31,0

`

Output


Since appendData is set to true, data from both files will be used. So the test will be run 6 times, 3 times for the data in testData.csv file and 3 times for the data in testData1.csv file. Note, in case we do not want data from both files to be used, this attribute can be set to false(or left out as it defaults to false). This will cause the data from testData1.csv file to be written to testData.csv file and then the tests will be executed. So the test will be executed only for the overwritten data in testData.csv.

Clone this wiki locally