Tdd & unit test

Post on 11-May-2015

763 views 2 download

Tags:

description

About Test first design development and Test Driven Devlopment along with unit test framework(nUnit)

Transcript of Tdd & unit test

Test First DevelopmentIntroduction to

@GomesNayagam

Introduction to Developer Testing

Unit Testing Frameworks

Test First Development

Higher quality

Fewer defects

Living documentation

Well Crafted code

Automatic regression harness

Benefits

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

My Code

Unit Test Runner EXE

My Unit Test LibUnit Test Common Lib

Test First Development

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

TFD Details

For software, Jig is built just a moment before it is being used

Create a failing test Red

Make the test pass Green

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

RED

Refactor Green

The Tests are the Specs

TFD: Writing Unit Tests

Test Organization

Testing the Sad Path

Integration vs. Unit Tests

Unit Test Lifecycle

Unit Test or Integration Test

Test Organization - xUnit

Test Lifecycle

TestFixtureSetup

SetUp

Test

TearDown

TestFixtureTearDown

nUnit

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 - AssertionsUse 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 Object

Test Object

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

Faking out the SUT

Test Method

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);

public class StubRepo : IOwnerRepository {

public IOwner FindById(int id){}

public IOwner Save(IOwner owner) { return new Owner();

} public void Delete(IOwner owner){}

}

Stub

Fake

Mock

TypeMock

RhinoMock

Moq

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 productvar 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 repositoryvar productRepository = new Mock<IProductRepository>();productRepository.Expect(p => p.Get(1)).Returns(newProduct.Object)

// Actvar productReturned = productRepository.Object.Get(1);// AssertAssert.AreEqual("Bushmills", productReturned.Name);

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

e.g.

Write tests in a separate project

Treat test code with respect

Keep practicing and learning

Stick with red-green-refactor

Summery…

Test Driven Development by Example , Kent Beck, 2002

Reference

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