Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Introduction to Unit Testing

July 23, 2012

Introduction to Unit Testing

Basic introduction to unit testing.


July 23, 2012

More Decks by Jaime

Other Decks in Programming


  1. Table Of Contents • Putting Things in Context • What

    is Unit Testing? • Why should I care? • How does it work? • Unit Tests in Action • Extra Goodies
  2. Unit Testing in Software • Functional Tests – Unit Tests

    – Integration Tests – System Tests – System Integration Tests • Non Functional Tests – Security, Performance, Usability…
  3. Automated and Repeatable • Run With One Click* • Deterministic

    • Once Written Remain For Future Use • Anyone Can Run Them • Easy to Implement and Run • Run Quickly * It can be fully automated
  4. Unit Tests. Summary • Test Units of Code in Isolation

    • Automated and Repeatable • Easy to Implement and Run • Run Quickly • Trustworthy, Readable and Maintainable
  5. Unit tests provide a safety-net and a sense of security

    it’s more fun to code with them than without low-level regression test suite refactor mercilessly improve design without breaking it
  6. Unit tests provide high granularity bug detection reduces the cost

    of bugs pinpoint a bug to the method-level
  7. Unit tests provide a living documentation it is always up-to-date

    it compiles! makes knowledge sharing easier
  8. Unit tests are one of the basic foundation components that

    enables continuous integration and continuous delivery
  9. Why Should I Care Wrap-Up • Prove That Your Code

    Works • Ultra-Fast Feedback • Safety Net & Sense of Security • Super High Granularity • Living Documentation (always up-to-date) • Impose Good Design Principles • Written by one, everyone benefits • Basic block for CI and CD
  10. Different Frameworks • Visual Studio Testing Framework (not MSTest) •

    NUnit • MbUnit • ... http://bit.ly/list-of-dotnet-unit-testing-frameworks use one and you pretty much can use all
  11. The Unit Testing API • Way to mark code as

    test-code • Organize tests • Assertion library
  12. Mark Code As Tests and Organizing Tests • Use of

    C# Attributes • [TestClass] and [TestMethod] • [TestCategory(“awesome-tests”)] • [Ignore]
  13. Assertion Library • Let’s you assert correctness • Provides a

    lot of built-in assertions • Microsoft.VisualStudio.TestTools.UnitTesting http://bit.ly/using-the-assert-classes
  14. Basic Assertions • Static class Assert Assert.AreEqual<T>(T expected, T actual)

    Assert.AreNotEqual<T>(T noExpected, T actual) Assert.AreSame<T>(T expected, T actual) Assert.IsTrue(bool condition) Assert.IsFalse(bool condition) Assert.IsNull(object value) Assert.IsInstanceOfType(object value, Type expectedType)
  15. Assertions Also Available For • Collections AssertCollection • Strings AssertString

    CollectionAssert.AllItemsAreInstancesOfType(ICollection collection, Type type) CollectionAssert.AllItemsAreNotNull(ICollection collection) StringAssert.Contains(string value, string substring) StringAssert.StartsWith(string value, string substring)
  16. Using Visual Studio Testing Framework. Summary 1. Created a Test

    project 2. Created a Test class 3. Created a method to test Calculator.Add() 1. Used AAA (Arrange, Act, Assert) pattern 4. Created a method to test Calculator.Substract() 5. Used [TestInitialize] and [TestCleanup] to make our test suite more maintanable 6. Illustrated what happens when a bug is introduced
  17. Use Fake Objects to Isolate Class Under Text ensure that

    class under test is in complete isolation
  18. Mocking Framework • Makes it easy to create Fake Objects

    • Lots of Mocking Frameworks: – Moq – RhinoMocks – EasyMock.NET – Nmock – … http://bit.ly/what-csharp-mocking-framework-to-use
  19. How To Create Fake Objects With Moq Bang! and you

    have a fake object that implements the IScreen interface! var fakeScreen = new Mock<IScreen>();
  20. Using Moq. Summary 1. Install Moq using NuGet 2. Created

    two fake objects using Moq 3. Used fake objects to isolate the Calculator class
  21. General Heuristics • No logic in tests • Make sure

    the tests test only one thing • Separate unit tests from integration tests • Check test coverage by playing with values • Avoid code duplication
  22. General Heuristics II • Don’t abuse TestInitialize and TestCleanup •

    Make sure tests are isolated and repeatable • Most test should have one only assert • Only one mock per test • Use good naming conventions
  23. Disadvantages Of Unit Testing • None really, if well done

    – Write more Code – Maintain more code • If not well done, they have no value
  24. Test Driven Development • Write test before production code •

    Test drives design • Test-Implement-Refactor cycle – Red/Green/Refactor
  25. Test Driven Development Benefits • You are the first user

    of your code • Only write the code you need to pass a test • Implement testable code • High test coverage • Increases productivity (red-green-refactor) • Increases quality (refactor)
  26. Awesome Tooling • NCrunch and Mighty Moose • Tests run

    always This test passed visual code coverage Code hasn’t been run yet status
  27. References • The Art Of Unit Testing http://amzn.to/the-art-of-unit-testing • Coding

    Horror http://bit.ly/i-pity-the-fool-who-doesnt-write-unit-tests