June 17, 2013

EasyTest: unit testing with externalized data files and reporting

EasyTest and reporting

EasyTest is a data driven test framework on top of JUnit. It has capabilities to externalize the test data from the unit tests so that the test classes remain very clean.
EasyTest exists already for quite some time and it has proven its value given the fact that it's easy to use.

Enumeration of some benefits of using EasyTest:
  • decouple the test data from the test classes, this means clean test classes
  • collaboration of testers and developers is made easier: the tester can define the input and expected output values in an externalized data file
  • simple annotating the unit test classes for data driven features and reporting
  • available in the maven respository
In this blog we'd like to explain the reporting features of EasyTest.

We'll use a very simple business use case: converting fahrenheit to celsius.

Business

public class TemperatureConverter {

 public int toCelsius(int fahrenheit) {
  return (fahrenheit - 32) * 5 / 9;
 }

}

Test data (temperatureConversionData.csv)

testToCelsiusConverter,fahrenheit,celsius
,0,-17
,1,-17
,2,-16
,3,-16
,4,-15
,5,-15
,6,-14
,7,-13
,8,-13
,9,-12
The first column is the unit test method name, the second column is the fahrenheit value and the third column is its corresponding celsius value.

Unit test

@RunWith(DataDrivenTestRunner.class)
@DataLoader(filePaths = { "data/temperatureConversionData.csv" })
@Report(outputFormats={Report.EXPORT_FORMAT.PDF}, outputLocation="classpath:org/easytech/easytest/output")
public class TestTemperatureConverter {

 private TemperatureConverter subject;

    @Before
    public void setup() {
        subject = new TemperatureConverter();
    }

    @Test
    public void testToCelsiusConverter(@Param(name = "fahrenheit") int fahrenheit, @Param(name = "celsius") int celsiusResult) {
  // execute the business method
        int celsius = subject.toCelsius(fahrenheit);

  System.out.println(fahrenheit + " Fahrenheit = " + celsius + " Celsius");

  // asserts the result from the business method with the celsius result that comes from the input data file
        Assert.assertEquals(celsiusResult, celsius);
    }
}
In this unit test, we want to test the proper business behaviour of converting fahrenheit to celsius.
EasyTest pulls the csv data through the test method. In this case, the test method runs 10 times.

Reporting

EasyTest has made it easy to create reports with the unit test results.
The unit test report gives the following information in one overview:
  • unit test class name
  • unit test methods
  • input values
  • output values
  • passed / failed / exception
Reporting can be activated by (1) annotations in the test class or by (2) added command line parameters using maven.
(1) Adding @Report annotation
By adding the @Report annotation, reports will be written to a certain location. Output formats are PDF, XLS and HTML. The default is PDF.
The reports are written to the output location. This can be in the classpath or on a disk location. Default is the current location.

(2) Adding command parameters
EasyTest makes it possible to produce reports by added commandline parameters.
E.g.
mavan clean install -Dreports.generate -Dreports.format=pdf, -Dreports.location=classpath:org/easytech/easytest/output

outputFormat and outputLocation both are optional parameters.

Ouput result


Conclusion

We have presented an easy use case of unit testing a business method by the EasyTest data driven test library.
EasyTest is a library that can be used to run unit test, with test data that is existing in an externalized data file.
One of the features of EasyTest is reporting. By adding a class based annotation or command line parameters, reports are generated that give you a an overview of which reports passed and which ones failed.


Other EasyTest blogs

http://www.dzone.com/links/r/easytesting_in_java_a_data_driven_testing_framewo.html
http://www.dzone.com/links/r/easytesting_in_java_externalize_your_test_configu.html
http://www.dzone.com/links/r/automated_data_driven_testing_easytest_a_case_stu.html

EasyTest home

EasyTest is available in the GitHub, including more extensive documentation
https://github.com/EaseTech/easytest-core

5 comments :

  1. You have typing mistake "TemperatureConvertor" should be TemperatureConverter (with E) to make it coherent with the class u are actually atempting to test.
    Then u have major logic mistake that u want to test a method that is not even there in the class that u test. U want to test "ToCelsiusConverter" while you only have in your "business" use case a method "toFahrenheit" - so this whole example is broken. Anyway after fixing this I'm still having the following error:

    java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory

    ReplyDelete
    Replies
    1. Do you have SLF4J in your class path?

      Delete
  2. Thanks for you comment. I have updated the mistakes. It should work now.

    ReplyDelete
  3. I submitted a post on stackflow with my problem:
    http://stackoverflow.com/questions/27233070/parameterizedassertionerror-using-easytest-when-test-case-fails

    FWIW I'm using easytest-core 1.3.1

    ReplyDelete
  4. Hey. i am new to Easy Test Framework. Is the approach for XML files containing the input values the same. Could u let me know how to go about utilizing inputs stored in XML files with this framework

    ReplyDelete