xunit

13
your trusted software partner CMM www.itsoft.com.eg 1 [email protected] Level 3 ® Implementing Unit Testing Frameworks for .NET Prepared By: Mohamed El-Deeb Date: 24-06-2007

Upload: mohamed-el-deeb

Post on 16-Jul-2015

66 views

Category:

Documents


0 download

TRANSCRIPT

your trusted software partner

CMM

www.itsoft.com.eg

[email protected]

Level 3®

ImplementingUnit Testing Frameworks for .NET

Prepared By: Mohamed El-Deeb

Date: 24-06-2007

your trusted software partner

CMM

www.itsoft.com.eg

[email protected]

Level 3®

your trusted software partner

CMM

www.itsoft.com.eg

[email protected]

Level 3®

SetUp

your trusted software partner

CMM

www.itsoft.com.eg

[email protected]

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;

}

your trusted software partner

CMM

www.itsoft.com.eg

[email protected]

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);

}

}

your trusted software partner

CMM

www.itsoft.com.eg

[email protected]

Level 3®

Test Doubles

your trusted software partner

CMM

www.itsoft.com.eg

[email protected]

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();

}

}

your trusted software partner

CMM

www.itsoft.com.eg

[email protected]

Level 3®

Exercise

your trusted software partner

CMM

www.itsoft.com.eg

[email protected]

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

your trusted software partner

CMM

www.itsoft.com.eg

[email protected]

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

your trusted software partner

CMM

www.itsoft.com.eg

[email protected]

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.

your trusted software partner

CMM

www.itsoft.com.eg

[email protected]

Level 3®

Teardown

your trusted software partner

CMM

www.itsoft.com.eg

[email protected]

Level 3®

Teardown