Tdd & unit test

  • View

  • Download

Embed Size (px)


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

Text of Tdd & unit test

  • 1.Introduction toTest First Development @GomesNayagam

2. Introduction to Developer TestingUnit 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 orcomponentin 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. TFDDetailsA 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 RedMake the test passGreen REDRefactor Improve the internal implementation without changing the external contract or behaviorRefactor Green 10. The Tests are the Specs 11. TFD: Writing Unit Tests 12. TestIntegration vs.OrganizationUnit TestsTesting the Sad Unit TestPathLifecycle 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 MethodSUTDependency Test ObjectObject 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. Dummyvar 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 Select();IProduct Get(int id); }public interface IProduct{ int Id {get; set;} string Name { get; set; }}} // Mock a productvar newProduct = new Mock();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();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();productRepository.Expect(p => p.Get(It.IsAny())).Returns(newProduct.Object); 30. Summery Stick with red -Write tests in a greenseparate -projectrefactor KeepTreat test codepracticing andwith respect learning 31. ReferenceTest Driven Development by Example , Kent Beck, 2002 Pluralsight,Google,WIKI