Tdd & unit test

970 views
901 views

Published on

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

Published in: Technology, Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
970
On SlideShare
0
From Embeds
0
Number of Embeds
9
Actions
Shares
0
Downloads
47
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Tdd & unit test

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

×