Introduction to
Test First Development
               @GomesNayagam
Introduction to Developer Testing


                                    Unit Testing Frameworks



Test First Development
Benefits

 Higher quality

 Fewer defects

 Living documentation

 Well Crafted code

 Automatic regression harness
A unit test confirms functionality
of a small unit of functionality or
            component
        in a larger system.
Unit Test Frameworks
How Unit Test Frameworks Work



                               Unit Test Runner
                               EXE




                                             Unit Test
My Code         My Unit Test Lib             Common Lib
Test First Development
TFD        Details

A Jig is a form upon which
something else is built

The jig is the specification for
the thing being built

Remove the jig when ready to
Launch




For software, Jig is built just a moment before it is being used
Create a failing test   Red

                    Make the test pass      Green


           RED      Refactor     Improve the internal
                                 implementation without
                                 changing the external
                                 contract or behavior




Refactor         Green
The Tests are the Specs
TFD: Writing Unit Tests
Test              Integration vs.
Organization      Unit Tests



Testing the Sad   Unit Test
Path              Lifecycle
Unit Test or Integration Test
Test Organization - xUnit
Test Lifecycle nUnit


                       TestFixtureSetup


                       SetUp


                       Test


                       TearDown


                       TestFixtureTearDown
Setting Up A Test Project
Tests live in a separate class library project
A First Test


Using Nunit.Framework

Namespace Domain.Tests
{
       [TestFixture]
       public class FirstTestFixture
       {
                 [Test]
                 public void AFirstTest()
                 {
                             Assert.IsTrue(true,”true is true!”);
                 }
       }
}
TFD - Assertions
Use one logical assertion per test

                                     [Test]
                                     public void the_order_is_canceled()
                                     {
                                               var customer = CreateCustomer();
                                               Assert.IsNotNull(customer);

                                              customer.PlaceOrder();
                                              Assert.IsTrue(customer.HasOrder);

                                              customer.CancelOrder();
                                              Assert.IsFalse(customer.HasOrder);
                                     }
Isolating Code
Isolation Techniques




Test Doubles




Isolation by Example
Isolation Techniques

                         Test Method



              SUT




            Dependency                 Test Object
              Object
Faking out the SUT
                                       Test Method

1.Create test specific objects

2.Create the SUT (using Interface)

3. Invoke operation on the SUT

4.Check results of SUT invocation On the SUT
Testing Double
Dummy
        var person = new Person();

        person.First = “Homer”;

        person.Last = “Simpson”;

        Assert.IsNotNull(person.FullName);


                                     var order = new Order();

                                     order.AddLineItem(12, 1);

                                     order.AddLineItem.Add(21, 3);

                                     Assert.AreEqual(2, order.NumLineItems);
Stub

       public class StubRepo : IOwnerRepository {

              public IOwner FindById(int id){}

              public IOwner Save(IOwner owner) {
                      return new Owner();
              }
              public void Delete(IOwner owner){}
       }
Fake
Mock


 TypeMock

 RhinoMock

 Moq
e.g.

using System;
using System.Collections.Generic;

 namespace MoqSamples.Models
{
    public interface IProductRepository
     {
        List<IProduct> Select();
        IProduct Get(int id);
     }
    public interface IProduct
    {
       int Id {get; set;}
       string Name { get; set; }
    }
}
 // Mock a product
var newProduct = new Mock<IProduct>();
newProduct.ExpectGet(p => p.Id).Returns(1);
newProduct.ExpectGet(p => p.Name).Returns("Bushmills");

Assert.AreEqual("Bushmills", newProduct.Object.Name);
e.g.
// Mock product repository
var productRepository = new Mock<IProductRepository>();
productRepository.Expect(p => p.Get(1)).Returns(newProduct.Object)

// Act
var productReturned = productRepository.Object.Get(1);
// Assert
Assert.AreEqual("Bushmills", productReturned.Name);

// Mock product repository
var productRepository = new Mock<IProductRepository>();
productRepository
.Expect(p => p.Get(It.IsAny<int>()))
.Returns(newProduct.Object);
Summery…

                   Stick with red
                   -
Write tests in a   green
separate           -
project            refactor



                   Keep
Treat test code    practicing and
with respect       learning
Reference


Test Driven Development by Example , Kent Beck, 2002

http://code.google.com/p/moq/wiki/QuickStart

http://stephenwalther.com/blog/archive/2008/06/12/tdd-
introduction-to-moq.aspx



Source: Pluralsight,Google,WIKI

Tdd & unit test

  • 1.
    Introduction to Test FirstDevelopment @GomesNayagam
  • 2.
    Introduction to DeveloperTesting Unit Testing Frameworks Test First Development
  • 3.
    Benefits  Higher quality Fewer defects  Living documentation  Well Crafted code  Automatic regression harness
  • 4.
    A unit testconfirms functionality of a small unit of functionality or component in a larger system.
  • 5.
  • 7.
    How Unit TestFrameworks Work Unit Test Runner EXE Unit Test My Code My Unit Test Lib Common Lib
  • 8.
  • 9.
    TFD Details A Jig is a form upon which something else is built The jig is the specification for the thing being built Remove the jig when ready to Launch For software, Jig is built just a moment before it is being used
  • 10.
    Create a failingtest Red Make the test pass Green RED Refactor Improve the internal implementation without changing the external contract or behavior Refactor Green
  • 11.
    The Tests arethe Specs
  • 12.
  • 13.
    Test Integration vs. Organization Unit Tests Testing the Sad Unit Test Path Lifecycle
  • 14.
    Unit Test orIntegration Test
  • 15.
  • 16.
    Test Lifecycle nUnit TestFixtureSetup SetUp Test TearDown TestFixtureTearDown
  • 17.
    Setting Up ATest Project Tests live in a separate class library project
  • 18.
    A First Test UsingNunit.Framework Namespace Domain.Tests { [TestFixture] public class FirstTestFixture { [Test] public void AFirstTest() { Assert.IsTrue(true,”true is true!”); } } }
  • 19.
    TFD - Assertions Useone logical assertion per test [Test] public void the_order_is_canceled() { var customer = CreateCustomer(); Assert.IsNotNull(customer); customer.PlaceOrder(); Assert.IsTrue(customer.HasOrder); customer.CancelOrder(); Assert.IsFalse(customer.HasOrder); }
  • 20.
  • 21.
  • 22.
    Isolation Techniques Test Method SUT Dependency Test Object Object
  • 23.
    Faking out theSUT Test Method 1.Create test specific objects 2.Create the SUT (using Interface) 3. Invoke operation on the SUT 4.Check results of SUT invocation On the SUT
  • 24.
  • 25.
    Dummy var person = new Person(); person.First = “Homer”; person.Last = “Simpson”; Assert.IsNotNull(person.FullName); var order = new Order(); order.AddLineItem(12, 1); order.AddLineItem.Add(21, 3); Assert.AreEqual(2, order.NumLineItems);
  • 26.
    Stub public class StubRepo : IOwnerRepository { public IOwner FindById(int id){} public IOwner Save(IOwner owner) { return new Owner(); } public void Delete(IOwner owner){} }
  • 27.
  • 28.
  • 29.
    e.g. using System; using System.Collections.Generic; namespace MoqSamples.Models { public interface IProductRepository { List<IProduct> Select(); IProduct Get(int id); } public interface IProduct { int Id {get; set;} string Name { get; set; } } } // Mock a product var newProduct = new Mock<IProduct>(); newProduct.ExpectGet(p => p.Id).Returns(1); newProduct.ExpectGet(p => p.Name).Returns("Bushmills"); Assert.AreEqual("Bushmills", newProduct.Object.Name);
  • 30.
    e.g. // Mock productrepository var productRepository = new Mock<IProductRepository>(); productRepository.Expect(p => p.Get(1)).Returns(newProduct.Object) // Act var productReturned = productRepository.Object.Get(1); // Assert Assert.AreEqual("Bushmills", productReturned.Name); // Mock product repository var productRepository = new Mock<IProductRepository>(); productRepository .Expect(p => p.Get(It.IsAny<int>())) .Returns(newProduct.Object);
  • 31.
    Summery… Stick with red - Write tests in a green separate - project refactor Keep Treat test code practicing and with respect learning
  • 32.
    Reference Test Driven Developmentby Example , Kent Beck, 2002 http://code.google.com/p/moq/wiki/QuickStart http://stephenwalther.com/blog/archive/2008/06/12/tdd- introduction-to-moq.aspx Source: Pluralsight,Google,WIKI