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
Search
Mathias Verraes
April 06, 2016
Technology
1
2.5k
Property Based Testing
5min lightning talk for the SoCraTes Belgium meetup and CukeUp London.
Mathias Verraes
April 06, 2016
Tweet
Share
More Decks by Mathias Verraes
See All by Mathias Verraes
On Being Explicit
mathiasverraes
0
2.8k
How to Find the Bar
mathiasverraes
1
1.9k
Designed Stickiness
mathiasverraes
1
2k
The Monty Hall Problem with Haskell
mathiasverraes
0
2.6k
The World's Shortest and Most Chaotic Introduction to Event Storming
mathiasverraes
1
2.6k
Towards Modelling Processes
mathiasverraes
3
5.5k
Modelling Heuristics
mathiasverraes
1
2.7k
Object Reorientation
mathiasverraes
6
2.6k
Small Controlled Experiments
mathiasverraes
1
3.8k
Other Decks in Technology
See All in Technology
AIアシスタントの活用で品質の向上と開発ワークフローのスピードアップ
nagix
1
210
コンテナ・K8s研修 - 後半 Kubernetes 基礎&ハンズオン【MIXI 24新卒技術研修】
mixi_engineers
PRO
1
120
Amazon FSx for NetApp ONTAPのパフォーマンスチューニング要素をまとめてみた #cm_odyssey #devio2024
non97
0
220
開発生産性をむしろ向上させる セキュリティパートナーの作り方 / Dev Productivity Con 2024
flatt_security
0
390
Azure OpenAI Service Dev Day / LLMでできる!使える!生成AIエージェント
masahiro_nishimi
3
810
Github Actions 로 Android 팀의 효율성 극대화
hadonghyun
0
160
エンジニアの生存戦略 〜クラウド潮流の経験から紐解く技術トレンドのメカニズムと乗りこなし方〜
shimy
9
1.9k
AWSで”最小権限の原則”を実現するための考え方 /20240722-ssmjp-aws-least-privilege
opelab
10
4.4k
AI研修【MIXI 24新卒技術研修】
mixi_engineers
PRO
0
130
テストケースの自動生成に生成AIの導入を試みた話と生成AIによる今後の期待
shift_evolve
0
190
ACRiルーム最新情報とAMD GPUサーバーのご紹介
anjn
0
160
シフトレフトで挑む セキュリティの生産性向上
sekido
PRO
0
270
Featured
See All Featured
Git: the NoSQL Database
bkeepers
PRO
423
64k
What the flash - Photography Introduction
edds
65
11k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
36
9.1k
4 Signs Your Business is Dying
shpigford
178
21k
Pencils Down: Stop Designing & Start Developing
hursman
118
11k
The Art of Programming - Codeland 2020
erikaheidi
48
13k
Typedesign – Prime Four
hannesfritz
37
2.2k
Web development in the modern age
philhawksworth
203
10k
Mobile First: as difficult as doing things right
swwweet
219
8.8k
Building Your Own Lightsaber
phodgson
101
5.9k
A Tale of Four Properties
chriscoyier
155
22k
Making Projects Easy
brettharned
111
5.7k
Transcript
Property Based Testing @mathiasverraes
None
function inc(x) { return x + 1; }
inc x = x + 1
-- Tests double 1 `should_be` 2 double 2 `should_be` 4
-- Implementation double x | x == 1 = 2 | x == 2 = 4
A property of double double_is_always_even :: Int -> Bool double_is_always_even
x = even (double x)
> quickCheck double_is_always_even Failed: 0 (after 1 test) Exception: Non-exhaustive
patterns in function double
double x = x * 2
quickCheck double_is_always_even Passed: 0 Passed: 1 Passed: -3 Passed: -1
(...) Passed: -58 Passed: 89 +++ OK, passed 100 tests.
More properties of double double_compare_to_input x | x > 0
= double x > x | x < 0 = double x < x | x == 0 = True double_minus_input x = double x - x == x
Distributivity Law reverse_is_distributive xs ys = reverse (xs++ys) == reverse
xs ++ reverse ys
> quickCheck reverse_is_distributive Passed: [] [] Passed: [] [1] Passed:
[1] [] Failed: [3,-3] [0,2,0] Passed: [] [0,2,0] Failed: [3] [0,2,0] Failed: [-3] [0,2,0] Failed: [0] [0,2,0] Failed: [0] [2,0] (...) Falsifiable (after 4 tests and 6 shrinks): [0] [1]
Oops... reverse_is_distributive xs ys = reverse (xs++ys) == reverse ys
++ reverse xs
Passed: [] [] Passed: [1,-2] [0] Passed: [-2] [] (...)
Passed: [-34,44,-58,-41,-17,-53,-14,27,54,46,-10,-46,-20,46] [-9,-32,-47,50,43,-47,-43,-61,37,4,-59,48,34] +++ OK, passed 100 tests.
Split 1 -- define split :: Char -> String ->
[String] -- so that split '@' "
[email protected]
" == ["foo","example.com"] split '/' "/usr/include" == ["", "usr", "include"] 1 https://www.schoolofhaskell.com/user/pbv/an-introduction-to-quickcheck-testing
-- splitting an empty list results in an empty list
split char [] = [] split char str | null after = before : [] | otherwise = before : split char (tail after) where before = takeWhile (/=char) str after = dropWhile (/=char) str
Property: Splitting and unsplitting -- test unsplit '@' ["foo","example.com"] ==
"
[email protected]
" unsplit '/' ["", "usr", "include"] == "/usr/include" --implementation unsplit :: Char -> [String] -> String unsplit char = concat . intersperse [char]
-- property unsplit_inverses_split str = forAll (elements str) (\char ->
unsplit char (split char str) == str)
> quickCheck unsplit_inverses_split Passed: "" Passed: "\252\210" '\252' Passed: "\163^\EOT"
'\163' Passed: "v\RSs" 'v' Failed: "y" 'y' Failed: "a" 'a' Falsifiable (after 6 tests and 1 shrink): "a" 'a'
split 'a' "a" == [""] unsplit 'a' [""] == ""
-- should be: split 'a' "a" == ["", ""] unsplit 'a' ["", ""] == "a"
Modify the definition of split split char [] = []
... -- becomes split char [] = [""] ... > quickCheck unsplit_inverses_split +++ OK, passed 100 tests.
Property Based Testing → test lots of random cases cheaply
→ encourage thinking about general properties Thanks :-) @mathiasverraes