An introduction to property-based testing

When you write unit tests for your projects, there’s a fair chance that you do so by following the classical « Given-When-Then » paradigm, in which you set some input data, execute the code you’re testing, and finally assert that its outcome is indeed the one you expected.

While this approach is perfectly sound, it does suffer one downside: your program will only be tested on the static input data defined in your tests, and there is no real guarantee that this data does cover all edge cases. This can be especially problematic for SDK developers, who, by definition, have a very hard time anticipating all the different situations in which their code will be used.

To improve on this issue, another approach exists, and it is called property-based testing. The idea behind it is very simple: you write your tests by defining properties that must always be true for your program. For example, « an array reversed twice is always equal to itself ». The testing framework will then generate random input values and test wether the property holds or not. And, as you can imagine, this approach is extremely good at narrowing down on overlooked edge cases.

In Swift, we are lucky enough to already have a full-fledged implementation called SwiftCheck, that enables property-based testing (https://github.com/typelift/SwiftCheck). The goal of this talk is thus to explain how property-based testing can be a powerful addition to a testing suite, and give actual and actionable examples of how it can be added to a project using SwiftCheck.

I gave this talk at FrenchKit, here is a copy of the slides: https://github.com/vincent-pradeilles/slides/blob/master/frenchkit-2019-an-introduction-to-property-based-testing.pdf

Zoom webinar: https://zoom.us/j/91661501704

Share

Session info:

Speaker: Vincent Pradeilles

Lead iOS developer at Worldline

Date: 10 December 2020

Time: 10:00 - 10:50

Relevant tags:
Swift Testing

See all speakers

See all videos