EasyTest Core : QuickStart guide

Anuj Kumar edited this page Aug 24, 2016 · 11 revisions

Lets get up and running quickly with EasyTest Core module. These are the steps you need to follow:

Step 1 : Have the latest EasyTest dependency in you pom file

<dependency>
 <groupId>org.easetech</groupId>
 <artifactId>easytest-core</artifactId>
 <version>1.4.0</version>
</dependency>`

Step 2 : Create a simple Test Class with test method

@RunWith(DataDrivenTestRunner.class)
@DataLoader(filePaths = { "org/example/data/testExcelData.xls" })
public class TestExcelDataLoader{

  private ItemService itemService = new RealItemService();

@Test
public void getExcelTestData(@Param(name="libraryId")
Float libraryId, @Param(name="itemId")
Float itemId) {
    System.out.print("Executing getExcelTestData :");
    System.out.println("LibraryId is :" + libraryId + " and Item Id is :" + itemId);
}

Alternatively, you can use TestPolicy annotation at the class level to move all the annotations to a separate reusable Test Policy class. Here is an example.

@RunWith(DataDrivenTestRunner.class)
@TestPolicy(TestExcelDataPolicy.class)
public class TestExcelDataLoader{

  @Duration(timeInMillis=1000)
  private ItemService itemService = new RealItemService();

@Test
public void getExcelTestData(@Param(name="libraryId")
Float libraryId, @Param(name="itemId")
Float itemId) {
    System.out.print("Executing getExcelTestData :");
    System.out.println("LibraryId is :" + libraryId + " and Item Id is :" + itemId);
}

And TestExcelDataPolicy class is defined as this :

 @DataLoader(filePaths = { "org/example/data/testExcelData.xls" })
 @Format(date='dd/MM/yyyy')
 @Report
 @Parallel(threads=5)
 public class TestExcelDataPolicy {
     
 } 

Step 3: Create you test data file(for above example it would be an excel file with name testExcelData.xls)

The first column of first row indicates the name of the test method for which the data needs to be supplied. 2nd and third column of the 1st row represents the name of the input parameters to the test. Row 2 and 3 represents the actual test data.

 getExcelTestData	itemId	libraryId

            	11567	91475

            	null	0

Congratulations. You have just written your first Data Driven Test. When you run the above test using Junit supported IDE, EasyTest will generate 2 Tests, one for each set of data. Thus, you have just saved yourself from writing different tests for testing with different parameters.

Next, lets extend this example and try to understand some extra features of EasyTest.

Understanding the IoC Container support in EasyTest

Instead of defining/initializing your testSubject (ItemService in the above test) in the test class itself, you can also externalize the initialization logic in a config file and inject the right instance at runtime. The advantage of doing this is that you separate the initialization logic from the test logic thereby making your tests cleaner and more maintainable. Secondly, you can reuse the externalized logic in other tests as well. Lets see how we can do it for the above test.

@RunWith(DataDrivenTestRunner.class)
@DataLoader(filePaths = { "org/example/data/testExcelData.xls" })
@TestConfigProvider({TestConfigProviderClass.class})
public class TestExcelDataLoader{

  @Inject
  private ItemService itemService;

@Test
public void getExcelTestData(@Param(name="libraryId")
Float libraryId, @Param(name="itemId")
Float itemId) {
    System.out.print("Executing getExcelTestData :");
    System.out.println("LibraryId is :" + libraryId + " and Item Id is :" + itemId);
}

Note we have added two things to the above tests:

  • @TestConfigProvider annotation
  • @Inject annotation

@TestConfigProvider annotation takes an array of Configuration provider classes from which to load the beans. In the above example the TestConfigProviderClass.class will look like the following:

public class TestConfigProviderClass {

@TestBean public ItemService itemService(){
    return new RealItemService();
}

Note also that we are using standard Javax annotation @Inject for injecting the Test Bean. EasyTest supports @Inject for injection by Type, @Named along with @Inject for injection by Name. Besides this EasyTest also supports injection by field name. EasyTest also has its own @Provided annotation in cases when the user doesnt or cant use the Javax annotations.