Professional Documents
Culture Documents
• From Wikipedia:
– a software verification and validation method in which a
programmer tests that individual units of source code are fit for
use
– a unit is the smallest testable part of an application
– in object-oriented programming, the smallest unit is a class,
which may belong to a base/super class, abstract class or
derived/child class
• Simple
– A programmed check of the result against the
expected result
• Independent
– does not depend on other tests
• Fast
– Can be run over and over in less than 1/100 of a
second
– Run tests frequently to immediately catch coding
errors
• A client of the public interface
– a test verifies the externally observable behavior of
the class under test
What is a good unit test?
(Osherove)
• Error localization
– Hard to determine what a test failure means
– Can take considerable work to pinpoint source of test
fail
• Execution time
– Larger tests take longer to execute
– Tests that take too long are not run frequently enough
• Coverage
– Hard to directly correlate large tests to class methods
– When new code is added, hard to modify large tests
to exercise the new code
Example of the problem with large functional tests
•Gamehand.Value
function receives the
strategy object that tells MockEvaluator
it how to calculate the Value(GameHand)
value
•It “calls back” into that
object to use the
valuation logic
BridgeHandEvaluator PokerHandEvaluator
•GameHand class is
decoupled from the Value(GameHand) Value(GameHand)
algorithm for calculating
the value
Adapter Pattern example
[Test]
[Description("Confirm InitializeAllResourceItems method returns false when the
IMapResourceManagerAdapter.InitializeMapResourceManager method fails")]
public void InitializeAllResourceItems_Returns_False_When_InitializeMapResourceManager_Fails()
{
IMapResourceItemFactory mriDummy = MockRepository.GenerateStub<IMapResourceItemFactory>();
ErrorLoggerMock logStub = new ErrorLoggerMock();
IMapResourceManagerAdapter mrmStub = MockRepository.GenerateStub<IMapResourceManagerAdapter>();
mrmStub.Expect(mrm => mrm.InitializeMapResourceManager()).Return(false);
[TestCase(0, 4)]
[TestCase(1, 1)]
[Description("Confirm InitializeAllResourceItems method correctly add/removes MapResourceItems in the MapResourceManager when there
are Factory failures")]
public void
InitializeAllResourceItems_Correctly_Removes_MapResourceItems_From_MapResourceManager_For_IMapResourceItemFactory_Failures(int
failScenario, int failCount)
{
IErrorLogger logDummy = MockRepository.GenerateStub<IErrorLogger>();
MapResourceManagerAdapterMock mrmMock = new MapResourceManagerAdapterMock();
MapResourceItemFactoryMock mriMock = new MapResourceItemFactoryMock((MapResourceItemFactoryMock.FailScenario)failScenario);
MapResourceManagerInitializer obj = new MapResourceManagerInitializer(mrmMock, mriMock, logDummy);
obj.InitializeAllResourceItems(_lyrPropCollAll);
Assert.That(mrmMock.ResourceItemCount,
Is.EqualTo(_lyrPropCollAll.Count - failCount)); MapResourceManager mock counts
} the number of resources added
Rhino Mocks Example from Documentation
(http://ayende.com/Wiki/Rhino+Mocks+Ordered+and+Unordered.ashx)
[Test]
public void MovingFundsUsingTransactions()
{
MockRepository mocks = new MockRepository();
IDatabaseManager databaseManager = mocks.StrictMock<IDatabaseManager>();
IBankAccount accountOne = mocks.StrictMock<IBackAccount>(),
accountTwo = mocks.StrictMock<IBackAccount>();
using(mocks.Ordered()) This code verifies that the
{ transfer of funds from one
Expect.Call(databaseManager.BeginTransaction()) account to another is
.Return(databaseManager); wrapped in a transaction
using(mocks.Unordered())
{ The implementation is free
Expect.Call(accountOne.Withdraw(1000)); to withdraw from the first
Expect.Call(accountTwo.Deposit(1000)); account first, or to deposit
} into the second account
Expect.Call(databaseManager.Dispose()); first, both are legal, as long
} as both actions happen
mocks.ReplayAll();
Bank bank = new Bank(databaseManager);
bank.TransferFunds(accountOne,accountTwo,1000);
mocks.VerifyAll();
}
Tools
Feathers, Michael. Working Effectively with Legacy Code. New Jersey: Prentice Hall PTR, 2005.
Osherove, Roy. The Art of Unit Testing with Examples in .NET. Connecticut: Manning
Publications Co, 2009.
Tool References
NUnit
– http://www.nunit.org
FxCop
– http://msdn.microsoft.com/en-
us/library/bb429476(VS.80).aspx (MSDN description)
– http://www.microsoft.com/downloads/details.aspx?fa
milyid=9AEAA970-F281-4FB0-ABA1-
D59D7ED09772&displaylang=en (download)
– http://www.codeplex.com (custom rules)