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
TJ Usiyan
September 06, 2016
Programming
1
5.5k
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
Tweet
Share
More Decks by TJ Usiyan
See All by TJ Usiyan
Musical Phantoms
griotspeak
0
79
Speaking in Types
griotspeak
0
32
Other Decks in Programming
See All in Programming
複雑化したリポジトリをなんとかした話 pipenvからuvによるモノレポ構成への移行
satoshi256kbyte
1
1k
タスクの特性や不確実性に応じた最適な作業スタイルの選択(ペアプロ・モブプロ・ソロプロ)と実践 / Optimal Work Style Selection: Pair, Mob, or Solo Programming.
honyanya
3
160
Domain-centric? Why Hexagonal, Onion, and Clean Architecture Are Answers to the Wrong Question
olivergierke
2
790
コードとあなたと私の距離 / The Distance Between Code, You, and I
hiro_y
0
100
CSC509 Lecture 01
javiergs
PRO
1
440
非同期jobをtransaction内で 呼ぶなよ!絶対に呼ぶなよ!
alstrocrack
0
660
Pythonスレッドとは結局何なのか? CPython実装から見るNoGIL時代の変化
curekoshimizu
5
1.7k
Back to the Future: Let me tell you about the ACP protocol
terhechte
0
140
Cursorハンズオン実践!
eltociear
2
860
デミカツ切り抜きで面倒くさいことはPythonにやらせよう
aokswork3
0
220
Django Ninja による API 開発効率化とリプレースの実践
kashewnuts
0
1.2k
なぜあの開発者はDevRelに伴走し続けるのか / Why Does That Developer Keep Running Alongside DevRel?
nrslib
3
390
Featured
See All Featured
ReactJS: Keep Simple. Everything can be a component!
pedronauck
667
120k
How to Ace a Technical Interview
jacobian
280
24k
Building Better People: How to give real-time feedback that sticks.
wjessup
368
20k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
285
14k
Making Projects Easy
brettharned
119
6.4k
Code Reviewing Like a Champion
maltzj
525
40k
BBQ
matthewcrist
89
9.8k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
37
2.6k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
229
22k
KATA
mclloyd
32
15k
Statistics for Hackers
jakevdp
799
220k
The Power of CSS Pseudo Elements
geoffreycrofte
79
6k
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