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
SymfonyCon Vienna 2025: Twig, still relevant in 2025?
fabpot
3
1.2k
Go の GC の不得意な部分を克服したい
taiyow
3
780
KMP와 kotlinx.rpc로 서버와 클라이언트 동기화
kwakeuijin
0
140
PHPで作るWebSocketサーバー ~リアクティブなアプリケーションを知るために~ / WebSocket Server in PHP - To know reactive applications
seike460
PRO
2
280
Jakarta EE meets AI
ivargrimstad
0
240
Effective Signals in Angular 19+: Rules and Helpers
manfredsteyer
PRO
0
100
Refactor your code - refactor yourself
xosofox
1
260
talk-with-local-llm-with-web-streams-api
kbaba1001
0
180
コンテナをたくさん詰め込んだシステムとランタイムの変化
makihiro
1
130
今年一番支援させていただいたのは認証系サービスでした
satoshi256kbyte
1
250
Cloudflare MCP ServerでClaude Desktop からWeb APIを構築
kutakutat
1
540
ブラウザ単体でmp4書き出すまで - muddy-web - 2024-12
yue4u
3
470
Featured
See All Featured
Art, The Web, and Tiny UX
lynnandtonic
298
20k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
132
33k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
330
21k
Principles of Awesome APIs and How to Build Them.
keavy
126
17k
Fantastic passwords and where to find them - at NoRuKo
philnash
50
2.9k
Intergalactic Javascript Robots from Outer Space
tanoku
270
27k
Visualization
eitanlees
146
15k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
28
9.1k
Measuring & Analyzing Core Web Vitals
bluesmoon
4
170
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
48
2.2k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
665
120k
For a Future-Friendly Web
brad_frost
175
9.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