Introducción práctica a Test-Driven Development (TDD)

27
Introducción práctica a Test-Driven Development Software Craftsmanship Alicante

Transcript of Introducción práctica a Test-Driven Development (TDD)

Page 1: Introducción práctica a Test-Driven Development (TDD)

Introducción práctica a Test-Driven Development

Software Craftsmanship Alicante

Page 2: Introducción práctica a Test-Driven Development (TDD)

Who am I?

• Rubén Antón A.K.A @rubocoptero

• Crafting code for Flywire

• h4ckademy alumni

Page 3: Introducción práctica a Test-Driven Development (TDD)

What’s TDD?

Page 4: Introducción práctica a Test-Driven Development (TDD)

TDD is not testing!

Page 5: Introducción práctica a Test-Driven Development (TDD)

Two simple rules

• Don’t write a line of new code unless you first have a failing automated test.

• Eliminate duplication.

Page 6: Introducción práctica a Test-Driven Development (TDD)

What is TDD?

• XP practice that combines Test-First and Refactoring

• Tests as a design tool

• KISS

• Baby steps

Page 7: Introducción práctica a Test-Driven Development (TDD)

Mantra

Page 8: Introducción práctica a Test-Driven Development (TDD)

Steps

1. ROJO: write a little test that doesn’t work, perhaps doesn’t even compile at first

2. GREEN: make the test work quickly, committing whatever sins necessary in the process

3. REFACTOR: eliminate all the duplication created in just getting the test to work.

Page 9: Introducción práctica a Test-Driven Development (TDD)

Why use TDD?

Page 10: Introducción práctica a Test-Driven Development (TDD)

Benefits• Manually verification is time consuming

• Break down our work into manageable chunks

• Source of documentation

• Avoid over-engineering

• Safety net

• Encourages Loosely Coupled Design

Page 11: Introducción práctica a Test-Driven Development (TDD)

–Chris Jordan

“Software is inherently complex, and being able to make things easier for ourselves as

developers is a crucial part of our role to deliver high quality software.”

Page 12: Introducción práctica a Test-Driven Development (TDD)

Techniques

Page 13: Introducción práctica a Test-Driven Development (TDD)

Fake it (’til you make it)1. Return a constant to make it pass

2. Gradually replace constants with variables until you have the real code

[TestMethod]public void TestSum(){ Assert.Equals(4, Sum(3, 1));}

private int Sum(int x, int y){ return 4}

[TestMethod]public void TestSum(){ Assert.Equals(4, Sum(3, 1));}

private int Sum(int x, int y){ return 3 + 1;

}

[TestMethod]public void TestSum(){ Assert.Equals(4, Sum(3, 1)); Assert.Equals(5, Sum(3, 2));}

private int Sum(int x, int y){ return x + y;}

Page 14: Introducción práctica a Test-Driven Development (TDD)

Triangulation• Use two or three test cases before to generalize.

• Only use it when completely unsure of how to refactor.

• What axes of variability are you trying to support in your design?

[TestMethod]public void TestSum(){ Assert.Equals(4, Sum(3, 1));}

private int Sum(int x, int y){ return 4}

[TestMethod]public void TestSum(){ Assert.Equals(4, Sum(3, 1)); Assert.Equals(5, Sum(3, 2));}

private int Sum(int x, int y){ return x + y;}

[TestMethod]public void TestSum(){ Assert.Equals(4, Sum(3, 1)); Assert.Equals(5, Sum(3, 2));}

private int Sum(int x, int y){ return x + y;}

Page 15: Introducción práctica a Test-Driven Development (TDD)

Obvious Implementation

• Fake it and Triangulation use baby steps.

• If you think it’s obvious, type in the real implementation.

• It’s a "second gear".

• If you get an unexpected red bar, back up.

Page 16: Introducción práctica a Test-Driven Development (TDD)

–Kent Beck

“Remember, TDD is not about taking teensy tiny steps, it’s about being able to take teensy tiny steps. Would I code day-to-day with steps this

small? No. But when things get the least bit weird, I’m glad I can.”

Page 17: Introducción práctica a Test-Driven Development (TDD)

Let’s practice…just a few last things before to start.

Page 18: Introducción práctica a Test-Driven Development (TDD)

Rules

• 30 seconds to go from RED to GREEN

• Don’t lose the GREEN in the refactoring phase.

• No debug

Page 19: Introducción práctica a Test-Driven Development (TDD)

Good tests• Imagine the perfect interface for our operation

• Fast

• Independents

• Arrange - Act - Assert

// ArrangeMyProductionClass myProductionClass = new MyProductionClass();

// Actbool result = myProductionClass.GetBooleanResult();

// AssertAssert.IsTrue(result);

Page 20: Introducción práctica a Test-Driven Development (TDD)

Before you start• The focus is to practice writing the best code we

can possibly write and challenge ourselves. It is important to mention that the goal is not to finish the exercise as soon as posible, but to learn during the process via the discussion with our partner.

• Do one task at a time. The trick is to learn to work incrementally.

• Make sure you only test for correct inputs. there is no need to test for invalid inputs for this kata

Page 21: Introducción práctica a Test-Driven Development (TDD)

FizzBuzzReturn “fizz”, “buzz” or “fizzbuzz”.

For a given natural number greater zero return:

• “fizz” if it is dividable by 3

• “buzz” if it is dividable by 5

• “fizzbuzz” if it is dividable by 3 and 5.

Input Result

1 1

2 2

3 fizz

6 fizz

5 buzz

10 buzz

15 fizzbuzz

30 fizzbuzz

Page 22: Introducción práctica a Test-Driven Development (TDD)

FizzBuzz continuation

• A number is "fizz" if it is divisible by 3 or if it has a 3 in it.

• A number is "buzz" if it is divisible by 5 or if it has a 5 in it.

Page 23: Introducción práctica a Test-Driven Development (TDD)

Retrospective time

• What difficulties do we found?

• What we have done?

• What we have learnt?

Page 24: Introducción práctica a Test-Driven Development (TDD)

TDD economy• Either you will have to be able to write twice as

many lines per day as before, or write half as many lines for the same functionality.

• You’ll have to measure and see what effect TDD has on your own practice.

• Be sure to factor debugging, integrating, and explaining time into your metrics, though.

Page 25: Introducción práctica a Test-Driven Development (TDD)

More than TDD

• Extreme Programming practices

• SOLID

• Clean Code

• 4 rules of simple design

Page 26: Introducción práctica a Test-Driven Development (TDD)

How can I learn it?• Practice, practice and practice!

• Read good books

• TDD by example - Kent Beck

• Clean Code - Uncle Bob

• Refactoring - Martin Fowler

• Test Driven - Lasse Koskela

• …

Page 27: Introducción práctica a Test-Driven Development (TDD)

Thank you all!