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
Why Jakarta EE Matters to Spring - and Vice Versa
ivargrimstad
0
1.1k
AWS IaCの注目アップデート 2024年10月版
konokenj
3
3.3k
弊社の「意識チョット低いアーキテクチャ」10選
texmeijin
5
24k
Arm移行タイムアタック
qnighy
0
330
【Kaigi on Rails 2024】YOUTRUST スポンサーLT
krpk1900
1
330
『ドメイン駆動設計をはじめよう』のモデリングアプローチ
masuda220
PRO
8
540
Tauriでネイティブアプリを作りたい
tsucchinoko
0
370
初めてDefinitelyTypedにPRを出した話
syumai
0
410
Make Impossible States Impossibleを 意識してReactのPropsを設計しよう
ikumatadokoro
0
170
OSSで起業してもうすぐ10年 / Open Source Conference 2024 Shimane
furukawayasuto
0
110
Jakarta EE meets AI
ivargrimstad
0
560
as(型アサーション)を書く前にできること
marokanatani
10
2.7k
Featured
See All Featured
Imperfection Machines: The Place of Print at Facebook
scottboms
265
13k
Docker and Python
trallard
40
3.1k
Making the Leap to Tech Lead
cromwellryan
133
8.9k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
191
16k
The Illustrated Children's Guide to Kubernetes
chrisshort
48
48k
Large-scale JavaScript Application Architecture
addyosmani
510
110k
Visualization
eitanlees
145
15k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
232
17k
Fireside Chat
paigeccino
34
3k
Statistics for Hackers
jakevdp
796
220k
RailsConf 2023
tenderlove
29
900
Designing for Performance
lara
604
68k
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