Overcoming the Obstacles,Pitfalls, and Dangers ofAutomated Testing      Stephen D. Ritchie      13-Nov-2012
Live Tweet, Tonight’s Slides and Examples• Twitter:      @RuthlessHelp                @Apress• Slideshare: http://www.slid...
Purpose               Useful          Automated Testing   Make Software Better                Excella Consulting
Agenda  1   Motivation  2   Principles  3   Obstacles         Excella Consulting   -4-
Chrysler New Yorker                      Excella Consulting
First Topic: Motivation                    Why Write                    Unit Tests?             Why Automate              ...
Agenda  1   Motivation  2   Principles  3   Obstacles         Excella Consulting   -7-
Visibility & InsightProblem DetectionAdvance Warning
Microscope: Visibility and Insight                               Excella Consulting
Smoke Detector: Problem Detection                           Excella Consulting
Weather Satellite: Advance Warning                            Excella Consulting
Example           Perhaps          An Example           Would Be            Helpful             Excella Consulting
Automated Tests  Make SureSoftware Works  As Intended
Agenda  1   Motivation  2   Principles  3   Obstacles         Excella Consulting   - 14 -
Principles        Zero Configuration                  Fast               Clear Result             Easy To Maintain        ...
Automated Testing: Vocabulary• Test-Driven Development (TDD)    – Write a Test, Watch the Test Fail    – Write Code, Make ...
Principles        Zero Configuration I can run your tests, You can run mine.              Excella Consulting
Principles             Fast    All the tests run in    just a few minutes             Excella Consulting
Principles             Clear Results              Pass/Fail             Focused Test                 Excella Consulting
Principles             Easy to Maintain             Conventional                  Brief                  Excella Consulting
The Long-Term Goals• Automated Testing    – Vigilantly Monitoring the Code• Readability    – Have Mercy on Future Develope...
Agenda  1   Motivation  2   Principles  3   Obstacles         Excella Consulting   - 22 -
One Primary Assert To Rule Them All                   Obstacle 1        Over-Specifying                            Excella...
One Primary Assert To Rule Them All• Is your effort to refactor and improve code overwhelmed by the  time it takes to main...
One Primary Assert To Rule Them All• Debate: Only one assertion per test?• Test Method Tests One and Only One Scenario    ...
Four Ways to Fake Time                  Obstacle 2             Time Crunch                         Excella Consulting
Four Ways to Fake Time• Are your test methods starting to fail because the code-under-test is  coupled to the system clock...
Four Ways to Fake Time• Things to Watch For    – Thread Safety       • public static class SystemDateTime   – Making Your ...
Database Killed The Integration Test                   Obstacle 3                   Database                             E...
Database Killed The Integration Test• Are your automated integration tests failing because of the data in  the testing dat...
Database Killed The Integration Test• Automated Testing Persistence    – NDbUnit    – SQL Server Express    – NHibernate• ...
In Test Code, Do Repeat Yourself ... Do Repeat Yourself         Obstacle 4     Unhelpful …        Reuse        Repetition ...
In Test Code, Do Repeat Yourself ... Do Repeat Yourself• Do you have an explosion of test methods, with the ratio of test ...
In Test Code, Do Repeat Yourself ... Do Repeat Yourself• Data-Drive Test Cases    – One Test Method    – Many Test Scenari...
Agenda  1   Motivation  2   Principles  3   Obstacles         Excella Consulting   - 35 -
Of Course It’s Safe … After You                             Excella Consulting
Further Discussion            Any questions?           Any comments?                     Excella Consulting
Shameless Self Promotion Time!                            40% off eBook at                            apress.com          ...
Contact Me• Twitter:    @ruthlesshelp• Email:      stephen.ritchie@excella.com• Blog:       http://ruthlesslyhelpful.net• ...
Slides and Examples• Slideshare: http://www.slideshare.net/ruthlesshelp• Github:      http://github.com/ruthlesshelp      ...
Upcoming SlideShare
Loading in …5
×

Overcoming the Obstacles, Pitfalls, and Dangers of Unit Testing

1,247
-1

Published on

Have you ever bumped into a wall with your automated tests? Many developers bump into various roadblocks and hurdles when writing test code. Are your test methods starting to fail because the code-under-test uses DateTime.Now? Are your automated integration tests failing because the database they integrate with keeps changing? Do you have an explosion of test methods, with the ratio of test code to code-under-test way too high? Is your effort to refactor and improve code overwhelmed by the time it takes to rewrite all those failing unit tests?


This presentation is about clearing away automated testing obstacles, avoiding common pitfalls, and staying away from dangerous practices.

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
1,247
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
10
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Overcoming the Obstacles, Pitfalls, and Dangers of Unit Testing

  1. 1. Overcoming the Obstacles,Pitfalls, and Dangers ofAutomated Testing Stephen D. Ritchie 13-Nov-2012
  2. 2. Live Tweet, Tonight’s Slides and Examples• Twitter: @RuthlessHelp @Apress• Slideshare: http://www.slideshare.net/ruthlesshelp• Github: http://github.com/ruthlesshelp Excella Consulting
  3. 3. Purpose Useful Automated Testing Make Software Better Excella Consulting
  4. 4. Agenda 1 Motivation 2 Principles 3 Obstacles Excella Consulting -4-
  5. 5. Chrysler New Yorker Excella Consulting
  6. 6. First Topic: Motivation Why Write Unit Tests? Why Automate Testing? Excella Consulting
  7. 7. Agenda 1 Motivation 2 Principles 3 Obstacles Excella Consulting -7-
  8. 8. Visibility & InsightProblem DetectionAdvance Warning
  9. 9. Microscope: Visibility and Insight Excella Consulting
  10. 10. Smoke Detector: Problem Detection Excella Consulting
  11. 11. Weather Satellite: Advance Warning Excella Consulting
  12. 12. Example Perhaps An Example Would Be Helpful Excella Consulting
  13. 13. Automated Tests Make SureSoftware Works As Intended
  14. 14. Agenda 1 Motivation 2 Principles 3 Obstacles Excella Consulting - 14 -
  15. 15. Principles Zero Configuration Fast Clear Result Easy To Maintain Excella Consulting
  16. 16. Automated Testing: Vocabulary• Test-Driven Development (TDD) – Write a Test, Watch the Test Fail – Write Code, Make the Test Pass – Write the Next Test• Behavior-Driven Development (BDD) – Given a Desired Behavior• Intention Checking – The Software Works, As Intended Excella Consulting
  17. 17. Principles Zero Configuration I can run your tests, You can run mine. Excella Consulting
  18. 18. Principles Fast All the tests run in just a few minutes Excella Consulting
  19. 19. Principles Clear Results Pass/Fail Focused Test Excella Consulting
  20. 20. Principles Easy to Maintain Conventional Brief Excella Consulting
  21. 21. The Long-Term Goals• Automated Testing – Vigilantly Monitoring the Code• Readability – Have Mercy on Future Developers • Conventional • Short, Clear• Maintainability – Both a Sword and a Shield • Code Works as Intended • Protects Against Regression – Reliable – N+1 is Easy Excella Consulting
  22. 22. Agenda 1 Motivation 2 Principles 3 Obstacles Excella Consulting - 22 -
  23. 23. One Primary Assert To Rule Them All Obstacle 1 Over-Specifying Excella Consulting
  24. 24. One Primary Assert To Rule Them All• Is your effort to refactor and improve code overwhelmed by the time it takes to maintain/update/rewrite all those failing unit tests? – Your test-code could be over specifying things.• Perhaps an example would be helpful … Excella Consulting
  25. 25. One Primary Assert To Rule Them All• Debate: Only one assertion per test?• Test Method Tests One and Only One Scenario – 1 Primary Assert Verifies and Validates the Scenario• Secondary Asserts – Support Arrangement and Preconditions – Support Post-Conditions• Avoid Asserts that Over Specify – Too Literal => Inhibited Refactoring – Imagined Benefit => Rigidity Excella Consulting
  26. 26. Four Ways to Fake Time Obstacle 2 Time Crunch Excella Consulting
  27. 27. Four Ways to Fake Time• Are your test methods starting to fail because the code-under-test is coupled to the system clock? – Your code is too dependent on System.DateTime.Now• Perhaps an example would be helpful … Excella Consulting
  28. 28. Four Ways to Fake Time• Things to Watch For – Thread Safety • public static class SystemDateTime – Making Your Privates Public // Inject the class dependency on DateTime.Now private DateTime? _now; public DateTime Now { get { return _now ?? DateTime.Now; } set { _now = value; } } Excella Consulting
  29. 29. Database Killed The Integration Test Obstacle 3 Database Excella Consulting
  30. 30. Database Killed The Integration Test• Are your automated integration tests failing because of the data in the testing database; the data keeps changing?• Perhaps an example would be helpful … Excella Consulting
  31. 31. Database Killed The Integration Test• Automated Testing Persistence – NDbUnit – SQL Server Express – NHibernate• Surface Testing – Data Access Layer: API Surface – Liberates Refactoring Excella Consulting
  32. 32. In Test Code, Do Repeat Yourself ... Do Repeat Yourself Obstacle 4 Unhelpful … Reuse Repetition Coupling Excella Consulting
  33. 33. In Test Code, Do Repeat Yourself ... Do Repeat Yourself• Do you have an explosion of test methods, with the ratio of test code to code-under-test that’s way too high? – Your test-code is too DRY in some places and … – Not DRY enough in all the right places• Perhaps an example would be helpful … Excella Consulting
  34. 34. In Test Code, Do Repeat Yourself ... Do Repeat Yourself• Data-Drive Test Cases – One Test Method – Many Test Scenarios• Repeat Code in a “TestsContext” Class – Sidecar Approach• Use Helper Classes – Extension methods – Composition• Keep Inheritance in Reserve – Overall Testing Framework Excella Consulting
  35. 35. Agenda 1 Motivation 2 Principles 3 Obstacles Excella Consulting - 35 -
  36. 36. Of Course It’s Safe … After You Excella Consulting
  37. 37. Further Discussion Any questions? Any comments? Excella Consulting
  38. 38. Shameless Self Promotion Time! 40% off eBook at apress.com Use promo code: PR0N3T Offer ends 15-Dec-2012 Excella Consulting
  39. 39. Contact Me• Twitter: @ruthlesshelp• Email: stephen.ritchie@excella.com• Blog: http://ruthlesslyhelpful.net• LinkedIn: http://www.linkedin.com/in/sritchie Excella Consulting
  40. 40. Slides and Examples• Slideshare: http://www.slideshare.net/ruthlesshelp• Github: http://github.com/ruthlesshelp Excella Consulting
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×