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
Writing readable tests
Search
Alex Tercete
August 20, 2015
Programming
2
180
Writing readable tests
Lightning Talk presented at the LSCC Talks #14 event.
Alex Tercete
August 20, 2015
Tweet
Share
More Decks by Alex Tercete
See All by Alex Tercete
Making Makefiles
alextercete
0
64
Shipping containers
alextercete
0
52
Parallelism and Symmetry
alextercete
0
200
Porting to .NET Standard
alextercete
0
53
Prepping Commits
alextercete
1
120
The end of your line-endings nightmare
alextercete
0
170
I love sushi, therefore I love rebase
alextercete
0
99
ReadyRoll for DotNet developers
alextercete
0
84
Coding Dojo: The Randori Kata
alextercete
1
470
Other Decks in Programming
See All in Programming
アジャイルを支えるテストアーキテクチャ設計/Test Architecting for Agile
goyoki
9
3.1k
Importmapを使ったJavaScriptの 読み込みとブラウザアドオンの影響
swamp09
4
1.3k
タクシーアプリ『GO』のリアルタイムデータ分析基盤における機械学習サービスの活用
mot_techtalk
4
110
「今のプロジェクトいろいろ大変なんですよ、app/services とかもあって……」/After Kaigi on Rails 2024 LT Night
junk0612
2
510
Why Jakarta EE Matters to Spring - and Vice Versa
ivargrimstad
0
460
約9000個の自動テストの 時間を50分->10分に短縮 Flakyテストを1%以下に抑えた話
hatsu38
24
12k
デプロイを任されたので、教わった通りにデプロイしたら障害になった件 ~俺のやらかしを越えてゆけ~
techouse
53
33k
Go言語でターミナルフレンドリーなAIコマンド、afaを作った/fukuokago20_afa
monochromegane
2
140
LLM生成文章の精度評価自動化とプロンプトチューニングの効率化について
layerx
PRO
2
160
色々なIaCツールを実際に触って比較してみる
iriikeita
0
310
Jakarta Concurrencyによる並行処理プログラミングの始め方 (JJUG CCC 2024 Fall)
tnagao7
1
270
ECSのサービス間通信 4つの方法を比較する 〜Canary,Blue/Greenも添えて〜
tkikuc
11
2.4k
Featured
See All Featured
Music & Morning Musume
bryan
46
6.2k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
27
810
Statistics for Hackers
jakevdp
796
220k
Automating Front-end Workflow
addyosmani
1366
200k
BBQ
matthewcrist
85
9.3k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
15
2k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
159
15k
Adopting Sorbet at Scale
ufuk
73
9.1k
Practical Orchestrator
shlominoach
186
10k
Side Projects
sachag
452
42k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
191
16k
jQuery: Nuts, Bolts and Bling
dougneiner
61
7.5k
Transcript
Writing readable tests Separating mechanics from data Alex Tercete @alextercete
https://www.flickr.com/photos/vdepizzol/8081618546
read- ability?
read- ability
understand- ability
The four elements of readability
https://www.flickr.com/photos/tychosnose/15416119182 Context WHERE
Reader’s Experience https://www.flickr.com/photos/theilr/5048464567 WHO
https://www.flickr.com/photos/thomasclaveirole/463202335 Structure HOW
Content https://www.flickr.com/photos/carinefel/13916816466 WHAT
Structure Mechanics Content Data
https://www.flickr.com/photos/emily3333000/16210254212
[Test] public void Add_two_numbers() { var calculator = new RpnCalculator();
calculator.Push(5); calculator.Push(3); Assert.That(calculator.Add(), Is.EqualTo(8)); } [Test] public void Add_two_numbers() { var calculator = new RpnCalculator(); calculator.Push(5); calculator.Push(3); Assert.That(calculator.Add(), Is.EqualTo(8)); }
[Test] public void Subtract_two_numbers() { var calculator = new RpnCalculator();
calculator.Push(5); calculator.Push(3); Assert.That(calculator.Subtract(), Is.EqualTo(2)); } [Test] public void Add_two_numbers() { var calculator = new RpnCalculator(); calculator.Push(5); calculator.Push(3); Assert.That(calculator.Add(), Is.EqualTo(8)); } [Test] public void Subtract_two_numbers() { var calculator = new RpnCalculator(); calculator.Push(5); calculator.Push(3); Assert.That(calculator.Subtract(), Is.EqualTo(2)); }
Don’t Repeat Yourself
[SetUp] public void SetUp() { _calculator = new RpnCalculator(); _calculator.Push(5);
_calculator.Push(3); } [Test] public void Add_two_numbers() { Assert.That(_calculator.Add(), Is.EqualTo(8)); } private RpnCalculator _calculator; [SetUp] public void SetUp() { _calculator = new RpnCalculator(); _calculator.Push(5); _calculator.Push(3); } [Test] public void Add_two_numbers() { Assert.That(_calculator.Add(), Is.EqualTo(8)); } [Test] public void Subtract_two_numbers() { Assert.That(_calculator.Subtract(), Is.EqualTo(2)); }
Repeat everything
[Test] public void Add_two_numbers() { var calculator = new RpnCalculator();
calculator.Push(5); calculator.Push(3); Assert.That(calculator.Add(), Is.EqualTo(8)); } [Test] public void Subtract_two_numbers() { var calculator = new RpnCalculator(); calculator.Push(5); calculator.Push(3); Assert.That(calculator.Subtract(), Is.EqualTo(2)); } [Test] public void Add_two_numbers() { var calculator = new RpnCalculator(); calculator.Push(5); calculator.Push(3); Assert.That(calculator.Add(), Is.EqualTo(8)); }
Separate mechanics from data
public void SetUp() { _calculator = new RpnCalculator(); } [Test]
public void Add_two_numbers() { _calculator.Push(5); _calculator.Push(3); Assert.That(_calculator.Add(), Is.EqualTo(8)); } private RpnCalculator _calculator; [SetUp] public void SetUp() { _calculator = new RpnCalculator(); } [Test] public void Add_two_numbers() { _calculator.Push(5); _calculator.Push(3); Assert.That(_calculator.Add(), Is.EqualTo(8)); } [Test] public void Subtract_two_numbers() { _calculator.Push(5); _calculator.Push(3); Assert.That(_calculator.Subtract(), Is.EqualTo(2)); }
If it isn’t data, it must be mechanics https://www.flickr.com/photos/sarahreido/3120877348
[Test] public void Show_result_of_an_addition_on_the_display() { var stubCalculatorEngine = Substitute.For<ICalculatorEngine>(); stubCalculatorEngine.Add(1,
2).Returns(3); var mockCalculatorDisplay = Substitute.For<ICalculatorDisplay>(); var calculator = new Calculator(stubCalculatorEngine, mockCalculatorDisplay); calculator.Type('1', '+', '2', '='); mockCalculatorDisplay.Received().Show("3"); }
[Test] public void Show_result_of_an_addition_on_the_display() { var stubCalculatorEngine = Substitute.For<ICalculatorEngine>(); stubCalculatorEngine.Add(1,
2).Returns(3); var mockCalculatorDisplay = Substitute.For<ICalculatorDisplay>(); var calculator = new Calculator(stubCalculatorEngine, mockCalculatorDisplay); calculator.Type('1', '+', '2', '='); mockCalculatorDisplay.Received().Show("3"); }
[SetUp] public void SetUp() { _stubCalculatorEngine = Substitute.For<ICalculatorEngine>(); _mockCalculatorDisplay =
Substitute.For<ICalculatorDisplay>(); _calculator = new Calculator(_stubCalculatorEngine, _mockCalculatorDisplay); } [Test] public void Show_result_of_an_addition_on_the_display() { _stubCalculatorEngine.Add(1, 2).Returns(3); _calculator.Type('1', '+', '2', '='); _mockCalculatorDisplay.Received().Show("3"); }
Keep only what is essential to understand the test
Thanks! Alex Tercete @alextercete https://speakerdeck.com/alextercete/writing-readable-tests