1 / 55

The Path to Unit Testing

The Path to Unit Testing. Contact Information. Wes McClure wes.mcclure@gmail.com wmcclure@phxwg.com @g0t4. Poll – How do you test?. Manual Testing Automated Testing QA? Deploy and unplug the phone!. Agenda. Before Testing Unit Testing Frameworks Dependencies Legacy Testing

calvin
Download Presentation

The Path to Unit Testing

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. The Path to Unit Testing

  2. Contact Information • Wes McClure • wes.mcclure@gmail.com • wmcclure@phxwg.com • @g0t4

  3. Poll – How do you test? • Manual Testing • Automated Testing • QA? • Deploy and unplug the phone!

  4. Agenda • Before Testing • Unit Testing Frameworks • Dependencies • Legacy Testing • Good Testing

  5. Before Testing

  6. Scenario • Weather on company portal. • NOAA web service • Input zip code • Get latitude / longitude • Show weather forecast

  7. Where to start? • Build a Prototype? • Console app • Move into web app

  8. Ok Done! • What is done? • Hanselminutes #119 • Tested?

  9. It was tested … manually • How? • Run program • Provide input • Execute action • Subjectively verify output

  10. Problems with Manual • Thrown away • In the noggin • Not documented • Easily forgotten • Documented • Refactorability • Easy paths • 1 bug fixed = 2 new

  11. How can I fix this? • Automate

  12. Unit Testing • Definition (wikipedia) • Is a software verification and validation method in which a programmer tests if individual units of source code are fit for use. A unit is the smallest testable part of an application. … in object-oriented programming, the smallest unit is a class… • Testing the smallest piece

  13. Unit Test Composition • AAA • Arrange (Given) • Act (When) • Assert (Then) • Same as manual template

  14. States • Passing • Failing • Not Run

  15. Questions? Before Testing

  16. Unit Testing Frameworks

  17. Test Discovery • Inheritance • Naming Convetions • Attributes

  18. NUnit Test Discovery • Basic Attributes • TestFixture • Test • Ignore • Category

  19. Test Execution • Exectuion • Selection • Run • Results (State) • Types • Console • GUI • IDE Integrated

  20. NUnit Test Execution • Runners • Console • GUI • MiniGUI • IDE Integrated • TestDriven.Net • R# Test Runner • Gallio

  21. Assertions • Classic • Fluent “Fluent is the new xml”

  22. NUnit Assertion Syntax • Classic Assertions • Fluent Constraints • AssertionHelper • Expect • Constraints • Is • Has • Throws

  23. Putting it all together • Hello World Test

  24. More NUnit Attributes

  25. SetUp& TearDown • Per Test • Once for Test Fixture • Once for Namespace • Cautious

  26. Parameterized Tests • TestCase • Result • TestName • Theory • DataPoint • Caution

  27. Attributes to Avoid • Random • Tests should be repeatable! • Sequential • Tests shouldn’t be interdependent! • Repeat • Win the lottery?

  28. Other attributes • Localization • Culture • SetCulture • Labeling • Description • Suite • Property • Parametric • Attributes • Values • ValueSource • Range • Combinatorial • Pairwise • TestCaseSource • Timing (async) • MaxTime • Timeout • Threading • RequiresMTA • RequiresSTA • RequiresThread • Others • Explicit • RequiredAddIn • Suite • ExpectedException • Platform

  29. Questions? Unit Testing Frameworks (with NUnit)

  30. Dependencies another word for "testable" is "decoupled"

  31. Dependencies

  32. Weather Dependencies • Input to SUT • Acted upon

  33. Unit Testing with Dependencies • Unit Testing => Isolated Unit • Stubs • Simulate Dependencies • Return Dummy Data • Mocks • Record Calls

  34. Manual Stubs & Mocks • Roll our own • Cons • Low reuse • Boilerplate • Complex logic

  35. Poll • Who has used an isolation framework?

  36. Isolation Frameworks • Dynamic stubs • Proxies • Setup results • Dynamic mocks • Record calls • Verification syntax • Example

  37. Isolation Frameworks • Rhino.Mocks • TypeMock • Moq • NMock2

  38. Questions?

  39. Legacy Testing

  40. Define Legacy • Old • Untested – Michael Feathers • Missing seams

  41. Why Change Legacy Code? • Fix Bugs • Repeats • Add Features • Integrated • Isolated • Improve Design • 80/20 • Improve Moral

  42. Fixing Bugs • Example

  43. Legacy Testing @martinfowler @ JoshuaKerievsky

  44. Questions? Legacy Testing

  45. Good Unit Tests

  46. But… • Anything can be done but it takes effort to do it well! • Consistency in choices • Best Practices • Try alternatives

  47. Good Unit Tests • From “The Art of Unit Testing” • Readable • Maintainable • Trustworthy • For us and others @RoyOsherove http://www.artofunittesting.com/ http://weblogs.asp.net/ROsherove/

  48. Readability • Naming Convention • Method_Scenario_Expectation • Explicit Variables • Call stubs & mocks as such! • Fluent Assertions • var

  49. Readability • AAA • Physical Separate

  50. Readability • Single Assertion • SRP methods • Transfer? • Credit • Debit • Insufficient funds • Transactional • Success / Failure • Auditing

More Related