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.4k
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
66
Speaking in Types
griotspeak
0
27
Other Decks in Programming
See All in Programming
上手な探索的テストとその上達方法について
matsu802
4
650
Migrating to Signals: A Practical Workshop
manfredsteyer
PRO
0
280
htmx is fun!
codehex
2
190
クソコード動画『カプセル化 Mk-II』 で考える 上手くカプセル化できない理由 / encapsulation2
minodriven
11
7.9k
PHP8の機能を使って堅牢にコードを書く
fendo181
6
2.6k
PHP 8.3で追加されたjson_validate()を徹底的に深掘りしてみよう
mashirou1234
1
720
RubyVM を PHP で実装する 〜Hello World を出力するまで〜
memory1994
PRO
1
490
Crafting a Own PHP - ウキウキ手作りミニマリストPHP
uzulla
4
1.1k
Enhancing Applications with Accessibility API
kishikawakatsumi
3
920
Open Source Swift Workshop - Foundation and first party libraries
ikesyo
0
180
phpunit/php-code-coverageって何をしてるんだ #phperkaigi
o0h
PRO
2
220
ここ1~2年くらいで 使えるようになった(主要ブラウザーの最新版 がすべて対応した ) ウェブの新機能について ランダムに喋る!
myzkyy
9
6.5k
Featured
See All Featured
jQuery: Nuts, Bolts and Bling
dougneiner
57
7.1k
Principles of Awesome APIs and How to Build Them.
keavy
119
16k
Gamification - CAS2011
davidbonilla
76
4.5k
Learning to Love Humans: Emotional Interface Design
aarron
266
39k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
111
35k
Creatively Recalculating Your Daily Design Routine
revolveconf
209
11k
Atom: Resistance is Futile
akmur
258
25k
Stop Working from a Prison Cell
hatefulcrawdad
265
19k
Docker and Python
trallard
33
2.6k
How To Stay Up To Date on Web Technology
chriscoyier
781
250k
Being A Developer After 40
akosma
56
580k
Scaling GitHub
holman
456
140k
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