Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Property-Based Testing with SwiftCheck
Search
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
TJ Usiyan
September 06, 2016
Programming
5.5k
1
Share
Property-Based Testing with SwiftCheck
These are the slides for my talk at try! swift NYC on 2016-09-02
TJ Usiyan
September 06, 2016
More Decks by TJ Usiyan
See All by TJ Usiyan
Musical Phantoms
griotspeak
0
87
Speaking in Types
griotspeak
0
37
Other Decks in Programming
See All in Programming
dRuby over BLE
makicamel
2
300
Make SRE Operations Easier with Azure SRE Agent
kkamegawa
0
3.7k
Lessons from Spec-Driven Development
simas
PRO
0
140
軽量Java基盤の設計 DIコンテナに頼らない、長期保守と1秒起動の実現 JJUG CCC 2026 Spring
macha64
0
440
CSC307 Lecture 17
javiergs
PRO
0
310
Modding RubyKaigi for Myself
yui_knk
0
880
TSKaigi Night Talks 2026_TypeScriptでサプライチェーンの整合性を型に閉じ込める
geekplus_tech
0
270
TypeScriptだけでAIエージェントを作る フロント・エージェント・インフラのフルスタック実践
har1101
6
1.3k
代数的データ型って何が嬉しいの? #frontend_phpcon_do
kajitack
8
3.2k
ADKを使って簡単にAIエージェントを作ってみよう
k1mu21
0
210
Moments When Things Go Wrong
aurimas
3
140
正しくソフトウェアを作る、前提を疑うための認知の視点 / doubt-premise
minodriven
17
5.8k
Featured
See All Featured
WENDY [Excerpt]
tessaabrams
11
38k
Tell your own story through comics
letsgokoyo
1
940
SEO Brein meetup: CTRL+C is not how to scale international SEO
lindahogenes
1
2.7k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
32
3.3k
How To Stay Up To Date on Web Technology
chriscoyier
790
250k
Winning Ecommerce Organic Search in an AI Era - #searchnstuff2025
aleyda
1
2k
Designing for Timeless Needs
cassininazir
1
250
The innovator’s Mindset - Leading Through an Era of Exponential Change - McGill University 2025
jdejongh
PRO
1
190
HTML-Aware ERB: The Path to Reactive Rendering @ RubyCon 2026, Rimini, Italy
marcoroth
1
150
Effective software design: The role of men in debugging patriarchy in IT @ Voxxed Days AMS
baasie
0
380
Context Engineering - Making Every Token Count
addyosmani
9
940
4 Signs Your Business is Dying
shpigford
187
22k
Transcript
Property-Based Testing with SwiftCheck 1
Tests are difficult to write 2
Tests are difficult to write 3
Test cases are difficult to write 4
How are test cases difficult to write? 5
How are test cases difficult to write? • We need
to find all of the meaninful test cases 5
How are test cases difficult to write? • We need
to find all of the meaninful test cases • We need to organize them in a meaningful way 5
How are test cases difficult to write? • We need
to find all of the meaninful test cases • We need to organize them in a meaningful way • How many cases are enough? 5
How can we find test cases? 6
How can we find test cases? • Randomness 6
How can we find test cases? • Randomness • Reproducible
6
How can we find test cases? • Randomness • Reproducible
• Simplify failing case (when possible) 6
7
Let's Get Testing public struct Rational { public let numerator
: Int public let denominator : Int } 8
Arbitrary public protocol Arbitrary { /// The generator for this
particular type. public static var arbitrary: SwiftCheck.Gen<Self> { get } } 9
Gen Gen represents a generator for random arbitrary values of
a type. public struct Gen<A> 10
Conforming to Arbitrary public static var arbitrary: SwiftCheck.Gen<Rational> { return
Gen.compose { comp in return Rational(comp.generate(), comp.generate()) } } 11
Conforming to Arbitrary return Rational(comp.generate(), comp.generate()) 12
Filtering Generated values Int.arbitrary.suchThat { $0 != 0 } //
Denominator 13
Filtering Generated values extension Rational : Arbitrary { public static
var arbitrary: SwiftCheck.Gen<Rational> { return Gen<Rational>.compose { comp in let denGen = Int.arbitrary.suchThat { $0 != 0 } return Rational( comp.generate(), comp.generate(using: denGen) } } 14
OK, What to test? 15
Properties! 16
Round Trip Properties property("round trip to string") <- forAll {
(i: Int) in return Int(i.description)! == i } 17
Round Trip Properties // for all foo: [Int] foo.reverse().reverse() ==
foo 18
Commuting Diagram Properties • ----- | | | | |
! -----" 19
Commuting Diagram Properties property("base64 encoding commutes with Apple's ") <-
forAll { (data: Data) in return data.base64Encoding == data.myBase64Encoding } 20
Properties func testAddition() { property("Addition is commutative") <- forAll {
(i: Rational, j: Rational) in return (i + j) == (j + i) } } 21
Properties property("Addition is commutative 2") <- forAll { (i: Rational,
j: Rational, k: Rational) in let caseOne = (i + j + k) let caseTwo = ((i + j) + k) let caseThree = (i + (j + k)) return (caseOne == caseTwo) && (caseTwo == caseThree) } 22
Replay 23
Replay let (seedl, seedr) = (1640744780, 1403884642) let replayArgs =
CheckerArguments(replay: .some(StdGen(seedl, seedr), 3)) property("x / y", arguments: replayArgs) <- forAll { … 24
Mixed Testing func testEquality() { let oneOverTwo = Rational(1, 2)!
let twoOverFour = Rational(2, 4)! let ichiUeNi = Rational(1, 2)! XCTAssertEqual(oneOverTwo == twoOverFour, true) XCTAssertEqual(twoOverFour == ichiUeNi, true) XCTAssertEqual(ichiUeNi == oneOverTwo, true) XCTAssertEqual(oneOverTwo ≡ twoOverFour, false) XCTAssertEqual(twoOverFour ≡ ichiUeNi, false) XCTAssertEqual(ichiUeNi ≡ oneOverTwo, true) let halfIntMax = (Int.max / 2) - 1 property("value holds") <- forAll { (value: Rational) in return ((value.numerator < halfIntMax) && (value.denominator < halfIntMax)) ==> { let doubledSame = Rational(value.numerator * 2, value.denominator * 2)! return (value == doubledSame) && ((value ≡ doubledSame) == false) } } } 25
Shrinking 26
So? 27
So? • Less time writing tests? 27
So? • Less time writing tests? • Better tests 27
So? • Less time writing tests? • Better tests •
Easier diagnosis 27
So? • Less time writing tests? • Better tests •
Easier diagnosis • You will and should still write test cases! 27
For Our Friends in Obj-C https://github.com/jeffh/Fox 28
TJ Usiyan @griotspeak 29