Skip to main content

Data Driven test using EasyTest




We can use many methods for Data Driven testing. JUnit or TestNG has its own way to parameterize their test. Today we will discuss about a very easy method to parameterize out test. We will use a small library named EasyTest. As the name suggest it's very easy!!

We will use maven to manage out dependencies. I will use IntelliJ IDEA community edition for this project but the instructions can easily be followed by any IDE like Eclipse or NetBeans.

First create a maven project with your desired groupID and artifactID, I will use com.testautomationabc.test as groupID and data-driven-test as artifactID. Now locate you pom.xml file which is looks like this

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
         xmlns="http://maven.apache.org/POM/4.0.0"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.testautomationabc.test</groupId>
    <artifactId>data-driven-test</artifactId>
    <version>1.0-SNAPSHOT</version>
    
</project>
We will write test in JUnit so we have to add JUnit dependencies. Go to maven central repository from here and type junit in the search box. You will get desired result in return. Copy and paste dependency code in you pom.xml file under a dependencies tag. Similarly to add EasyTest dependency search for ‘easytest’ and add easytest-core under the same dependencies tag. Finally the pom.xml file is looks like this
<?xml version="1.0" encoding="UTF-8"?> 
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
         xmlns="http://maven.apache.org/POM/4.0.0"          
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.testautomationabc.test</groupId>
    <artifactId>data-driven-test</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>

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

    </dependencies>

</project>
Our project setup is done. Now what we gonna to test? We need to create a function or method for testing right? Suppose we create a method which will add two numbers.  Create a java class named it Calculator in src>main>java>your package name (optional).
File: Calculator.java


public class Calculator {
    public Double add(Double a, Double b){
        return a + b;
    }
}

Now to test this method create a class named TestCalculator in src>test>java> your package name (optional).
File: TestCalculator.java
@RunWith(DataDrivenTestRunner.class)
@Report(reportTypes = {Report.REPORT_TYPE.DEFAULT},
        outputLocation = "classpath:Reports",
        outputFormats = Report.EXPORT_FORMAT.XLS)
public class TestCalculator {
    private Calculator calculator;

    @Before    public void init() {
        calculator = new Calculator();
    }

    @Test    @DataLoader(filePaths = "DataProvider.xls", loaderType = LoaderType.EXCEL)
    public void testDataFromXLS(@Param(name = "a") Double a,
                                @Param(name = "b") Double b,
                                @Param(name = "expected") Double expected) {
        Double actualValue = calculator.add(a, b);
        Assert.assertEquals(expected, actualValue, 0.0);
    }
}
Brief explanation first line @RunWith, this is JUnit test runner to ease our test run. @Report, EasyTest report type, location and format. You can do some experiment with those lines! Here I have taken default report type, project class path as report location and excel file as report format.
@DataLoader, this is the important part of our data driven test. First put you file path with extension, this may be xls, xml or csv which contains all test data. LoaderType should correspond to your data provider file. For example if your data provider file is excel then loader type should be excel or if your data provider is csv then loader type is csv. Your test method name, here testDataFromXLS is the first column of your excel data provider then second column name, third column and so on. Finally a JUnit assert will do the trick. Run the test and view the report. Report will be like this:

You can download the data provider excel file from here

References: 
https://github.com/EaseTech/easytest-core 
https://www.youtube.com/watch?v=Bq52fNXwuvs 

Special thanks to Shantonu Sarker for inspiring me.


Comments

Most Popular

REST API functional and performance test using Jmeter

Let's start with some basics about API. An API – application programming interface –  allows your product or service to talk to other products or services. In this way, an API allows you to open up data and functionality to other developers and to other businesses. As API is a bridge between you application with third parties so it's functional, performance and security test is very important. Required tool set: 1. Your favorite IDE (Sprig Tool Suite 3.8.2 in my case) 2. MySQL database engine 3.  Latest version of JMeter 4. Postman (optional) There are lots of public APIs are available but we will use our own to have a better understanding how a RESTful API works. Clone or download this project: https://github.com/muhin/RestWebService.git. It's a sprint boot REST web service. Use your favorite IDE to import as maven project. You can also use spring tool suite . Now create a database in MySQL named country and then use the following scripts to make this proces...