top of page
Concrete

Test Data in Automation Testing

  • Writer: John Chitiris
    John Chitiris
  • Feb 10, 2021
  • 2 min read

Updated: Feb 11, 2021

Test Data in Software Testing is the input given to a software program during test execution. It represents data that affects or affected by software execution while testing. Test data is used for both positive testing to verify that functions produce expected results for given inputs and for negative testing to test software ability to handle unusual, exceptional or unexpected inputs.


It's a crucial part of an automation project to find a way to initialize your beans with default values which of course can be overridden in order to create data on your app.

For example, let's say that we need to create a new User who has the following variables:

  1. first name

  2. last name

  3. email

  4. pwd

This User can be represented as a java bean like this:

public class User {

    String firstName;
    String lastName;
    String email;
    String pwd;

}

The first approach is a common simple Java one, by creating a constructor, like this :

public class User {

    String firstName;
    String lastName;
    String email;
    String pwd;

public User(String firstName, String lastName, String email, String pwd) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.email = email;
        this.pwd = pwd;
    }
}

Following this approach via the test class - script you can initialize User with values like:

User user = new User("giannis","chitiris","chitiris@mail.com","pwd");

A second spring -based approach would be to invoke spring for injecting the bean User (@Component) with default values by using the @Value annotations in order to give explicit values, like this:


@Component
@Data
public class User {

    @Value("${firstName:giannis}")
    String firstName;
    @Value("${lastName:chitiris}")
    String lastName;
    @Value("${email:chitiris@email.com}")
    String email;
    @Value("${pwd:password]}")
    String pwd;
}

@Data annotation is a lombok one that actually generates, besides others, Getters and Setters.


According to this spring -based approach the way to retrieve the spring User bean with default values would be by @Autowired it into the test script:

@Slf4j
public class TestDemo{

    @Autowired
    protected User user;

    @BeforeClass
    public void loadData(){
        user.getFirstName();
        user.getLastName();
        user.getEmail();
        user.getPwd();
    }

    @Test
    public void testStep1(){
    }
}

In this approach also, variable values can be defined or either being override from the application.properties file and finally assigned during spring bean creation.


Third approach: Define default values via lombok builder pattern(@Builder), like this:

@Data
@Builder
public class User {

    @Builder.Default  String firstName = "giannis";
    @Builder.Default  String lastName = "chitiris";
    @Builder.Default  String email = "chitiris@email.com";
    @Builder.Default  String pwd = "password";
}

According to this lombok -based approach the way to build the User bean with default values would be:

User user = User.builder().build();

Moreover, if we need to override any of the default variable value(e.g: giannis value of firstName variable) we can do it as:

User user = User.builder().firstName("John").build();

Furthermore, there are many other kinds of approaches where data are store in files, like .csv or .xml or .json(or even into databases), and map the values to the corresponding beans.


Fourth approach: Use an Object Mapper (Gson library) to convert a Json file to Java Object


So in this case we define the default variable values in a json file under the path of the resource of the spring project, like this:

{
  "firstName" : "giannis",
  "lastName"  : "chitiris",
  "email"     : "chitiris@mail.com",
  "pwd"       : "password"
}

User bean is being instantiated with default values as exist in json file in this way:

ClassPathResource resource = new ClassPathResource("user.json");
Gson gson = new Gson();
User user = gson.fromJson(new FileReader(resource.getFile()), User.class); 

In conclusion, a lot of approaches to choose from according to your project needs...




Komentar


bottom of page