$30 off During Our Annual Pro Sale. View Details »

Introduction to Unit Testing

Jaime
July 23, 2012

Introduction to Unit Testing

Basic introduction to unit testing.

Jaime

July 23, 2012
Tweet

More Decks by Jaime

Other Decks in Programming

Transcript

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

    View Slide

  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

    View Slide

  3. Context

    View Slide

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

    View Slide

  5. View Slide

  6. Unit Testing in Software
    • Functional Tests
    – Unit Tests
    – Integration Tests
    – System Tests
    – System Integration Tests
    • Non Functional Tests
    – Security, Performance, Usability…

    View Slide

  7. What are Unit Tests

    View Slide

  8. Integration Test
    Unit Test
    Tests that Test Units Of Code
    in Isolation
    low level

    View Slide

  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

    View Slide

  10. Trustworthy
    Readable
    Maintainable

    View Slide

  11. A Simple Unit Test

    View Slide

  12. A Simple Unit Test

    View Slide

  13. Unit Tests. Summary
    • Test Units of Code in Isolation
    • Automated and Repeatable
    • Easy to Implement and Run
    • Run Quickly
    • Trustworthy, Readable and Maintainable

    View Slide

  14. Why Should I Care

    View Slide

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

    View Slide

  16. Unit tests provide ultra-fast feedback
    demonstrate concrete progress
    increase productivity
    keeps you in the zone

    View Slide

  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

    View Slide

  18. Unit tests provide
    high granularity bug detection
    reduces the cost of bugs
    pinpoint a bug to the method-level

    View Slide

  19. Unit tests provide
    a living documentation
    it is always up-to-date
    it compiles!
    makes knowledge sharing easier

    View Slide

  20. Unit Tests impose
    good design principles

    View Slide

  21. Written by one, everyone benefits

    View Slide

  22. Unit tests are one of the basic
    foundation components that enables
    continuous integration and
    continuous delivery

    View Slide

  23. View Slide

  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

    View Slide

  25. How Does Unit Testing Work

    View Slide

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

    View Slide

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

    View Slide

  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

    View Slide

  29. The Unit Testing API
    • Way to mark code as test-code
    • Organize tests
    • Assertion library

    View Slide

  30. Mark Code As Tests
    and Organizing Tests
    • Use of C# Attributes
    • [TestClass] and [TestMethod]
    • [TestCategory(“awesome-tests”)]
    • [Ignore]

    View Slide

  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

    View Slide

  32. Basic Assertions
    • Static class Assert
    Assert.AreEqual(T expected, T actual)
    Assert.AreNotEqual(T noExpected, T actual)
    Assert.AreSame(T expected, T actual)
    Assert.IsTrue(bool condition)
    Assert.IsFalse(bool condition)
    Assert.IsNull(object value)
    Assert.IsInstanceOfType(object value, Type expectedType)

    View Slide

  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)

    View Slide

  34. Asserting Exceptions
    • [ExpectedException(typeof(MyException))]

    View Slide

  35. The Unit Test Runner
    • Runs Tests
    • Provides feedback
    • Red/Yellow/Green

    View Slide

  36. DEMO
    Using VS Testing Framework

    View Slide

  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

    View Slide

  38. Mocks And Stubs
    Testing in Isolation

    View Slide

  39. What happens when a class has
    dependencies?

    View Slide

  40. Imagine...

    View Slide

  41. Use Fake Objects
    to Isolate Class Under Text
    ensure that class under
    test is in complete
    isolation

    View Slide

  42. What are Fake Objects
    crash test dummy simile

    View Slide

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

    View Slide

  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

    View Slide

  45. How To Create Fake Objects
    With Moq
    Bang!
    and you have a fake object that implements the
    IScreen interface!
    var fakeScreen = new Mock();

    View Slide

  46. DEMO
    Using Moq

    View Slide

  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

    View Slide

  48. Testing Best Practices
    • Naming Conventions
    • AAA (Arrange Act Assert pattern)
    • Heuristics

    View Slide

  49. Testing Naming Conventions

    View Slide

  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

    View Slide

  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

    View Slide

  52. Disadvantages Of Unit Testing
    • None really, if well done
    – Write more Code
    – Maintain more code
    • If not well done, they have no value

    View Slide

  53. Treat your tests as
    if they were production code

    View Slide

  54. EXERCISE
    UNIT TESTING IN ACTION

    View Slide

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

    View Slide

  56. Test Driven Development
    • Write test before production code
    • Test drives design
    • Test-Implement-Refactor cycle
    – Red/Green/Refactor

    View Slide

  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)

    View Slide

  58. Behavior Driven Development

    View Slide

  59. Awesome Tooling
    • NCrunch and Mighty Moose
    • Tests run always
    This test passed
    visual code coverage
    Code hasn’t
    been run yet
    status

    View Slide

  60. And Remember...

    View Slide

  61. If you want to know more...

    View Slide

  62. If you want to know more...

    View Slide

  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

    View Slide

  64. Thank you

    View Slide