Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

xUnit

490 views

Published on

  • Be the first to comment

  • Be the first to like this

xUnit

  1. 1. your trusted software partner CMM www.itsoft.com.eg 1 info@itsoft.com.eg Level 3® Implementing Unit Testing Frameworks for .NET Prepared By: Mohamed El-Deeb Date: 24-06-2007
  2. 2. your trusted software partner CMM www.itsoft.com.eg 2 info@itsoft.com.eg Level 3®
  3. 3. your trusted software partner CMM www.itsoft.com.eg 3 info@itsoft.com.eg Level 3® SetUp
  4. 4. your trusted software partner CMM www.itsoft.com.eg 4 info@itsoft.com.eg Level 3® How //TearDown account.Balance = 0; [SetUp] public void Init() { //SetUp account = new Account(); account.Deposit(150); //Excercise account.Withdraw(50); //Verify Assert.AreEqual(account.Balance, 100); } [TearDown] public void Dispose() { } } [Test] public void TestWithdraw() { [TestFixture] public class AccountTest { Account account; }
  5. 5. your trusted software partner CMM www.itsoft.com.eg 5 info@itsoft.com.eg Level 3® How [TestFixture] public class AccountTest { [Test] public void TestWithdraw() { //SetUp Account source = new Account(); source.Deposit(200); Account destination = new Account(); destination.Deposit(150); //Excercise source.TransferFunds(destination, 100); //Verify Assert.AreEqual(source.Balance, 100); Assert.AreEqual(destination.Balance, 250); } }
  6. 6. your trusted software partner CMM www.itsoft.com.eg 6 info@itsoft.com.eg Level 3® Test Doubles
  7. 7. your trusted software partner CMM www.itsoft.com.eg 7 info@itsoft.com.eg Level 3® State verification [TestFixture] public class OrderStateTester { [Test] public void TestOrderWithInvFound() { //setup - data Warehouse warehouse = new Warehouse(); warehouse.Add("foo", 150); Order order = new Order("foo", 50); //setup - expectations int expected = 150 - 50; // 100 //exercise order.FillFrom(warehouse); //verify Assert.AreEqual(warehouse.GetInventory("foo“), expected); } } Behavior Verification [TestFixture] public class OrderInteractionTester { Mockery mocks = new Mockery(); [Test] public void TestOrderWithInvFound() { //setup - data Warehouse warehouse = (Warehouse)mocks.NewMock(typeof(Warehouse)); Order order = new Order("foo", 50); //setup - expectations using (mocks.Ordered) { Expect.Once.On(warehouse) .Method("hasInventory") .With("foo", 50) .Will(Return.Value(true)); Expect.Once.On(warehouse) .Method("Remove") .With("foo", 50); } //exercise order.FillFrom(warehouse); //verify mocks.VerifyAllExpectationsHaveBeenMet(); } }
  8. 8. your trusted software partner CMM www.itsoft.com.eg 8 info@itsoft.com.eg Level 3® Exercise
  9. 9. your trusted software partner CMM www.itsoft.com.eg 9 info@itsoft.com.eg Level 3® Verify PC-COF  Partial runs are possible  Consistent results on every test run  Configuration is unneeded before run  Order of tests does not matter  Fast run time
  10. 10. your trusted software partner CMM www.itsoft.com.eg 10 info@itsoft.com.eg Level 3® Verify PC-COF  Partial runs are possible  Consistent results on every test run  Order of tests does not matter  Fast run time  Configuration is unneeded before run
  11. 11. your trusted software partner CMM www.itsoft.com.eg 11 info@itsoft.com.eg Level 3® Verify  Don't inherit from classes you can't control. Encapsulate and wrap it up.  Make methods virtual by default.  Don't use 'Sealed' unless you really have to.  Add a setter to the singleton instance.  Make sure a singleton always return an interface rather than a concrete class.  Use internal keyword to hide setters from production code, but visible to test code.  If possible, create an interface per class. You never know when you're gonna need it.
  12. 12. your trusted software partner CMM www.itsoft.com.eg 12 info@itsoft.com.eg Level 3® Teardown
  13. 13. your trusted software partner CMM www.itsoft.com.eg 13 info@itsoft.com.eg Level 3® Teardown

×