Introduction to Unit Testing

1dfad8107453e0f493ff1a6a8e48d503?s=47 Jaime
July 23, 2012

Introduction to Unit Testing

Basic introduction to unit testing.

1dfad8107453e0f493ff1a6a8e48d503?s=128

Jaime

July 23, 2012
Tweet

Transcript

  1. P2P Code Academy Introduction to Unit Testing 27th June 2012

  2. 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
  3. Context

  4. We All Have Tested Before • Manual tests • Least

    efficient
  5. None
  6. Unit Testing in Software • Functional Tests – Unit Tests

    – Integration Tests – System Tests – System Integration Tests • Non Functional Tests – Security, Performance, Usability…
  7. What are Unit Tests

  8. Integration Test Unit Test Tests that Test Units Of Code

    in Isolation low level
  9. 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
  10. Trustworthy Readable Maintainable

  11. A Simple Unit Test

  12. A Simple Unit Test

  13. Unit Tests. Summary • Test Units of Code in Isolation

    • Automated and Repeatable • Easy to Implement and Run • Run Quickly • Trustworthy, Readable and Maintainable
  14. Why Should I Care

  15. Unit tests prove that your code actually works rnsure correctness

    works
  16. Unit tests provide ultra-fast feedback demonstrate concrete progress increase productivity

    keeps you in the zone
  17. 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
  18. Unit tests provide high granularity bug detection reduces the cost

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

    it compiles! makes knowledge sharing easier
  20. Unit Tests impose good design principles

  21. Written by one, everyone benefits

  22. Unit tests are one of the basic foundation components that

    enables continuous integration and continuous delivery
  23. None
  24. 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
  25. How Does Unit Testing Work

  26. All You Need Is Love • Unit Test Framework •

    Mocking Framework
  27. The Unit Test Framework • Unit testing API • Test

    Runner
  28. 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
  29. The Unit Testing API • Way to mark code as

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

    C# Attributes • [TestClass] and [TestMethod] • [TestCategory(“awesome-tests”)] • [Ignore]
  31. 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
  32. 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)
  33. 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)
  34. Asserting Exceptions • [ExpectedException(typeof(MyException))]

  35. The Unit Test Runner • Runs Tests • Provides feedback

    • Red/Yellow/Green
  36. DEMO Using VS Testing Framework

  37. 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
  38. Mocks And Stubs Testing in Isolation

  39. What happens when a class has dependencies?

  40. Imagine...

  41. Use Fake Objects to Isolate Class Under Text ensure that

    class under test is in complete isolation
  42. What are Fake Objects crash test dummy simile

  43. Two Types of Fake Objects • Stubs (passive testing) •

    Mocks (active testing)
  44. 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
  45. How To Create Fake Objects With Moq Bang! and you

    have a fake object that implements the IScreen interface! var fakeScreen = new Mock<IScreen>();
  46. DEMO Using Moq

  47. Using Moq. Summary 1. Install Moq using NuGet 2. Created

    two fake objects using Moq 3. Used fake objects to isolate the Calculator class
  48. Testing Best Practices • Naming Conventions • AAA (Arrange Act

    Assert pattern) • Heuristics
  49. Testing Naming Conventions

  50. 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
  51. 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
  52. Disadvantages Of Unit Testing • None really, if well done

    – Write more Code – Maintain more code • If not well done, they have no value
  53. Treat your tests as if they were production code

  54. EXERCISE UNIT TESTING IN ACTION

  55. Extra Goodies • Test Driven Development • Behavior Driven Development

    • Awesome Tools
  56. Test Driven Development • Write test before production code •

    Test drives design • Test-Implement-Refactor cycle – Red/Green/Refactor
  57. 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)
  58. Behavior Driven Development

  59. Awesome Tooling • NCrunch and Mighty Moose • Tests run

    always This test passed visual code coverage Code hasn’t been run yet status
  60. And Remember...

  61. If you want to know more...

  62. If you want to know more...

  63. 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
  64. Thank you