PDA

View Full Version : تست یونیت در سی شارپ



sg.programmer
سه شنبه 21 آبان 1392, 21:28 عصر
سلام
تو این تالار من چیزی در مورد تست ندیدم
اکه ممکنه این تاپیک را گسترش بدیم تا یک چیز جامع ای در مورد تست نرم افزار باشه
-------------------------
این مقاله را از سایت msdn توی این لینک
http://msdn.microsoft.com/en-us/library/ms182532.aspx

اینم از کد ها

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BankAccountNS
{
/// <summary>
/// Bank Account demo class.
/// </summary>
public class BankAccount
{
private string m_customerName;

private double m_balance;

private bool m_frozen = false;

private BankAccount()
{
}

public BankAccount(string customerName, double balance)
{
m_customerName = customerName;
m_balance = balance;
}

public string CustomerName
{
get { return m_customerName; }
}

public double Balance
{
get { return m_balance; }
}

public void Debit(double amount)
{
if (m_frozen)
{
throw new Exception("Account frozen");
}

if (amount > m_balance)
{
throw new ArgumentOutOfRangeException("amount");
}

if (amount < 0)
{
throw new ArgumentOutOfRangeException("amount");
}

m_balance -= amount;
}

public void Credit(double amount)
{
if (m_frozen)
{
throw new Exception("Account frozen");
}

if (amount < 0)
{
throw new ArgumentOutOfRangeException("amount");
}

m_balance += amount;
}

private void FreezeAccount()
{
m_frozen = true;
}

private void UnfreezeAccount()
{
m_frozen = false;
}

public static void Main()
{
BankAccount ba = new BankAccount("Mr. Bryan Walton", 11.99);

ba.Credit(5.77);
ba.Debit(11.22);
Console.WriteLine("Current balance is ${0}", ba.Balance);
}

}
}


اینم از کلاس تست


using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using BankAccountNS;

namespace BankTests
{
[TestClass]
public class BankAccountTests
{// class under test
public const string DebitAmountExceedsBalanceMessage = "Debit amount exceeds balance";
public const string DebitAmountLessThanZeroMessage = "Debit amount less than zero";

[TestMethod]
public void TestMethod1()
{
}
// unit test code
[TestMethod]
public void Debit_WithValidAmount_UpdatesBalance()
{
// arrange
double beginningBalance = 11.99;
double debitAmount = 4.55;
double expected = 7.44;
BankAccount account = new BankAccount("Mr. Bryan Walton", beginningBalance);

// act
account.Debit(debitAmount);

// assert
double actual = account.Balance;
Assert.AreEqual(expected, actual, 0.001, "Account not debited correctly");

}

//unit test method
[TestMethod]
[ExpectedException(typeof(ArgumentOutOfRangeExcepti on))]
public void Debit_WhenAmountIsLessThanZero_ShouldThrowArgument OutOfRange()
{
// arrange
double beginningBalance = 11.99;
double debitAmount = -100.00;
BankAccount account = new BankAccount("Mr. Bryan Walton", beginningBalance);

// act
account.Debit(debitAmount);

// assert is handled by ExpectedException


}


}
}



اگه ممکنه یکی از دوستان توضیحی در مورد کار کرد این تست - و همچنین کد های به مربوطه بدند که چطور تست صورت گرفته شده

برنامه توی پیوست

sg.programmer
چهارشنبه 22 آبان 1392, 15:40 عصر
منتظر دوستان که در این زمینه کار کردن هستیم