NUnit and Unit Testing at code level

NUnit has two different ways to run your tests. The console runner, nunit-console.exe, is the fastest to launch, but is not interactive. The gui runner, nunit-gui.exe


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)
{
}

public float Balance
{
get{ return balance;}
}
}
}
namespace bank
{
using NUnit.Framework;

[TestFixture]
public class AccountTest
{
[Test]
public void TransferFunds()
{
Account source = new Account();
source.Deposit(200.00F);
Account destination = new Account();
destination.Deposit(150.00F);

source.TransferFunds(destination, 100.00F);
Assert.AreEqual(250.00F, destination.Balance);
Assert.AreEqual(100.00F, source.Balance);

}
}
}


TestFixture

• Indicates class contains test code.
• Class must be public.
• Class must have default constructor.

Test
• Indicates that method is test method.
• Must be void and have no parameters

[Test]
Public void Add()
Public void TestAdd()

Assert class
Used to define post conditions
Methods (overloaded)

COMPARISON
1. AreEqual(expected,actual).
i. In Arrays AreEqual check for length of array and each element is equal, collections are not supported by NUnit 2.2.
2. AreSame—to check same objects are referenced by both arguments.
3.
CONDITION
1. IsTrue
2. IsFalse
3. IsNull
4. IsNot Null

Utility Method
1. Fail
2. Ignore


[Test]
[ExpectedException(typeof(InsufficientFundsException))]
public void TransferWithInsufficientFunds()
{
Account source = new Account();
source.Deposit(200.00F);
Account destination = new Account();
destination.Deposit(150.00F);
source.TransferFunds(destination, 300.00F);
}


ExpectedException
• Indicate that the test code is expecting an exception of a certain type; if such an exception is not thrown during the execution – the test will fail.

Setup

• To initialize class level variables at common place, instead of declaring mock objects in all individual procedures.
• Return type is void and procedure name Init()
• TestFixture can have only one Setup Method, if more than one test will compile but will not run.
• Setup will be fired while running any Test method.

TearDown
• Will be called after every test method.
• Can have only one TearDown Method, if multiple test will compile but will not run.

TestFixtureSetup and TestFixtureTearDown
• Will run before any test method will be called.
• Will run after all tests are completed.

Explicit
• [TestFixture,Explicit]
• [Test,Explicit]

Canonly run by either selection from GUI, by passing in console, by defining attributes. If part of running test ,it will exclude this fixture or test.

Ignore

To ignore any test to run basically used for avoiding commenting or renaming test to avoid runnig test in NUnit

NUnit Attribute Event Order
TestFixtureSetup
Setup
Test
TearDown
TestFixtureTearDown


Mock Objects
Using NUnit.Mock;

Class DynamicMock (type of (object))

• Mock.MockInstance—return instance of mock object
• Mock.SetReturnValue()
• Mock.Expect
• Mock.Verify
• Mock.ExpectNoCall
• Mock.ExpectandThrow


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: