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
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,-12The 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
(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.htmlhttp://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 documentationhttps://github.com/EaseTech/easytest-core