Tdd & unit test
Upcoming SlideShare
Loading in...5
×
 

Tdd & unit test

on

  • 1,030 views

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

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

Statistics

Views

Total Views
1,030
Views on SlideShare
1,030
Embed Views
0

Actions

Likes
0
Downloads
43
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Tdd & unit test Tdd & unit test Presentation Transcript

  • Introduction toTest First Development @GomesNayagam
  • Introduction to Developer Testing Unit Testing FrameworksTest First Development
  • Benefits Higher quality Fewer defects Living documentation Well Crafted code Automatic regression harness View slide
  • A unit test confirms functionalityof a small unit of functionality or component in a larger system. View slide
  • Unit Test Frameworks
  • How Unit Test Frameworks Work Unit Test Runner EXE Unit TestMy Code My Unit Test Lib Common Lib
  • Test First Development
  • TFD DetailsA Jig is a form upon whichsomething else is builtThe jig is the specification forthe thing being builtRemove the jig when ready toLaunchFor 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 behaviorRefactor Green
  • The Tests are the Specs
  • TFD: Writing Unit Tests
  • Test Integration vs.Organization Unit TestsTesting the Sad Unit TestPath Lifecycle
  • Unit Test or Integration Test
  • Test Organization - xUnit
  • Test Lifecycle nUnit TestFixtureSetup SetUp Test TearDown TestFixtureTearDown
  • Setting Up A Test ProjectTests live in a separate class library project
  • A First TestUsing Nunit.FrameworkNamespace 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 TechniquesTest DoublesIsolation by Example
  • Isolation Techniques Test Method SUT Dependency Test Object Object
  • Faking out the SUT Test Method1.Create test specific objects2.Create the SUT (using Interface)3. Invoke operation on the SUT4.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 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);
  • Summery… Stick with red -Write tests in a greenseparate -project refactor KeepTreat test code practicing andwith respect learning
  • ReferenceTest Driven Development by Example , Kent Beck, 2002http://code.google.com/p/moq/wiki/QuickStarthttp://stephenwalther.com/blog/archive/2008/06/12/tdd-introduction-to-moq.aspxSource: Pluralsight,Google,WIKI