Reading data from a CSV file
We saw a simple data-driven test TestNG. The test data was hardcoded in the test-script code. This could become difficult to maintain. It is recommended that we store the test data separately from the test scripts. Often, we use data from the production environment for testing. This data can be exported in the CSV format. We can read these CSV files in data-provider methods and pass the data to the test instead of hardcoded object arrays.
In this example, we will use the OpenCSV library to read a CSV file. OpenCSV is a simple Java library for reading CSV files in Java. You can find more details on OpenCSV at http://opencsv.sourceforge.net/.
Let's first create a CSV file, named data.csv, in the src/test/resources/data ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access