test-driven development eduard miric ă. the problem

16
Test-Driven Development Eduard Mirică

Upload: juliana-stevenson

Post on 20-Jan-2016

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Test-Driven Development Eduard Miric ă. The problem

Test-Driven DevelopmentEduard Mirică

Page 2: Test-Driven Development Eduard Miric ă. The problem

The problem

Page 3: Test-Driven Development Eduard Miric ă. The problem

However, with testing...

• A bit faster• A bit cheaper• A bit better

The solution• Testing• Test Driven Development

Page 4: Test-Driven Development Eduard Miric ă. The problem

Test-driven development

• A software development technique where you write automated unit tests before you write your implementation code

• A technique for ensuring good quality and good design

• “Clean code that works is the goal of Test Driven Development.” -- Ron Jeffries

Page 5: Test-Driven Development Eduard Miric ă. The problem

How to do it

• Design: figure out what you want to do

• Test: write a test to express the design– It should FAIL

• Implement: write the code– Test again– It should PASS

• Refactor

Page 6: Test-Driven Development Eduard Miric ă. The problem

TDD cycle

New require-

mentWrite new test

Run tests

Write new code

Run tests

Refactor

Run tests

Page 7: Test-Driven Development Eduard Miric ă. The problem

TDD Cycle• Write Test Code

– Guarantees that every functional code is testable– Provides a specification for the functional code– Helps to think about design– Ensure the functional code is tangible

• Write Functional Code– Fulfill the requirement (test code)– Write the simplest solution that works– Leave Improvements for a later step– The code written is only designed to pass the test

• no further (and therefore untested code is not created).

• Refactor– Clean-up the code (test and functional)– Make sure the code expresses intent– Remove code smells– Re-think the design– Delete unnecessary code

Page 8: Test-Driven Development Eduard Miric ă. The problem

Clear up a commonmisconception...

• TDD != writing tests first• TDD =~ writing test first.. but• TDD isn’t about testing...• TDD is about DESIGN• RED -> GREEN -> REFACTOR

Page 9: Test-Driven Development Eduard Miric ă. The problem

TDD Concepts & Patterns • What to Test & in What Order?

– Details vs. big picture– Uncertain vs. familiar– High value vs. low-hanging fruit– Happy path vs. error situations

• Implementation Strategies– Faking it– Triangulation– Obvious Implementation

• Prime Guidelines for Test-Driving– Do Not Skip Re-factoring– Get to Green Fast– Slow Down After a Mistake

Page 10: Test-Driven Development Eduard Miric ă. The problem

Why TDD?

• Most of us aren’t disciplined enough to test last. Once we have manually tested the system taking the time to write an automated test seems like a pain.

• Code that isn’t written with tests in mind tends to be very difficult to test without refactoring it first.

Page 11: Test-Driven Development Eduard Miric ă. The problem

But I don’t have time to do TDD!

• I don’t have time to fix bugs• I don’t have time to constantly step through code to see

if it’s all working• I don’t have time to figure out what your code is

supposed to do• I don’t have time to figure out if my changes will break

something in your code• I don’t have time to rewrite code

Think long-term, not short-term!!

Page 12: Test-Driven Development Eduard Miric ă. The problem

The Cost of Unit Testing

Page 13: Test-Driven Development Eduard Miric ă. The problem

The Cost of Not Unit Testing

Page 14: Test-Driven Development Eduard Miric ă. The problem

Benefits

• Reduce development lead-time significantly• Programmers that write tests are more productive• Rarely use a debugger• Easy to revert on test failure• Programmers tests interface first, then code• Shorter implementation time• High Code coverage• Regressions• More modular code

Page 15: Test-Driven Development Eduard Miric ă. The problem

Possible reasons to avoid TDD

• Tests are programmer focused• May not be requirement focused• Running all tests are slow• Sprinting• Refactoring• Comparing

Page 16: Test-Driven Development Eduard Miric ă. The problem

TDD Summary

• Never write any code without a failing test• Start from the outside, with acceptance tests• Drive design inwards using mock objects• Tests should be descriptive specifications• Red – Green – Refactor• YAGNI - You ain't gonna need it