Unit testing

546 views

Published on

My slides from the ERDDUG June 2011 meeting. The slides cover Unit Testing, what makes code hard to test and applying polymorphism to wire your application up differently in testing and production configurations.

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
546
On SlideShare
0
From Embeds
0
Number of Embeds
44
Actions
Shares
0
Downloads
5
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Unit testing

  1. 1. Unit testing,mocking &tdd with .net<br />ERDDUG – 11 June 2011<br />
  2. 2. Types of Testing<br />Scenario Testing<br />You pretend to be a user.<br />The app gets tested as a single unit. You’re testing by runningstatic void Main()<br />Can’t test all edge cases.<br />Functional Testing<br />Sub-systems of the app get tested in isolation, e.g. by substituting databases with XML data sources.<br />Ensures that groups of classes interact with each other correctly.<br />When green – sure the subsystem works correctly.<br />When red – easier to reproduce, but still need a debugger to find the exact problem.<br />
  3. 3. Unit Testing<br />Tests each individual class in isolation.<br />Can simulate all edge cases and error conditions<br />Fast – only instantiates the part of the app that’s actually needed for the test<br />When green – know the class is OK in isolation, but haven’t tested interaction with others classes. (Functional test)<br />When red – know we can reproduce the error easily.<br />
  4. 4. What makes code hard to test?<br />Real issues<br /><ul><li>Static calls
  5. 5. Singletons
  6. 6. Mixing new with logic
  7. 7. Work in constructor (static)
  8. 8. Global state
  9. 9. Deep inheritance
  10. 10. Conditionals
  11. 11. Mixing concerns</li></ul>Usual answers…<br /><ul><li>Make things private
  12. 12. Use sealed
  13. 13. Long methods</li></li></ul><li>Unit Testing Classes<br />Other Class<br />Class Under Test<br />Test Driver<br />Other Class<br />Other Class<br />Object Lifetime and Calling<br />Object Instantiated<br />Object Passed In<br />Global Object<br />
  14. 14. Unit Testing Classes<br />Other Class<br />File<br />System<br />CPU Intensive<br />Seam<br />Other Class<br />Other Class<br />Class Under Test<br />Test Driver<br />Other Class<br />Destructive operation<br />Other Class<br />Other Class<br />Other Servers<br />Object Lifetime and Calling<br />Object Instantiated<br />Object Passed In<br />Global Object<br />
  15. 15. Unit Testing Classes<br />Seam (Polymorphism)<br />Other Class<br />Friendly<br />Class Under Test<br />Test Driver<br />Other Class<br />Friendly<br />Other Class<br />Object Lifetime and Calling<br />Friendly<br />Object Instantiated<br />Object Passed In<br />Global Object<br />
  16. 16. Unit Testing Classes<br />Seam<br />Dependency<br />Injection<br />Friendly<br />Class Under Test<br />Test Driver<br />Friendly<br />Friendly<br />Object Lifetime and Calling<br />Object Instantiated<br />Object Passed In<br />Global Object<br />

×