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
190
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
210
Porting to .NET Standard
alextercete
0
53
Prepping Commits
alextercete
1
120
The end of your line-endings nightmare
alextercete
0
180
I love sushi, therefore I love rebase
alextercete
0
99
ReadyRoll for DotNet developers
alextercete
0
85
Coding Dojo: The Randori Kata
alextercete
1
470
Other Decks in Programming
See All in Programming
命名をリントする
chiroruxx
1
400
ゆるやかにgolangci-lintのルールを強くする / Kyoto.go #56
utgwkk
1
370
Haze - Real time background blurring
chrisbanes
1
510
KMP와 kotlinx.rpc로 서버와 클라이언트 동기화
kwakeuijin
0
140
fs2-io を試してたらバグを見つけて直した話
chencmd
0
230
これでLambdaが不要に?!Step FunctionsのJSONata対応について
iwatatomoya
2
3.6k
20年もののレガシープロダクトに 0からPHPStanを入れるまで / phpcon2024
hirobe1999
0
460
CSC305 Lecture 26
javiergs
PRO
0
140
複雑な仕様に立ち向かうアーキテクチャ
myohei
0
170
テストケースの名前はどうつけるべきか?
orgachem
PRO
0
130
nekko cloudにおけるProxmox VE利用事例
irumaru
3
430
バグを見つけた?それAppleに直してもらおう!
uetyo
0
180
Featured
See All Featured
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
251
21k
The Art of Programming - Codeland 2020
erikaheidi
53
13k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
29
2k
Embracing the Ebb and Flow
colly
84
4.5k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
280
13k
Automating Front-end Workflow
addyosmani
1366
200k
How STYLIGHT went responsive
nonsquared
95
5.2k
Building a Scalable Design System with Sketch
lauravandoore
460
33k
Thoughts on Productivity
jonyablonski
67
4.4k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
232
17k
Music & Morning Musume
bryan
46
6.2k
Typedesign – Prime Four
hannesfritz
40
2.4k
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