Tests that (Almost) Write Themselves Hints for Golden Master - - PowerPoint PPT Presentation

tests that almost write themselves
SMART_READER_LITE
LIVE PREVIEW

Tests that (Almost) Write Themselves Hints for Golden Master - - PowerPoint PPT Presentation

Tests that (Almost) Write Themselves Hints for Golden Master Testing in Python Stefan Baerisch, stefan@stbaer.com, 2020-07-20 1 image: freepik.com premium, by rawpixel About Stefan Baerisch stbaer.com (German only, sorry) Software since


slide-1
SLIDE 1

Stefan Baerisch, stefan@stbaer.com, 2020-07-20

Tests that (Almost) Write Themselves

1 image: freepik.com premium, by rawpixel

Hints for Golden Master Testing in Python

slide-2
SLIDE 2

About

2

Software since 2005 Python since 2006 Project mangement / Test Management since 2010

Stefan Baerisch

stbaer.com (German only, sorry)

slide-3
SLIDE 3

Some Background on Golden Master Tests

3

slide-4
SLIDE 4

Testing Ducks

4 image: freepik.com premium, by rawpixel

How would you describe an ‘acceptable’ rubber duck ? Do you know it when you see it?

Good Duck?

slide-5
SLIDE 5

Testing Ducks

5

How would you describe an ‘acceptable’ rubber duck ? Do you know it when you see it?

image: freepik.com premium, by rawpixel

slide-6
SLIDE 6

The Idea behind Golden Master Testing

6

How to implement test if data is unknown

  • r too complex for an assert() ?
slide-7
SLIDE 7

What is Golden Master Testing

7

? ?

?

?

? ?

?

?

? ?

?

?

1 Run 2 Change 3 Run Again 4 Compare 1a Capture Output 3a Capture Output

slide-8
SLIDE 8

How does this help?

8

Testing a legacy system

Scenario Action

Ensure that changes are visable Testing complex data Identify and review changes Testing complex data with some changes Select or filter data used for tests

slide-9
SLIDE 9

Golden Master Testing - An Overview

9

  • Characterization Tests
  • Approval Tests

Golden Master Tests are also known as:

  • Snapshot Tests

Because changes to the data are ‘approved’ In the Javascript World, especially Jest

slide-10
SLIDE 10

Implementing Golden Master Tests

10

slide-11
SLIDE 11

The Process

11

Store current results

assert(check(data))

Return True Compare current data to stored data Review Differences Change Code Replace Stored Data OK? Not OK? Create Diffable Format Differences? No Differences? Store data Data exists? No Yes Return True Return False

Automatic Test Process Manual Approval Process

slide-12
SLIDE 12

Design Decisions

12

Test Target

State Log Output

Test Size

Many Small Few Large

Storage Formats

Text Json/XML Custom

Diff Approach

CLI $… Custom Lib

What to Ignore

Key Order Dates Random Values

slide-13
SLIDE 13

Test Target

13

Text / Logfiles Databases JSON Objects Images Use as is, with Filtering Select and Store as DDL import json custom implementation or… import jsonpickle or… import deepdiff pillow Dataframes json Keep it simple. Python makes it quite easy to get a text presentation of data

slide-14
SLIDE 14

What to Ignore

14

Dates / timestamps Key order for non-ordered dicts Random data (object ids / sequence ids) Non-relevant data (non significant floating point data)

What to ignore?

Needs some pre-processing, depending on use case

slide-15
SLIDE 15

Test Size

15

Large Tests Small Tests

Less work cleaning up data will notice most changes less frequent reviews / shorter reviews will need to break up / select data frequent reviews may miss changes easy, fast setup likely more work cleaning up data

slide-16
SLIDE 16

Storage Formats

16

How do you store results from past runs? The format should be: Should be structured to support working with Python Supported by Editors / Views Diffable in Python and External Tools Easy to store in VCSs If possible, use normalized JSON with linebreaks

slide-17
SLIDE 17

An Example Implementation

17

slide-18
SLIDE 18

Design Goals

18

Compare Python Objects, no extentions

jsonpickle

Keep things simple

Simple comparision

difflib.unified_diff

Simple storage store files with tests, explicit naming of tests Simple operations check() - store or compare data list() - show stored data and conflicts review(name) - show differences approve(name) - mark the current version as ok

slide-19
SLIDE 19

Our Sample Class

19

slide-20
SLIDE 20

Overview

20

Success Failure Set storage location Show state of tests / stored data Show diff of “name2” test Accept the lastest version

Save Compare Save Compare

slide-21
SLIDE 21

Overview Results

21

checker.list() checker.approve("name2") checker.review("name2")

slide-22
SLIDE 22

Implementation - Check

22

slide-23
SLIDE 23

Implementation - List Status

23

slide-24
SLIDE 24

Implementation - Store

24

slide-25
SLIDE 25

Implementation - Diff

25

slide-26
SLIDE 26

Other Existing Libraries

26

github.com/approvals/ApprovalTests.Python github.com/syrusakbary/snapshottest

Inspired by Javascript’s Jess. Nice Integration with unittest/nose/pytest Python implementation of approval testing,

slide-27
SLIDE 27

Summary

27

Golden Master Tests work by capturing & comparing results of program

  • executions. It helps with complex data,

especially wen we want to monitor for changes. We store results and compare between run. On differences, we review & approve the results Python has many modules to help us. Some existing implementations exist if we don’t want to implement it ourselves

Why How Python

slide-28
SLIDE 28

Stefan Baerisch, stefan@stbaer.com, 2020-04-07

Thank you!

28