Slide 1

Slide 1 text

P2P Code Academy Introduction to Unit Testing 27th June 2012

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

Context

Slide 4

Slide 4 text

We All Have Tested Before • Manual tests • Least efficient

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

What are Unit Tests

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

Trustworthy Readable Maintainable

Slide 11

Slide 11 text

A Simple Unit Test

Slide 12

Slide 12 text

A Simple Unit Test

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

Why Should I Care

Slide 15

Slide 15 text

Unit tests prove that your code actually works rnsure correctness works

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

Unit Tests impose good design principles

Slide 21

Slide 21 text

Written by one, everyone benefits

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

No content

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

How Does Unit Testing Work

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

The Unit Test Framework • Unit testing API • Test Runner

Slide 28

Slide 28 text

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

Slide 29

Slide 29 text

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

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

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

Slide 32

Slide 32 text

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)

Slide 33

Slide 33 text

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)

Slide 34

Slide 34 text

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

Slide 35

Slide 35 text

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

Slide 36

Slide 36 text

DEMO Using VS Testing Framework

Slide 37

Slide 37 text

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

Slide 38

Slide 38 text

Mocks And Stubs Testing in Isolation

Slide 39

Slide 39 text

What happens when a class has dependencies?

Slide 40

Slide 40 text

Imagine...

Slide 41

Slide 41 text

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

Slide 42

Slide 42 text

What are Fake Objects crash test dummy simile

Slide 43

Slide 43 text

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

Slide 44

Slide 44 text

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

Slide 45

Slide 45 text

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

Slide 46

Slide 46 text

DEMO Using Moq

Slide 47

Slide 47 text

Using Moq. Summary 1. Install Moq using NuGet 2. Created two fake objects using Moq 3. Used fake objects to isolate the Calculator class

Slide 48

Slide 48 text

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

Slide 49

Slide 49 text

Testing Naming Conventions

Slide 50

Slide 50 text

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

Slide 51

Slide 51 text

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

Slide 52

Slide 52 text

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

Slide 53

Slide 53 text

Treat your tests as if they were production code

Slide 54

Slide 54 text

EXERCISE UNIT TESTING IN ACTION

Slide 55

Slide 55 text

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

Slide 56

Slide 56 text

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

Slide 57

Slide 57 text

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)

Slide 58

Slide 58 text

Behavior Driven Development

Slide 59

Slide 59 text

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

Slide 60

Slide 60 text

And Remember...

Slide 61

Slide 61 text

If you want to know more...

Slide 62

Slide 62 text

If you want to know more...

Slide 63

Slide 63 text

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

Slide 64

Slide 64 text

Thank you