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
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
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
85
Speaking in Types
griotspeak
0
35
Other Decks in Programming
See All in Programming
属人化しないコード品質の作り方_2026.04.07.pdf
muraaano
0
300
ソフトウェア設計の結合バランス #phperkaigi
kajitack
0
480
ふりがな Deep Dive try! Swift Tokyo 2026
watura
0
270
Back to the roots of date
jinroq
0
650
WebAssembly を読み込むベストプラクティス 2026年春版 / Best Practices for Loading WebAssembly (Spring 2026)
petamoriken
5
1k
【26新卒研修】OpenAPI/Swagger REST API研修
dip_tech
PRO
0
130
クラウドネイティブなエンジニアに向ける Raycastの魅力と実際の活用事例
nealle
2
230
Vibe NLP for Applied NLP
inesmontani
PRO
0
580
How Swift's Type System Guides AI Agents
koher
0
330
From Formal Specification to Property Based Test
ohbarye
0
680
Road to RubyKaigi: Play Hard(ware)
makicamel
1
530
ローカルLLMでどこまでコードが書けるか / How much code can be written on a local LLM
kishida
2
210
Featured
See All Featured
Mobile First: as difficult as doing things right
swwweet
225
10k
Pawsitive SEO: Lessons from My Dog (and Many Mistakes) on Thriving as a Consultant in the Age of AI
davidcarrasco
0
130
Fireside Chat
paigeccino
42
3.9k
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
21
1.5k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
31
2.8k
The Pragmatic Product Professional
lauravandoore
37
7.2k
jQuery: Nuts, Bolts and Bling
dougneiner
66
8.4k
The Director’s Chair: Orchestrating AI for Truly Effective Learning
tmiket
1
160
The B2B funnel & how to create a winning content strategy
katarinadahlin
PRO
1
350
The AI Revolution Will Not Be Monopolized: How open-source beats economies of scale, even for LLMs
inesmontani
PRO
3
3.4k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
35
3.4k
Why Your Marketing Sucks and What You Can Do About It - Sophie Logan
marketingsoph
0
140
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