Advanced unit testing – real life examples and mistakes
-
Upload
milan-vukoje -
Category
Technology
-
view
2.001 -
download
0
Transcript of Advanced unit testing – real life examples and mistakes
Advanced Unit Testing – real life examples and mistakes
Milan Vukojewww.Vukoje.NET
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 write Poor Organization /Maintenance problems
Dangers of UT
Soprex Core Application (SCA)
DB
Win Web
UI
BL
DAL
SCRUMRefactoring
Coding StandardCode 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 Parameterized tests
Test Run War
SCA.DAL
DB
Win Web
UI
BL
DAL
Database Sandbox
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
SCA.BLTester example
SCA.BLTester cleanup
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
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
Very important Bug can be very expensive Huge num. of combinations Integration Test failure
SCA.PriceCalculator
DB
Price Calculator
BL
DAL
PriceRequest Price
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
PriceRequest Price
Price Manager
When you have a new hammer, everything looks like a nail!
Calculator example Use the Front Door First Guard Assert Example
Overdoing mocking
Use Case testing
DB
Win Web
UI
BL
DAL
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
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
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
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 Notification
TFS - Continues Integration with
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…
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
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: