Skip to content

EasyTest : Loading Data using CSV file

Anuj Kumar edited this page Nov 12, 2013 · 2 revisions

EasyTest provides its users with the facility to load the input test data using various file types. In this WIKI page we will see how a user can provide test data to its test classes using CSV file.

In order to understand the concepts, lets take an example and walk through it:

 @RunWith(DataDrivenTestRunner.class)
 @DataLoader(filePaths={testData.csv})
 public class TestClass{
  
     @Test
     public void simplTestMethod(@Param(name="name")String name , @Param(name="age")int age , @Param(name="expectedOutput")int expectedOutput){
       ...............//your test conditions here
     }
 }

As you can see, its a simple example of a TestClass that has a single method under test. The test method takes 3 parameters : name , age and expectedOutput.

We also see that we are using @DataLoader annotation on our class, which is a way to tell the EasyTest framework about how and from where to load the test data. DataDrivenTestRunner class from EasyTest understands how to load the test data and how to provide it to the test methods.

In our case, we are telling the EasyTest framework to load the input test data from a file path "testData.csv". EasyTest will try to search for this file in your relative path folder. Next, lets look at how we can define our testData.csv file.

Here is a sample CSV file that you can look at to see how we are defining the test data: https://github.com/EaseTech/easytest/blob/master/src/test/resources/getDDTData.csv This is how we define the input test data for the method defined above in the example:

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

As you can visualize, we have in the first column, the name of the test method for which we need to define the test data. The name in this case is "simpleTestMethod". Next in the same column, we have defined the input parameter names that will be used in the test method. In this case the parameters are : name , age , expectedOutput. All these are comma seperated as is expected out of the CSV file. The next columns define the actual data associated with the test method and for each parameter. Thus, in the above case, the test method : simpleTestMethod will be executed 3 times with 3 sets of data defined in each column of the Excel file. Note the leading "," in each of the columns that define the test data. It is required to tell the EasyTest framework that it is a data column and not the column that defines a new test method and its input types.

You can have as many test methods defined in a single file as you want. You can also use as many input files to load the data from as you want.

Happy EasyTest(ing)