1 / 28

Test-Driven Development

Test-Driven Development. An introduction for C# developers. Who I am. Mathias Brandewinder Background in business and quantitative analysis methods Develop in C#/.NET for the past 5 years Started my own business since over a year www.clear-lines.com/blog. My goal for today.

heba
Download Presentation

Test-Driven Development

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Test-Driven Development An introduction for C# developers

  2. Who I am • Mathias Brandewinder • Background in business and quantitative analysis methods • Develop in C#/.NET for the past 5 years • Started my own business since over a year • www.clear-lines.com/blog

  3. My goal for today I would like you to be able to start as you leave the room

  4. Approach • Interactive: write code live • Focus on C# • Focus primarily on NUnit • Covering also Visual Studio 2008 tools • Assuming little or no background in TDD/unit testing

  5. The plan • What is TDD? • TDD in action: the basics • Using Nunit • Using VS2008 • Less basic TDD • Mocks • Beyond TDD • Questions

  6. What is TDD?

  7. Test-Driven Development • Is a development methodology • Not a testing methodology • Aims for “clean code that works, now” • Write the test first, then the code which satisfies the test

  8. Source • Test-Driven Development By Example, Kent Beck (Addison Wesley)

  9. The rules Write new code only if an automated test has failed Eliminate duplication

  10. Red/Green/Refactor Red: write a little test that doesn’t work, and perhaps doesn’t even compile at first Green: make the test work quickly, committing whatever sins necessary in the process Refactor: eliminate all of the duplication created in merely getting the test to work

  11. Benefits: the obvious Simple and predictable Small steps Fully tested code from the get-go Very reassuring

  12. TDD in action

  13. It’s Monday morning, and…

  14. Your mission, if you accept it Add a functionality to an existing application, to compute the distance between cities

  15. A quick math reminder We will assume that “the world is flat” Distance between 2 points:

  16. Let’s get started!

  17. What have we seen so far? • [TestFixture], [Test], Assert • One test at a time • Write “as if” you had the feature done • Write tests as “sentences” • 3 patterns • Fake it ‘til you make it • Obvious implementation • Triangulate

  18. What have we seen so far? Separate projects Debugging a test

  19. Less basic TDD

  20. Some classic useful test techniques TestRow Testing for exceptions Setup & Teardown

  21. Mocks

  22. Mocks • “Subject under test” vs. Collaborators • Mocks to simplify your life • “Classicists”: by default, use the real thing • Mocks, the dark side of design patterns • “Mockists”: use Mocks in interactions

  23. What have we seen so far? When testing the interaction of a class with another class, Mocks are a convenient way to “fake” the collaborator Focus on behavior and not state

  24. Mocks and patterns MyClass Strategy: IStrategy IStrategy StrategyB StrategyA

  25. Beyond TDD

  26. A few questions What are the benefits of TDD? Does TDD slow you down? How much testing is enough? Does TDD mean no upfront design? What are good tests? Are TDD people terrorists?

  27. Questions?

  28. To contact me… mathias.brandewinder@clear-lines.com

More Related