Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Advanced unit testing – real life examples and mistakes

3,077 views

Published on

Published in: Technology
  • Be the first to comment

Advanced unit testing – real life examples and mistakes

  1. 1. Advanced Unit Testing – real life examples and mistakes Milan Vukoje www.Vukoje.NET vukoje@gmail.com
  2. 2.  Goals & Dangers  Real life examples  VS & TFS tips  Best Practices  Smells Themes
  3. 3.  Improved quality  SUT understanding  Reduced risk  Easy creation/maintaining  Easy run Goals of UT
  4. 4.  Additional costs  Slowing down production code changing  Losing tests in logic migration  Tests take too long to write  Poor Organization /Maintenance problems Dangers of UT
  5. 5. Soprex Core Application (SCA) DB Win Web UI BL DAL SCRUM Refactoring Coding Standard Code Review
  6. 6.  DB problems  Persistent  Slow  Shared  Complicated Persistent fixture DB Win Web UI BL DAL
  7. 7.  Shared Fixture + Partial cleanup  Cleanup in Finally blocks SCA.DAL – Cleanup Strategy
  8. 8.  Not treating tests as production code  Do not duplicate test code  Creation and Finder methods  Custom Assertations  Parameterized tests  Test Run War SCA.DAL DB Win Web UI BL DAL
  9. 9. Database Sandbox
  10. 10.  Generic CRUD tests  Uses generated values  Tries to break constraints  Insert nulls  Max values  Mini integration tests  Great for detecting DB schema mismatch SCA.BLTester BL DAL DB BLTester
  11. 11. SCA.BLTester example
  12. 12. SCA.BLTester cleanup
  13. 13.  Isolated Business Logic testing  Mini Integration vs. FakeDB  QueryObject + DataTable = FakeDB  SCA.MockHelper  Setup with Reflection  Factory would be better choice SCA.FakeDB BL DAL DB Fake DB
  14. 14.  Avoid Fragile Tests (To Pass or Not to Pass)  Test Run War  Non- deterministic tests  Interacting tests  Don’t use DB accident/random records  When there is a way of testing w/o the DB, test w/o DB.  SCA.AppTranslation  Testing w/o DB  And than back to DB… Lessons Learned DB Win Web UI BL DAL
  15. 15.  Very important  Bug can be very expensive  Huge num. of combinations  Integration Test failure SCA.PriceCalculator DB Price Calculator BL DAL Price Request Price
  16. 16.  Price Manager  Simple calculations  Easily tested with FakeDB  Highly optimized cache  Price Calculator  Main algorithm  Easily tested with Stub  Concurrency control Stub to the rescue! DB Price Calculator BL DAL Price Request Price Price Manager
  17. 17. When you have a new hammer, everything looks like a nail!  Calculator example  Use the Front Door First  Guard Assert Example Overdoing mocking
  18. 18. Use Case testing DB Win Web UI BL DAL
  19. 19.  Model – View – Problem  Presenter testing problems  Not Isolated (GUI)  Hard Indirect output control  Testing Mocks  Solution  View Stub  Setup Methods SCA.UI DB Win Web UI BL DAL
  20. 20.  Design for Testability  Just a bit design upfront  Be ware of the code smells  Keep It Simple and Stupid (KISS)  Always know what you are testing Lessons learned DB Win Web UI BL DAL
  21. 21.  Minimize untestable code  Auto-synchronization  Helper classes  Indirect GUI control  Don’t test GUI  Also… buy GUI controls  SCA.GUI DB Win Web UI BL DAL
  22. 22.  Avoid Accessors  Do not generate unit tests  Avoid assertion roulette  Use Assert class properly Visual Studio tips
  23. 23.  Test Run & Build Policy  Check-in Build & Build With Tests  Nightly build with Code Analysis and Tests  Build Error Notification TFS - Continues Integration with
  24. 24.  Write the Tests First  Communicate Intent  Do not modify the SUT  Keep Tests Independent  Minimize Test Overlap  Keep Test Logic Out of Production Code  Verify One Condition per Test More best practices…
  25. 25.  Design  Long setup  Hard to test code  Slow tests  Test logic in production  Code  Obscure test  High test maintenance  Conditional test logic  Duplication  Frequent Debugging  Production bugs Smells
  26. 26.  UT have grate benefits  UT comes with dangers  Watch the smells?  Follow best practices Summary
  27. 27. Questions?
  28. 28. Please fill the questionnaire ! You have a chance to win: Sponsored by:
  29. 29. Thanks! Milan Vukoje www.Vukoje.NET vukoje@gmail.com

×