How Snapshot Testing can help to detect Errors

With snapshot technology, the comparison of the data can be outsourced as a technical aspect. If deviations are found when comparing the data, they are automatically analyzed and displayed in the log in a form that the user can understand. This allows the programmer to understand the cause of the error much more quickly.

For example, the cause of an unstable test that is sometimes green and sometimes red could be the incorrect sorting of the data. Such errors can be automatically detected by the Simple-Report Feature of the snapshot library and displayed in a report in the Jenkins log. As a result, the priority of most errors can already be estimated in the daily and assigned to the responsible programmer.

With Simple-Report you will understand the test errors in a short time and save yourself hours of frustration trying to understand the changed expected values in the “data jungle”.

Because the values that deviate from the snapshot file are displayed in the log in a form that people can understand. As a result, many errors can be identified at first glance in the daily and delegated to the responsible developer.

void storeUsersInSnapshot() {

  List<User> users = Arrays.asList(
      new User("Admin", 40, MALE),
      new User("Secretary", 35, FEMALE),
      new User("Customer", 55, FEMALE)

  //create snapshot a list of java objects
  CsvSnapshot csvSnapshot = Snapshot.csvSnapshotFromObjects(users);

      //assert ids are unique and not null

      //implement custom assertion
      .peekRow(csvRow -> {
        //verify id is a positive number
        long id = Long.parseLong(csvRow.getValue("id"));
        assertTrue(id >= 0, "id should be a positive number");

        //assert age
        long age = Long.parseLong(csvRow.getValue("age"));
        assertTrue(age > 18, "age should be a positive number");


      //remove id, age - this columns dinamiclly change
      .removeColumns("id", "age")

      //reorder columns for better reading according to sorting criteria
      .moveColumnsToLeft("sex", "name")

      //sort first on name column and then on age column
      .sortRows("sex", "name")

      //assert to values stored in file

When the actual and expected snapshots do not match, you may see a message like this in your log file:

Different values:
|         | AGE|
| ACTUAL  | 41 |
| EXPECTED| 35 |

All values
|         | NAME   | AGE| SEX |
| ACTUAL  | Andreas| 41 | MALE|
| EXPECTED| Andreas| 35 | MALE|

Your passionate tester, Andreas Hollmann

Leave a Comment

GDPR Cookie Consent with Real Cookie Banner