Your SlideShare is downloading. ×
Tdd & unit test
Upcoming SlideShare
Loading in...5

Thanks for flagging this SlideShare!

Oops! An error has occurred.

Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Tdd & unit test


Published on

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)

Published in: Technology, Education
  • Be the first to comment

  • Be the first to like this

No Downloads
Total Views
On Slideshare
From Embeds
Number of Embeds
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

No notes for slide


  • 1. Introduction toTest First Development @GomesNayagam
  • 2. Introduction to Developer Testing Unit Testing FrameworksTest First Development
  • 3. Benefits Higher quality Fewer defects Living documentation Well Crafted code Automatic regression harness
  • 4. A unit test confirms functionalityof a small unit of functionality or component in a larger system.
  • 5. Unit Test Frameworks
  • 6. How Unit Test Frameworks Work Unit Test Runner EXE Unit TestMy Code My Unit Test Lib Common Lib
  • 7. Test First Development
  • 8. 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
  • 9. Create a failing test Red Make the test pass Green RED Refactor Improve the internal implementation without changing the external contract or behaviorRefactor Green
  • 10. The Tests are the Specs
  • 11. TFD: Writing Unit Tests
  • 12. Test Integration vs.Organization Unit TestsTesting the Sad Unit TestPath Lifecycle
  • 13. Unit Test or Integration Test
  • 14. Test Organization - xUnit
  • 15. Test Lifecycle nUnit TestFixtureSetup SetUp Test TearDown TestFixtureTearDown
  • 16. Setting Up A Test ProjectTests live in a separate class library project
  • 17. A First TestUsing Nunit.FrameworkNamespace Domain.Tests{ [TestFixture] public class FirstTestFixture { [Test] public void AFirstTest() { Assert.IsTrue(true,”true is true!”); } }}
  • 18. 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); }
  • 19. Isolating Code
  • 20. Isolation TechniquesTest DoublesIsolation by Example
  • 21. Isolation Techniques Test Method SUT Dependency Test Object Object
  • 22. 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
  • 23. Testing Double
  • 24. 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);
  • 25. Stub public class StubRepo : IOwnerRepository { public IOwner FindById(int id){} public IOwner Save(IOwner owner) { return new Owner(); } public void Delete(IOwner owner){} }
  • 26. Fake
  • 27. Mock TypeMock RhinoMock Moq
  • 28. 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);
  • 29. 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);
  • 30. Summery… Stick with red -Write tests in a greenseparate -project refactor KeepTreat test code practicing andwith respect learning
  • 31. ReferenceTest Driven Development by Example , Kent Beck, 2002 Pluralsight,Google,WIKI