Tdd & unit test

32
Test First Development Introduction to @GomesNayagam

description

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

Transcript of Tdd & unit test

Page 1: Tdd & unit test

Test First DevelopmentIntroduction to

@GomesNayagam

Page 2: Tdd & unit test

Introduction to Developer Testing

Unit Testing Frameworks

Test First Development

Page 3: Tdd & unit test

Higher quality

Fewer defects

Living documentation

Well Crafted code

Automatic regression harness

Benefits

Page 4: Tdd & unit test

A unit test confirms functionality of a small unit of functionality or

component in a larger system.

Page 5: Tdd & unit test

Unit Test Frameworks

Page 6: Tdd & unit test
Page 7: Tdd & unit test

How Unit Test Frameworks Work

My Code

Unit Test Runner EXE

My Unit Test LibUnit Test Common Lib

Page 8: Tdd & unit test

Test First Development

Page 9: Tdd & unit test

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

Page 10: Tdd & unit test

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

Page 11: Tdd & unit test

The Tests are the Specs

Page 12: Tdd & unit test

TFD: Writing Unit Tests

Page 13: Tdd & unit test

Test Organization

Testing the Sad Path

Integration vs. Unit Tests

Unit Test Lifecycle

Page 14: Tdd & unit test

Unit Test or Integration Test

Page 15: Tdd & unit test

Test Organization - xUnit

Page 16: Tdd & unit test

Test Lifecycle

TestFixtureSetup

SetUp

Test

TearDown

TestFixtureTearDown

nUnit

Page 17: Tdd & unit test

Setting Up A Test Project

Tests live in a separate class library project

Page 18: Tdd & unit test

A First Test

Using Nunit.Framework

Namespace Domain.Tests{

[TestFixture]public class FirstTestFixture{

[Test]public void AFirstTest(){

Assert.IsTrue(true,”true is true!”);}

}}

Page 19: Tdd & unit test

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

}

Page 20: Tdd & unit test

Isolating Code

Page 21: Tdd & unit test

Isolation Techniques

Test Doubles

Isolation by Example

Page 22: Tdd & unit test

Isolation Techniques

Test Method

SUT

Dependency Object

Test Object

Page 23: Tdd & unit test

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

Page 24: Tdd & unit test

Testing Double

Page 25: Tdd & unit test

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

Page 26: Tdd & unit test

public class StubRepo : IOwnerRepository {

public IOwner FindById(int id){}

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

} public void Delete(IOwner owner){}

}

Stub

Page 27: Tdd & unit test

Fake

Page 28: Tdd & unit test

Mock

TypeMock

RhinoMock

Moq

Page 29: Tdd & unit test

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.

Page 30: Tdd & unit test

// 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.

Page 31: Tdd & unit test

Write tests in a separate project

Treat test code with respect

Keep practicing and learning

Stick with red-green-refactor

Summery…

Page 32: Tdd & unit test

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