Unit Testing in Software • Functional Tests – Unit Tests – Integration Tests – System Tests – System Integration Tests • Non Functional Tests – Security, Performance, Usability…
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
Unit Tests. Summary • Test Units of Code in Isolation • Automated and Repeatable • Easy to Implement and Run • Run Quickly • Trustworthy, Readable and Maintainable
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
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
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
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
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
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
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
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)