Skip to content

Understanding @DataLoader annotation

resh123 edited this page Jul 21, 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 DataLoaderannotation. 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.

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


Input CSV file



Output


loader

Attribute Description


Javadoc


Java documentation for the filePaths attribute can be found [here]

Attribute Input


Default value


Sample Usage

Code Description


Input CSV file



Output


writeData

Attribute Description


Javadoc


Java documentation for the filePaths attribute can be found [here]

Attribute Input


Default value


Sample Usage

Code Description


Input CSV file



Output


appendData

Attribute Description


Javadoc


Java documentation for the filePaths attribute can be found [here]

Attribute Input


Default value


Sample Usage

Code Description


Input CSV file



Output


Clone this wiki locally