What Is Unit test?
A unit test verifies that a function or set of functions meet the requirements. Unit tests inspect both black boxes or white boxes. NUnitis a unit-testing framework for all .Net languages.
Here is an example. Let’s take a look this class:
namespace Bank
{
public class Account
{
private float balance;
public void Deposit(float amount)
{
balance += amount;
}
public void Withdraw(float amount)
{
balance -= amount;
}
public void TransferFunds(Account destination, float amount)
{
destination.Deposit(amount);
Withdraw(amount);
}
public float Balance
{
get { return balance; }
}
}
}
for this class we make corresponding unit test class like this:
using NUnit.Framework;
using Bank;
namespace Test
{
[TestFixture]
public class AccountTests
{
[Test]
public void TestTransferFunds()
{
Account source = new Account();
source.Deposit(200.00F);
Account destination = new Account();
destination.Deposit(150.00F);
source.TransferFunds(destination, 100.00F);
//Here are assertions
//we check actual value with expected value
Assert.AreEqual(250.00F, destination.Balance);
Assert.AreEqual(100.00F, source.Balance);
}
}
}
Assertions are central part of unit testing in any of the xUnit frameworks. NUnit provides a rich set of assertions as static methods of the Assert class.
Example:
Assert.AreEqual( int expected, int actual );
There are:
- StringAssert
- CollectionAssert
- FileAssert
From version 2.0 on, NUnit has used custom attributes.
All NUnit attributes are contained in the NUnit.Framework namespace. Each source file that contains tests must include a using statement for that namespace and the project must reference the framework assembly, nunit.framework.dll.
SetUpFixture Attribute
This is the attribute that marks a class that contains the one-time setup or teardown methods for all the test fixtures under a given namespace. The class may contain at most one method marked with the SetUpAttribute and one method marked with the TearDownAttribute.
Example:
[SetUpFixture]
public class MySetUpClass
{
[SetUp]
RunBeforeAnyTests()
{
// ...
}
[TearDown]
RunAfterAnyTests()
{
// ...
}
}
Test Attribute (NUnit 2.0)
The Test attribute marks a specific method inside a class that has already been marked as a TestFixture, as a test method. The signature for a test method is defined as follows:
public void MethodName()
Example:
[Test]
public void Add()
{
/* ... */
}
SetUpAttribute
This attribute is used inside a TestFixture to provide a common set of functions that are performed just before each test method is called. A TestFixture can have only one SetUp method. If more than one is defined the TestFixture will compile successfully, but its tests will not run.
Example:
[TestFixture]
public class SuccessTests
{
[SetUp]
public void Init()
{
/* ... */
}
}
TearDownAttribute
This attribute is used inside a TestFixture to provide a common set of functions that are performed after each test method is run. A TestFixture can have only one TearDown method. If more than one is defined the TestFixture will compile successfully, but its tests will not run.
Example:
[TestFixture]
public class SuccessTests
{
[SetUp]
public void Init()
{
/* ... */
}
[TearDown]
public void Dispose()
{
/* ... */
}
[Test]
public void Add()
{
/* ... */
}
}
At the end I want to tell you about ExpectedException Attribute.
We use it when we want to test some exception.
This is the way to specify that the execution of a test will throw an exception.
Example:
[ExpectedException( typeof( ArgumentException ) )]
public void TestMethod()
{
...
}
[ExpectedException( typeof( ArgumentException ), ExpectedMessage="expected message" )]
public void TestMethod()
{
...
}
Leave a Reply
You must be logged in to post a comment.
Recent Comments