WordPress Websites for Engineers: Elevate Your Brand
Advanced unit testing – real life examples and mistakes
1. Advanced Unit Testing – real life
examples and mistakes
Milan Vukoje
www.Vukoje.NET
vukoje@gmail.com
2. Goals & Dangers
Real life examples
VS & TFS tips
Best Practices
Smells
Themes
3. Improved quality
SUT understanding
Reduced risk
Easy creation/maintaining
Easy run
Goals of UT
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. Soprex Core Application (SCA)
DB
Win Web
UI
BL
DAL
SCRUM
Refactoring
Coding Standard
Code Review
6. DB problems
Persistent
Slow
Shared
Complicated
Persistent fixture
DB
Win Web
UI
BL
DAL
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
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
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. 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. 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. 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. When you have a new hammer, everything looks like a nail!
Calculator example
Use the Front Door First
Guard Assert Example
Overdoing mocking
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. 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. 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. Avoid Accessors
Do not generate unit tests
Avoid assertion roulette
Use Assert class properly
Visual Studio tips
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. 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. 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. UT have grate benefits
UT comes with dangers
Watch the smells?
Follow best practices
Summary