Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Snapshot testing. Testing the UI and beyond

Snapshot testing. Testing the UI and beyond

What is snapshot testing? How can we use it to test the UI? How can it help us test quickly complex data structures or define a contract to an external system that we do not control?

Georgios

June 07, 2019
Tweet

Other Decks in Programming

Transcript

  1. SOFTWARE VERIFICATION • Assure that software fully satisfies all the

    expected requirements, equivalent to software testing • Static and dynamic verification
  2. DYNAMIC VERIFICATION SOFTWARE VERIFICATION • Unit tests • Snapshot tests

    • Characterisation tests • Integration tests • UI functional tests • Manual testing
  3. HOW DOES IT WORK? WHAT IS SNAPSHOT TESTING? func testAddition()

    { assertSnapshot(add(3,5)) } • Record mode Saves a snapshot of the output as an artifact on disk e.g. testAddition1.txt • Verify mode Compares produced snapshot with saved reference snapshot
  4. WHY DO THIS? SCREENSHOT TESTING • They provide far more

    coverage than a unit test normally allows • A fast way to see how our view looks while developing • Immediately detect UI breaking changes and avoid regression bugs
  5. TEXT INSTEAD OF IMAGE SNAPSHOT TESTING THE UI • Could

    also use a textual representation of the UI as a snapshot
  6. COMPLEX DATA STRUCTURES SNAPSHOT TESTING let request = URLRequest(endpoint: .user)

    assertSnapshot(request) -POST https://xm.com/user +GET https://xm.com/user Accept: application/json { "name" : "George", "email" : “[email protected]", +"type": “demo", "password" : "abc123" }
  7. COMPLEX DATA STRUCTURES SNAPSHOT TESTING curl -X POST -H "Content-Type:

    application/json" -d '{"name":"George", "email":"[email protected]", "password": #some_encrypted_password#}' http://xm.com/user
  8. COMPLEX DATA STRUCTURES SNAPSHOT TESTING struct URLProvider: Codable { let

    webTraderHomeURL: String let loginURL: String let newsURL: String let serverTimeURL: String let userURL: String ... ... ... let watchlistURL: String }
  9. COMPLEX DATA STRUCTURES SNAPSHOT TESTING _assertInlineSnapshot(matching: provider, as: .json, with:

    """ { "loginURL" : "https://xm.com/login", "newsURL" : "https://xm.com/news", "serverTimeURL" : "https://xm.com/serverTime", "userURL" : "https://xm.com/user/{userid}", "watchlistURL" : "https://xm.com/watchlist/ {watchlistid}", ... "webTraderHomeURL" : “https://xm.com/home" } """)
  10. AS A CHANGE DETECTOR SNAPSHOT TESTING • Snapshot tests are

    essentially change detectors • Ideal when using external systems that we do not control • ..or when using external libraries that we want to update from time-to-time V2 V1 ✅ ✅ ✅ ✅
  11. SNAPSHOT TESTING • Interested whether the output has changed and

    less about whether it is correct • Lock the behaviour to a golden master version • Low effort method that tests multiple properties at once using a single assertion WHEN WRITING CHARACTERISATION TESTS
  12. CONS SNAPSHOT TESTING • Effortless to update but easier to

    accept new snapshots that have problems • The context of what you test is lost • Might lead to fragile tests
  13. WHO IS XM • Leading Forex broker • Offices in

    X countries • Athens X developers (Kotlin, Swift, Java, PHP, Go, C#, C++) • Multi-culture