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
55
Prepping Commits
alextercete
1
120
The end of your line-endings nightmare
alextercete
0
180
I love sushi, therefore I love rebase
alextercete
0
100
ReadyRoll for DotNet developers
alextercete
0
85
Coding Dojo: The Randori Kata
alextercete
1
480
Other Decks in Programming
See All in Programming
Итераторы в Go 1.23: зачем они нужны, как использовать, и насколько они быстрые?
lamodatech
0
1.4k
生成AIでGitHubソースコード取得して仕様書を作成
shukob
0
630
自分ひとりから始められる生産性向上の取り組み #でぃーぷらすオオサカ
irof
3
300
20年もののレガシープロダクトに 0からPHPStanを入れるまで / phpcon2024
hirobe1999
0
1k
サーバーゆる勉強会 DBMS の仕組み編
kj455
1
300
PHPUnitしか使ってこなかった 一般PHPerがPestに乗り換えた実録
mashirou1234
0
420
混沌とした例外処理とエラー監視に秩序をもたらす
morihirok
13
2.3k
Flatt Security XSS Challenge 解答・解説
flatt_security
0
740
毎日13時間もかかるバッチ処理をたった3日で60%短縮するためにやったこと
sho_ssk_
1
550
責務を分離するための例外設計 - PHPカンファレンス 2024
kajitack
9
2.4k
Внедряем бюджетирование, или Как сделать хорошо?
lamodatech
0
950
[JAWS-UG横浜 #80] うわっ…今年のServerless アップデート、少なすぎ…?
maroon1st
0
110
Featured
See All Featured
Mobile First: as difficult as doing things right
swwweet
222
9k
The Straight Up "How To Draw Better" Workshop
denniskardys
232
140k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
6
500
It's Worth the Effort
3n
183
28k
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
656
59k
How STYLIGHT went responsive
nonsquared
96
5.3k
GraphQLとの向き合い方2022年版
quramy
44
13k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
19
2.3k
The Language of Interfaces
destraynor
155
24k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
28
4.5k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
365
25k
The Cost Of JavaScript in 2023
addyosmani
46
7.2k
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