1 / 29

Unit Testing and Test-Driven Development

Unit Testing and Test-Driven Development. What is Unit Testing? How TDD Works?. Tsvyatko Konov. Telerik Corporation. www.telerik.com. So what’s a unit test?. A Unit Test is a test of a small functional piece of code. Public bool IsLoginOK(string user, string password) { //………………………… }.

dalton
Download Presentation

Unit Testing and 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. Unit Testing and Test-Driven Development What is Unit Testing? How TDD Works? Tsvyatko Konov Telerik Corporation www.telerik.com

  2. So what’s a unit test?

  3. A Unit Test is a testof a small functional piece of code Public bool IsLoginOK(string user, string password) { //………………………… }

  4. Possible tests Should return true when… Should return false when… Public bool IsLoginOK(string user, string password) { //………………………… }

  5. Unit Testing makes your developer lives easier • Easier to find bugs • Easier to maintain • Easier to understand • Easier to Develop source flickr.com

  6. You have already done Unit testing • Not structured • Not Repeatable • Not on all your code • Not easy to do as it should be • A framework is missing

  7. The xUnit Frameworks • Original was for SmallTalk • Kent Beck and Erich Gamma • Ported to Various languages and platforms • JUnit, CppUnit, DUnit, VBUnit, RUnit, PyUnit, Sunit, HtmlUnit, … • Good list at www.xprogramming.com • Standard test architecture

  8. How we use the framework • Write Tests • Make it easy to create and organize tests • Reference an assembly, spread some attributes, you’re done • Run Tests • Allow running all of our tests, a group or just one. • From command line or GUI • Review Results • Immediate Pass/Fail feedback • Details on each failure

  9. .NET unit Test Frameworks • NUnit • XUnit • MbUnit • Team System Unit Testing

  10. NUnit

  11. Team System Unit Testing

  12. The 3A Pattern • Arrange all necessary preconditions and inputs. • Act on the object or method under test. • Assert that the expected results have occurred.

  13. Attributes • TestClass • TestMethod • TestInitialize • TestCleanup • ExpectedException • Ignore • Description

  14. TDD = Test First Development + Refactoring Make itFail No code without a failing test Make itWork As simply as possible Make itBetter Refactor What is Test Driven Development?

  15. Development Cycle add a test [Pass] run the tests [Development continues] [Fail] make a little change (refactor) [Fail] run the tests [Development stops]

  16. Demo TDD + Code Coverage

  17. TDD = Test First Development + Refactoring But what are the advantages? Test Driven Development

  18. TDD shortens the programming feedback loop TDD provides detailed specification (tests) TDD promotes the development of high-quality code TDD provides concrete evidence that your software works What are the advantages of TDD?

  19. (a) Traditional waterfall development process (b) Evolutionary development process (EVO). What are the advantages of TDD?

  20. TDD “speaks” to programmers TDD provides very finely grained concrete feedback on the order of minutes TDD helps to ensure that your design is clean by focusing on creation of operations that are callable and testable TDD supports evolutionary development. What are the advantages of TDD?

  21. Defect Rate Long term and short term affect Productivity Lines of code per month impact Test Frequency Ratio of interactive to automated Design Design robustness Integration Smoothness of code integration Case Study: TDD Concerns

  22. Defect Rate 50% improvement Productivity Below initial estimates (<400 LOC) Test Frequency 86% of tests were automated Design Aided in late changes Integration Testing made problems surface early Case Study: TDD Results

  23. Results from Microsoft Study Tested 4 products Pre-release defect density reduces - 40% to 90% Increase in initial development time - 15 – 35% Case Study: TDD Results(2)

  24. Why write the test before the code? • Think through the requirement • Think about the design and usability of the API • There’s never time to write it afterwards • You write much less tests (if at all) otherwise

  25. Why make it fail first? • Make sure the test does not have a bug • Make sure you’re testing the right thing • Define a small problem to solve • When the test passes, you are done • If you can’t write that test, you may • Not understand the requirement • Have over designed things (not testable) • Not have a small enough problem to solve

  26. Why Refactor? • Constantly improve the design of the code • Unit tests act as safety net • Remove duplication, improve readability and maintainability • You’ll need to when things change (requirements, your understanding of them, other factors..)

  27. Real World • It’s not easy to learn • Dealing with legacy code • Requires a lot of discipline • You have to want to do it • But once you do… • It gets easier and easier • It’s a hard habit to let go

  28. Pillars of good unit tests • Making your tests trustworthy • Creating maintainable tests • Readable tests – last but not least!

More Related