TDD - Seriously, try it - Codemotion (May '24)

icougil 145 views 33 slides May 13, 2024
Slide 1
Slide 1 of 33
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33

About This Presentation

Ever wondered about the wonders of Test-Driven Development (TDD)? Curious devs, this session is for you!
Get ready to dive into TDD and explore its benefits. We'll see the "secrets" behind TDD, its roots, and the rules surrounding it. But that's not all! We'll also uncover the...


Slide Content

May 22nd, 2024 TDD: seriously, try it  ! 😃  

How many of you know what TDD is ? And how many of you practice TDD in your day-to-day job?

Review the basics around TDD Understand the benefits of building code designed by tests ( VS building the tests after we write our code ) (I hope) learn some tips See a { live coding } example … so do not run away, please! 😉 Why this session?

Nacho Cougil Principal Software Engineer at Dynatrace TDD & clean code fan Started to write in Java before Y2K Founder of the Barcelona Java Users Group ( BarcelonaJUG ) & co-founder of the Barcelona Developers Conference ( DevBcn ) former Java and JVM Barcelona Conference Java Champion Father , former mountain marathon runner 😅 @ icougil https:// jvm.social /@ icougil Who am I?

Raise your hand at any moment! Ask questions at any time, please!

Based on my *personal* experience Warning

How can we, developers, sleep peacefully? how can we sleep without fear that something fails badly, and we must wake up in the middle of the night because something has failed ?

Thanks to tests! ❤️🤩

But testing is boring! … isn’t it?

How can we design (and deliver) software in such a way that we can deliver portions incrementally ? With parts that provide value and be confident that they are OK / bug-free ? ( or at least we handle some error cases ) ?

Kent Beck “ rediscovered ” TDD when writing first testing framework SUnit in 1994. TDD became part of eXtreme Programming (XP) book in 2000. Developed XP during his work at Chrysler The book Test-Driven Development by Example was published in 2002 The history

… you follow a simple process ⚙️ that helps you to design and develop your software in a straightforward way … you can find defects earlier ⏱ (when running or designing your tests) … you can easily detect regression errors 🔴 … your software is going to be easier to refactor because of a safety net ✅ With TDD…

... your software guides you how a consumer will use your component ⏛ … tests are living documentation (really 🖖 !!) … the software you write it is likely to have less bugs 🐛 … your development costs 💰 will be lower References: - Test-driven development as a defect-reduction practice (IEEE) - Guest Editors' Introduction: TDD--The Art of Fearless Programming (computer.org) With TDD…

It is not so easy to start with 😅 (not many good places to help, majority of people are not using it daily) Has a high learning curve 😟 (depends directly on the complexity of your code + your design and engineering capabilities) Can be a large investment 💸 in a team (depending on the experience of the members + requires time ⏳ & effort 🙇‍♀️🙇‍♂️) Yes, TDD has some disadvantages

It is really easy to forget 😓 about it It provokes resistance ☹️ (in some people ⚔️) Can be corrupted and lead to the syndrome of reaching the highest possible level of coverage 🙈 I t is extremely difficult 😤 to master at it Yes, TDD has some disadvantages

You learned how to write code time ago,… …and now you’d may learn a different way of writing software. Like “ learning to ride a very different bike” ,... but being older 😅 So, … how it is?

To be used in your design & coding process , for building better modularized and tested software And, what does it seek?

The process

Start little by little: katas, POCs, personal projects, ... Practice... ... and keep practicing ... and keep on ... until you finally internalise it 🧘‍♀️😉 Practice with somebody else ( pair-programming ) Keep learning: online content, books, ... Learn and practice

An  Extreme Programming (XP) Practice in which 2 developers participate in one development effort at one workstation. One, the  driver , writes code  while the other, the  navigator or observer , reviews the code as it is typed in. The 2 engineers switch roles frequently.   While reviewing, the observer also considers the " strategic " direction of the work, coming up with ideas for improvements and likely future problems to address. Pair programming

Martin Fowler (concepts) https://www.martinfowler.com/bliki/TestDouble.html https://martinfowler.com/articles/mocksArentStubs.html https://martinfowler.com/articles/practical-test-pyramid.html Dave Farley (CI, TDD, etc) https://www.youtube.com/ContinuousDelivery/ James Shore (TDD series) https://www.youtube.com/jdlshore/ Jason Gorman (Java Code Craft) https://www.youtube.com/parlezuml/ Recommended content

TAKE MY MONEY AND SHOW ME THE CODE !

Before you write production code, check 👀 that your test is failing 🔴 Each test has only 1 reason to fail Write the assertion first Some good habits

Tests naming convention Describe the expected behaviour, not the internals. Our tests should describe behaviour in plain english E nding the class name with Should will “force “ you to start describing an action for every test you may create

Our tests methods will only describe behavior . Therefore, we will have a better understanding on what this class does Our tests will be more clear If some tests fails, we can have a look and see easily which case is failing We don’t need to get into details on any test to understand what are they pursuing The result will be focused on the business

Test creation order 1) Name the class 2) Name the method 3) Define what you want to check 4) Trigger the code 5) Do the setup

Warning – Live coding!

Requirements : Build a service that returns a list of films that are associated with a particular genre By default, the result must be ordered according to the average rating given to the films by the users A film should contain at least an id, title a year when it was published, one or more tags and one or more genres Example: recommendationService.filmsByGenre (”science-fiction”) https://github.com/icougil/FilmRecommendationService We have just started working in a startup that sells and rents out films over the Internet Our product managers have told us that they want to add new functionalities by building a service that allows them to recommend films to the users of the current platform. Kata: Film Recommendation Service

TDD helps you to design & develop your code in a simple and effective way (better modular design) 👌 It is difficult to adapt to its mechanics and sometimes is difficult 😅 to maintain its practice... It worth a try 😉 ( y our software will have less bugs 🐛 & it will be easier to maintain 💰) There are some tips 💡 ( could make your life easier ) Try to pair , it will help 😃 you ( a lot ) Practice , practice & practice 🥋 again Final Recap

Questions? [email protected] https://nacho.cougil.com @ icougil https:// jvm.social /@ icougil