Reading Test Data From Files

Một phần của tài liệu Practical unit testing with JUnit and mockito (Trang 154 - 157)

Chapter 6. Things You Should Know

6.13. Reading Test Data From Files

A commonly arising issue concerns the reading of data for tests from CSV or Excel files. This is especially important if the data has been provided by someone else (e.g. QA people, or maybe even your client). The likelihood of them using Excel (or its Open/LibreOffice counterparts) is quite high.

Of course, it is possible to come up with a custom solution and leverage libraries like OpenCSV26, SuperCSV27, Apache POI28 or JExcelApi29. These libraries help you to read data from CSV and Excel files – but still, we would need to use them within our test code. This is neither hard, nor time consuming, but since we have some ready solutions, let us have a look at them.

For both CSV and Excel files we will use the same test case: to verify whether our software calculates valid discounts based on the total value of a purchase made by a client.

25DSL - Domain Specific Language, http://en.wikipedia.org/wiki/Domain-specific_language

26http://opencsv.sourceforge.net/

27http://supercsv.sourceforge.net/

28http://poi.apache.org/

29http://jexcelapi.sourceforge.net/

Chapter 6. Things You Should Know

6.13.1. CSV Files

As for the CSV files, we can make use of what the JUnitParams library offers. Up to now we have been using its @Parameters annotation with the method attribute, to point out a method responsible for providing test data. This time we will use another annotation - @FileParameters - in order to use a CSV file as a test data reservoir.

First of all, we need a CSV file with test data. Every row should contain a set of data (in our case, this will be purchase value and percentage discount) separated by a comma. An example is given below:

financial.csv.

value,discount 0.00,0

999.99,0 1000.00,0.01 1999.99,0.01 2000.00,0.02 2999.99,0.02 5000.00,0.03 10000.00,0.03 378433.00,0.03

A header which will be ignored.

Now, we want to test whether the DiscountCalculator class, when given the first value from a row, returns a discount equal to the second value from the same row.

Listing 6.49. Reading test data from CSV file

import junitparams.FileParameters;

import junitparams.JUnitParamsRunner;

import junitparams.mappers.CsvWithHeaderMapper;

import org.junit.Test;

import org.junit.runner.RunWith;

import static org.junit.Assert.assertEquals;

@RunWith(JUnitParamsRunner.class) public class ReadCSVJUnitParamsTest { @Test

@FileParameters(value = "classpath:financial.csv", mapper = CsvWithHeaderMapper.class)

public void shouldCalculateDiscount(double value, double discount) { assertEquals(discount,

DiscountCalculator.calculateDiscount(value), 0.0001);

} }

Importings as required, including the annotation and the

Chapter 6. Things You Should Know And that is it! All we have to do now is run the test.

JUnitParams also allows to specify an absolute path (e.g. /home/mynick/data/testdata.csv).

As usual when pointing to files make sure you do not make a typo providing their name and/

or path!

6.13.2. Excel Files

Like with CSV, there are many ways to read test data from an Excel file. For example, one could write a custom implementation of the DataMapper class (part of the JUnitParams library), and use it exactly as we have used the CsvWithHeaderMapper class in the previous example. In this section I would like to present another solution based on annotations provided by another useful library: the EasyTest project.

Let us start with an Excel file which contains the test data. The first column of this file is required by the EasyTest library, and must be identical to the name of a test method that is going to use the data from this file.

Figure 6.1. Excel data file (created with LibreOffice)

The EasyTest project offers more than this: for example, it is possible to have data for many test methods within one Excel file. Please read EasyTest’s documentation to learn more.

The following listing shows how to read test data from this Excel file using annotations provided by the EasyTest project. As you can see, the test method is named shouldCalculateDiscount(), which is exactly the same name that appeared in the Excel file.

Chapter 6. Things You Should Know

Listing 6.50. Test reading data from an Excel file

import org.easetech.easytest.annotation.DataLoader;

import org.easetech.easytest.annotation.Param;

import org.easetech.easytest.loader.LoaderType;

import org.easetech.easytest.runner.DataDrivenTestRunner;

import org.junit.Test;

import org.junit.runner.RunWith;

import static org.junit.Assert.assertEquals;

@RunWith(DataDrivenTestRunner.class) public class ReadExcelTest {

@Test

@DataLoader(

filePaths = {"com/practicalunittesting/chp06/excel/financial.xls"}, loaderType = LoaderType.EXCEL)

public void shouldCalculateDiscount(

@Param(name="value") String value,

@Param(name="discount") String discount) { assertEquals(Double.parseDouble(discount), DiscountCalculator.calculateDiscount(

Double.parseDouble(value)), 0.0001);

} }

Annotations as required.

A special runner must be used which makes JUnit cooperate with EasyTest when running this test.

Some EasyTest-specific annotations. The first attribute - filePaths - describes the file that contains the test data, and the second attribute - loaderType - points to a specific class which knows how to handle this type of file.

EasyTest requires you to use the @Param annotation next to all parameters of the test method.

After running this test executes exactly as other parameterized tests we have seen so far. The only difference is that it takes parameters from the Excel file.

Một phần của tài liệu Practical unit testing with JUnit and mockito (Trang 154 - 157)

Tải bản đầy đủ (PDF)

(310 trang)