Advanced unit testing – real life examples and mistakes

29
Advanced Unit Testing – real life examples and mistakes Milan Vukoje www.Vukoje.NET [email protected]

Transcript of Advanced unit testing – real life examples and mistakes

Page 1: Advanced unit testing – real life examples and mistakes

Advanced Unit Testing – real life examples and mistakes

Milan Vukojewww.Vukoje.NET

[email protected]

Page 2: Advanced unit testing – real life examples and mistakes

Goals & Dangers Real life examples VS & TFS tips Best Practices Smells

Themes

Page 3: Advanced unit testing – real life examples and mistakes

Improved quality SUT understanding Reduced risk Easy creation/maintaining Easy run

Goals of UT

Page 4: Advanced unit testing – real life examples and mistakes

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

Page 5: Advanced unit testing – real life examples and mistakes

Soprex Core Application (SCA)

DB

Win Web

UI

BL

DAL

SCRUMRefactoring

Coding StandardCode Review

Page 6: Advanced unit testing – real life examples and mistakes

DB problems Persistent Slow Shared Complicated

Persistent fixture

DB

Win Web

UI

BL

DAL

Page 7: Advanced unit testing – real life examples and mistakes

Shared Fixture + Partial cleanup Cleanup in Finally blocks

SCA.DAL – Cleanup Strategy

Page 8: Advanced unit testing – real life examples and mistakes

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

Page 9: Advanced unit testing – real life examples and mistakes

Database Sandbox

Page 10: Advanced unit testing – real life examples and mistakes

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

Page 11: Advanced unit testing – real life examples and mistakes

SCA.BLTester example

Page 12: Advanced unit testing – real life examples and mistakes

SCA.BLTester cleanup

Page 13: Advanced unit testing – real life examples and mistakes

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

Page 14: Advanced unit testing – real life examples and mistakes

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

Page 15: Advanced unit testing – real life examples and mistakes

Very important Bug can be very expensive Huge num. of combinations Integration Test failure

SCA.PriceCalculator

DB

Price Calculator

BL

DAL

PriceRequest Price

Page 16: Advanced unit testing – real life examples and mistakes

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

Page 17: Advanced unit testing – real life examples and mistakes

When you have a new hammer, everything looks like a nail!

Calculator example Use the Front Door First Guard Assert Example

Overdoing mocking

Page 18: Advanced unit testing – real life examples and mistakes

Use Case testing

DB

Win Web

UI

BL

DAL

Page 19: Advanced unit testing – real life examples and mistakes

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

Page 20: Advanced unit testing – real life examples and mistakes

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

Page 21: Advanced unit testing – real life examples and mistakes

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

Page 22: Advanced unit testing – real life examples and mistakes

Avoid Accessors Do not generate unit tests Avoid assertion roulette Use Assert class properly

Visual Studio tips

Page 23: Advanced unit testing – real life examples and mistakes

Test Run & Build Policy Check-in Build & Build With Tests Nightly build with Code Analysis and Tests Build Error Notification

TFS - Continues Integration with

Page 24: Advanced unit testing – real life examples and mistakes

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…

Page 25: Advanced unit testing – real life examples and mistakes

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

Page 26: Advanced unit testing – real life examples and mistakes

UT have grate benefits UT comes with dangers Watch the smells? Follow best practices

Summary

Page 27: Advanced unit testing – real life examples and mistakes

Questions?

Page 28: Advanced unit testing – real life examples and mistakes

Please fill the questionnaire !

You have a chance to win:

Sponsored by:

Page 29: Advanced unit testing – real life examples and mistakes

Thanks!

Milan Vukojewww.Vukoje.NET

[email protected]