Can you help me with some ideas on how to deal more efficent with test data used as precondition for automated api tests? I can’t use api calls for creating the data, so I would have to use sql scripts. However, as every test requires quite different test data(data to be inserted in 4,5 tables; more in some cases) I don’t know how to proceed to be efficient. I will try to group somehow tests that might use same data and only run the scripts once before that group of tests and delete them after(with @before class perhaps. What are your approaches when you can t use api calls and you have to deal with large data sets? Btw, I’m using java, rest assured, junit
1 Answer 1
You can create a CSV file and put your test data in that file. Now, you can read the data from the CSV file and use it in your test case.
Create a CSV file with your test data. I am using the following as an example:
Name,Emp Id,Department
abc,123,xyz
You can modify the data in your CSV file according to your test requirements.
Use the following code as a reference:
public class YourTestClass {
@ParameterizedTest
@CsvFileSource(resources = "/your_data_file.csv", numLinesToSkip = 1)
// Assuming your CSV file is in the resources directory and the first line contains headers
public void testMethod(String name, int empId, String department) {
System.out.println("Name: " + name + ", Emp Id: " + empId + ", Department: " + department);
// Add your assertions or test logic here
// For example:
assertEquals("xyz", department);
}
}
Alternatively, if you have a large list of test data, you can use a CSV parsing library to read the data from the file and store it in a map. Below is an example:
import com.opencsv.CSVReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
public class TestDataLoader {
public static Map<String, Object[]> loadTestData(String filePath) {
Map<String, Object[]> testDataMap = new HashMap<>();
try (CSVReader reader = new CSVReader(new FileReader(filePath))) {
String[] headers = reader.readNext(); // Assuming the first line contains headers
String[] nextLine;
int index = 0;
while ((nextLine = reader.readNext()) != null) {
Object[] rowData = new Object[headers.length];
for (int i = 0; i < headers.length; i++) {
rowData[i] = nextLine[i];
}
testDataMap.put("Test" + index++, rowData);
}
} catch (IOException e) {
e.printStackTrace();
// Handle the exception according to your requirements
}
return testDataMap;
}
}
Use the map in your JUnit test class:
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import java.util.Map;
import static org.junit.Assert.assertEquals;
@RunWith(Parameterized.class)
public class YourTestClass {
private String name;
private int empId;
private String department;
// Constructor to receive the parameters
public YourTestClass(String name, int empId, String department) {
this.name = name;
this.empId = empId;
this.department = department;
}
@Test
public void testMethod() {
// Your test logic using the parameters
System.out.println("Name: " + name + ", Emp Id: " + empId + ", Department: " + department);
// Add your assertions or test logic here
// For example:
assertEquals("xyz", department);
}
@Parameterized.Parameters
public static Map<String, Object[]> data() {
String filePath = "path/to/testdata.csv"; // Update with the actual path
return TestDataLoader.loadTestData(filePath);
}
}
With this approach, there is no need to hard-code test data within the code or scripts. Test data can be easily updated in the CSV file whenever any changes occur.
Explore related questions
See similar questions with these tags.