20
Clear Lines Consulting · clear- lines.com 5/15/22 · 1 hat is TDD? nswer: Test-Driven Development) and why you should car

BizSpark SF Lightning Talk: "Refactoring and Test-Driven Development" by Mathias Brandewinder

  • Upload
    mark-a

  • View
    393

  • Download
    0

Embed Size (px)

DESCRIPTION

Presentation from November 2011 BizSparkSF Meetup entitled "Tools, Tools and More Tools!" http://www.bizsparksf.com/events/34653282/

Citation preview

Page 1: BizSpark SF Lightning Talk: "Refactoring and Test-Driven Development" by Mathias Brandewinder

Clear Lines Consulting · clear-lines.com 11/6/2011 · 1

What is TDD? (answer: Test-Driven Development)

… and why you should care

Page 2: BizSpark SF Lightning Talk: "Refactoring and Test-Driven Development" by Mathias Brandewinder

Clear Lines Consulting · clear-lines.com 11/6/2011 · 2

Once upon a Time…

Design

Implement

Test

Page 3: BizSpark SF Lightning Talk: "Refactoring and Test-Driven Development" by Mathias Brandewinder

Clear Lines Consulting · clear-lines.com 11/6/2011 · 3

Stay buggy, my friends!

Page 4: BizSpark SF Lightning Talk: "Refactoring and Test-Driven Development" by Mathias Brandewinder

Clear Lines Consulting · clear-lines.com 11/6/2011 · 4

Start with the tests!

If I needed to Add 1 + 1…

Page 5: BizSpark SF Lightning Talk: "Refactoring and Test-Driven Development" by Mathias Brandewinder

Clear Lines Consulting · clear-lines.com 11/6/2011 · 5

If I needed to Add 1 + 1…

[Test]public void When_Adding_One_Plus_One_Answer_Should_Be_Two(){ // Assert var expectedResult = 2; Assert.That(actualResult, Is.EqualTo(expectedResult));}

… I would expect the answer to be 2

Page 6: BizSpark SF Lightning Talk: "Refactoring and Test-Driven Development" by Mathias Brandewinder

Clear Lines Consulting · clear-lines.com 11/6/2011 · 6

If I needed to Add 1 + 1…

[Test]public void When_Adding_One_Plus_One_Answer_Should_Be_Two(){ // Act var actualResult = calculator.Add(1, 1);

// Assert var expectedResult = 2; Assert.That(actualResult, Is.EqualTo(expectedResult));}

… I would need a Calculator

Page 7: BizSpark SF Lightning Talk: "Refactoring and Test-Driven Development" by Mathias Brandewinder

Clear Lines Consulting · clear-lines.com 11/6/2011 · 7

If I needed to Add 1 + 1…

[Test]public void When_Adding_One_Plus_One_Answer_Should_Be_Two(){ // Arrange var calculator = new Calculator();

// Act var actualResult = calculator.Add(1, 1);

// Assert var expectedResult = 2; Assert.That(actualResult, Is.EqualTo(expectedResult));}

… I would need some code

Page 8: BizSpark SF Lightning Talk: "Refactoring and Test-Driven Development" by Mathias Brandewinder

Clear Lines Consulting · clear-lines.com 11/6/2011 · 8

If I needed to Add 1 + 1…

… Does the code work?

Page 9: BizSpark SF Lightning Talk: "Refactoring and Test-Driven Development" by Mathias Brandewinder

Clear Lines Consulting · clear-lines.com 11/6/2011 · 9

MsTest

Testing frameworks

Page 10: BizSpark SF Lightning Talk: "Refactoring and Test-Driven Development" by Mathias Brandewinder

Clear Lines Consulting · clear-lines.com 11/6/2011 · 10

It’s not about Testing

» It’s about writing Code that works, now» Imagine you had implemented the feature» … then implement it» Rinse & Repeat until you have no test to write

Page 11: BizSpark SF Lightning Talk: "Refactoring and Test-Driven Development" by Mathias Brandewinder

Clear Lines Consulting · clear-lines.com 11/6/2011 · 11

Why should you care?

Page 12: BizSpark SF Lightning Talk: "Refactoring and Test-Driven Development" by Mathias Brandewinder

Clear Lines Consulting · clear-lines.com 11/6/2011 · 12

© www.dilbert.com

The dreaded L word

Page 13: BizSpark SF Lightning Talk: "Refactoring and Test-Driven Development" by Mathias Brandewinder

Clear Lines Consulting · clear-lines.com 11/6/2011 · 13

“To me, legacy code is simply code without tests.”

Michael Feathers,Working Effectively with Legacy Code

Page 14: BizSpark SF Lightning Talk: "Refactoring and Test-Driven Development" by Mathias Brandewinder

Clear Lines Consulting · clear-lines.com 11/6/2011 · 14

Did I break everything?

Page 15: BizSpark SF Lightning Talk: "Refactoring and Test-Driven Development" by Mathias Brandewinder

Clear Lines Consulting · clear-lines.com 11/6/2011 · 15

Tests: a Vise holding your code steady

Page 16: BizSpark SF Lightning Talk: "Refactoring and Test-Driven Development" by Mathias Brandewinder

Clear Lines Consulting · clear-lines.com 11/6/2011 · 16

A good test is

» Fast» Focused» Replicable» Automated

Page 17: BizSpark SF Lightning Talk: "Refactoring and Test-Driven Development" by Mathias Brandewinder

Clear Lines Consulting · clear-lines.com 11/6/2011 · 17

Refactoring: improve the code structure, without changing its external behavior.

Page 18: BizSpark SF Lightning Talk: "Refactoring and Test-Driven Development" by Mathias Brandewinder

Clear Lines Consulting · clear-lines.com 11/6/2011 · 18

Red, Green, Refactor

»Red: write a little test that doesn’t work

»Green: make the test work quickly

»Refactor: cleanup time!

Page 19: BizSpark SF Lightning Talk: "Refactoring and Test-Driven Development" by Mathias Brandewinder

Clear Lines Consulting · clear-lines.com 11/6/2011 · 19

Test-Driven Development is a way of managing fear during programming.

Kent Beck,Test-Driven Development by Example

Page 20: BizSpark SF Lightning Talk: "Refactoring and Test-Driven Development" by Mathias Brandewinder

Clear Lines Consulting · clear-lines.com 11/6/2011 · 20

The Cast

» Me: Mathias Brandewinder– [email protected] (clear-lines.com/blog)

» Frameworks/Tools– NUnit: www.nunit.org– xUnit: xunit.codeplex.com/– Resharper: www.jetbrains.com/resharper/

» Books– Kent Beck, “Test Driven Development by Example”– Michael Feathers, “Working Effectively with Legacy Code”– Roy Osherove, “The Art of Unit Testing”