Advanced Unit Testing – real life
examples and mistakes
Milan Vukoje
www.Vukoje.NET
vukoje@gmail.com
 Goals & Dangers
 Real life examples
 VS & TFS tips
 Best Practices
 Smells
Themes
 Improved quality
 SUT understanding
 Reduced risk
 Easy creation/maintaining
 Easy run
Goals of UT
 Additional costs
 Slowing down production code changing
 Losing tests in logic migration
 Tests take too long to writ...
Soprex Core Application (SCA)
DB
Win Web
UI
BL
DAL
SCRUM
Refactoring
Coding Standard
Code Review
 DB problems
 Persistent
 Slow
 Shared
 Complicated
Persistent fixture
DB
Win Web
UI
BL
DAL
 Shared Fixture + Partial cleanup
 Cleanup in Finally blocks
SCA.DAL – Cleanup Strategy
 Not treating tests as production code
 Do not duplicate test code
 Creation and Finder methods
 Custom Assertations
...
Database Sandbox
 Generic CRUD tests
 Uses generated values
 Tries to break constraints
 Insert nulls
 Max values
 Mini integration t...
SCA.BLTester example
SCA.BLTester cleanup
 Isolated Business Logic testing
 Mini Integration vs. FakeDB
 QueryObject + DataTable = FakeDB
 SCA.MockHelper
 Setu...
 Avoid Fragile Tests (To Pass or Not to Pass)
 Test Run War
 Non- deterministic tests
 Interacting tests
 Don’t use D...
 Very important
 Bug can be very expensive
 Huge num. of combinations
 Integration Test failure
SCA.PriceCalculator
DB...
 Price Manager
 Simple calculations
 Easily tested with FakeDB
 Highly optimized cache
 Price Calculator
 Main algor...
When you have a new hammer, everything looks like a nail!
 Calculator example
 Use the Front Door First
 Guard Assert E...
Use Case testing
DB
Win Web
UI
BL
DAL
 Model – View – Problem
 Presenter testing problems
 Not Isolated (GUI)
 Hard Indirect output control
 Testing Mocks
...
 Design for Testability
 Just a bit design upfront
 Be ware of the code smells
 Keep It Simple and Stupid (KISS)
 Alw...
 Minimize untestable code
 Auto-synchronization
 Helper classes
 Indirect GUI control
 Don’t test GUI
 Also… buy GUI...
 Avoid Accessors
 Do not generate unit tests
 Avoid assertion roulette
 Use Assert class properly
Visual Studio tips
 Test Run & Build Policy
 Check-in Build & Build With Tests
 Nightly build with Code Analysis and Tests
 Build Error N...
 Write the Tests First
 Communicate Intent
 Do not modify the SUT
 Keep Tests Independent
 Minimize Test Overlap
 Ke...
 Design
 Long setup
 Hard to test code
 Slow tests
 Test logic in production
 Code
 Obscure test
 High test mainte...
 UT have grate benefits
 UT comes with dangers
 Watch the smells?
 Follow best practices
Summary
Questions?
Please fill the questionnaire !
You have a chance to win:
Sponsored by:
Thanks!
Milan Vukoje
www.Vukoje.NET
vukoje@gmail.com
Upcoming SlideShare
Loading in …5
×

Advanced unit testing – real life examples and mistakes

2,722 views

Published on

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

No Downloads
Views
Total views
2,722
On SlideShare
0
From Embeds
0
Number of Embeds
17
Actions
Shares
0
Downloads
29
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

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

×