- Trước kết bạn cần download NUnit framework từ trang web NUnit website - http://www.nunit.org/index.php?p=download (phiên bản tôi đang sử dụng là NUnit-2.5.2.9222.msi).
Tiến hành cài đặt Nunit bình thường
- Nhấn nút Add, đặt title là NUnit, chọn command là đường dẫn đến file nunit.exe, chọn arguments là /run $(ProjectDir)/$(ProjectFileName), chọn Project Dir là $(ProjectDir).
Code: Select all
using System;
namespace Banking
{
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; }
}
}
}
Bước 4: Tạo các Test case. Nguyên tắc là thêm 1 annotation [TestFixture] cho lớp. Mỗi test case ta phải có annotation [Test]. Ví dụ tôi có test case sau:
Code: Select all
using System;
using NUnit.Framework;
using Banking;
namespace unittest
{
[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);
}
[Test]
public void DepositFunds()
{
Account source = new Account();
source.Deposit(200.00F);
Assert.AreEqual(200.00F, source.Balance);
}
}
}
Nội dung Hướng dẫn chi tiết, file pdf