Successful Unit testing for large applications

1,059 views

Published on

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Successful Unit testing for large applications

  1. 1. SUCCESSFUL UNIT TESTING FOR LARGE APPLICATIONS Erik LeBel elebel@pyxis-tech.com
  2. 2. © Pyxis Technologies inc. PYXIS STUDIO
  3. 3. SETTING THE SCENE
  4. 4. IF YOU ARE USING AUTOMATED TESTING, THEN… © Pyxis Technologies inc.  You are trying to reduce frequency with which regressions get pushed into production  You are trying to speed up validation for quicker release cycles  You are cool  You‟ve been told to do so by someone higher up in your organization
  5. 5. YOU ARE INTERESTED IN THIS PRESENTATION BECAUSE… © Pyxis Technologies inc.  Your project has tests, but they aren't helping you deliver stable software  Your project has tests, but they‟re a nightmare to maintain  Your project has tests, but they are a haphazard mess in the making  You like tests
  6. 6. We will be covering:  practices and concerns to help make automated tests an enabling force We are not discussing: © Pyxis Technologies inc.  why we automate testing
  7. 7. © Pyxis Technologies inc. SO, WHAT‟S THIS ABOUT DEBILITATING TESTS?
  8. 8. THIS IS OFTEN BECAUSE… But we want them:  Are hard to understand  Break frequently  Are slow to execute  Are hard to fix  Let bugs through  Don‟t validate anything useful © Pyxis Technologies inc. Tests:  To be clear to understand  To break legitimately  To be fast to execute  To be concise
  9. 9. MAKE TESTS READABLE
  10. 10. CLEAN UP TEST SETUP  Use OO practices to:  Make setup code easy to read  Unclutter © Pyxis Technologies inc.  Use factory methods  Use fluent builders  Can build complex actions as well as objects
  11. 11. COLLECTION ASSERTS © Pyxis Technologies inc.  Simplify result validation  Can easily handle unordered cases  Write your own if your test framework doesn‟t have any
  12. 12. USE LITERALS © Pyxis Technologies inc.  They are easier to read
  13. 13. BUILD INTUITIVE ASSERTIONS  Use extension methods for concise type assertions © Pyxis Technologies inc.  Build utility methods for complex validations
  14. 14. USE TEST DSLS FOR INTEGRATION TESTS Domain Specific Languages (DSLS) © Pyxis Technologies inc.  Test API wrappers  Model wrappers
  15. 15. USE READABLE DATA FORMATS © Pyxis Technologies inc.  Capture large expected value sets in common file formats such as XML, JSON, YAML  Write assertion utilities that highlight multiple differences between expected and actual values (eg XmlUnit)
  16. 16. © Pyxis Technologies inc. AS TESTS GET CLEANER You will more easily spot redundant or useless tests Tests should start to match business requirements Its should start to be clearer where there are gaps The factories, builders and helpers that you develop for testing will make it easier to add new tests scenarios
  17. 17. AT ALL COST, AVOID… © Pyxis Technologies inc.  „If‟ statements in tests  Loops in tests  Exception handling
  18. 18. BALANCE
  19. 19. THE NUMBERS SHOW THAT… $$$ Time to execute Fragility Lines of test code Effort to fix Overall cost of use System tests Integration tests © Pyxis Technologies inc. Unit tests $
  20. 20. BUILD YOUR TESTS AROUND YOUR MODULES Module1.dll Module2.dll © Pyxis Technologies inc. Module1.Unit.Tests.dll Module2.Unit.Tests.dll Module1.Integration.Tests.dll Module2.Integration.Tests.dll
  21. 21. TECHNOLOGIES THAT HINDER TESTING © Pyxis Technologies inc.       Configurations Databases File systems External services Sandboxing technologies Flashy, shiny widgets  Use them… don‟t let it be the other way around
  22. 22. © Pyxis Technologies inc. GETTING THERE IN BABY STEPS  Don‟t expect to stop delivering software while fixing things  Don‟t be afraid to experiment  When a pattern emerges, make sure it‟s communicated to the whole team  Don‟t get hung up on the “green field” vs “brown field” over time even the greenest fields get muddy  This is and will continue to be an organic process
  23. 23. BUILDING TRUST
  24. 24. DO OR DO NOT  Commit to keeping tests green all the time. © Pyxis Technologies inc.  If it‟s broken, fix it, or delete it.
  25. 25. AUTOMATE TEST EXECUTION  Use a Continuous Integration tool:  TFS, CruiseControl.NET, Hudson/Jenkins, roll your own  Don‟t allow the build to stay broken © Pyxis Technologies inc.  Make sure your integration build and tests are run as often as possible  Split long-running test suites to run as part of an alternate set of build jobs
  26. 26. WATCH CODE COVERAGE  Work to improve test coverage  Beware of high test coverage and low software quality © Pyxis Technologies inc.  And remember it‟s just an indicator
  27. 27. INSPECT AND ADAPT  Monitor test and production failures  Use production issues to infer how deep the iceberg is © Pyxis Technologies inc.  When something fails, find the cause, and improve your test and application design
  28. 28. FAULT SEEDING  Test the reliability of your tests by seeding bugs and checking the detection rate © Pyxis Technologies inc.  Automate the activity with a mutation testing framework:  See http://www.mutation-testing.net
  29. 29. CONCLUSION
  30. 30. TAKE AWAY  Treat your tests as first order citizens  Work to improve your feedback cycle © Pyxis Technologies inc.  Commit or get out, but don‟t go in halfway
  31. 31. Erik LeBel elebel@pyxis-tech.com Questions? T h a n k yo u ! Titre sur mesure POINTS FORTS 1 pyxis-tech.com

×