Unit Test Practice
tags: NTU_ST
Software Testing
:::spoiler Click to open TOC [TOC] :::
Install the environment
- Install Visual Studio with .NET that we can use C# language to implement unit test properly.
Create a project to test
- Create a new project and choose C# as your language and named the project Bank.
- Rename Program.cs as BankAccount.cs and replace all content by the following code. ```c# using System;
namespace BankAccountNS
1 |
|
Create a unit test project
- Create a new project at solution explorer and named it BankTests. The other part just maintain default setup.
- Select MSTest Test Project
<-This is important.
-
Add reference by selecting Add Reference at BankTests/Dependencies
- In the Reference Manager dialog box, expand Projects, select Solution, and then check the Bank item.
Create the test class
- Rename UnitTest1.cs to BankAccountTests.cs and replace the original code with the following section and add using statement at the top of the class file. ```c# using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace BankTests { [TestClass] public class BankAccountTests { [TestMethod] public void TestMethod1() { } } }
1 |
|
- Analyze this part: Assert.AreEqual method will catch the exception when expected value unequal actual value or their difference larger than delta(0.001).
- Comparing with BankAccount.cs, the truly process of Debit function is $beginningBalance - debitAmount = expected$ -> $11.99 - 4.55 = 7.44$
Build and run the test
- On the Build menu, choose Build Solution (or press Ctrl + SHIFT + B) and press Ctrl + E, T to open Test Explorer, then Run All.
- You’ll find that all Test are failed, so you must modify somewhere incorrect.
Fix your code and rerun your tests
- Return to BankAccount.cs and observe Debit() function.
- Must change
m_balance += amount;
tom_balance -= amount;
- Build and Run the code again and you’ll find the test is correct this time
Create and run new test methods
-
Add the following program in test class and rebuild it. ```c# [TestMethod] public void Debit_WhenAmountIsLessThanZero_ShouldThrowArgumentOutOfRange() { // Arrange double beginningBalance = 11.99; double debitAmount = -100.00; BankAccount account = new BankAccount(“Mr. Bryan Walton”, beginningBalance);
// Act and assert Assert.ThrowsException
(() => account.Debit(debitAmount)); }
[TestMethod] public void Debit_WhenAmountIsMoreThanBalance_ShouldThrowArgumentOutOfRange() { // Arrange double beginningBalance = 11.99; double debitAmount = 200.00; BankAccount account = new BankAccount(“Mr. Bryan Walton”, beginningBalance);
1 |
|
Revise BankAccount.cs
- Trace back to BankAccount.Debit and you’ll notice that they used the same exception, so you can determine to use ArgumentOutOfRangeException(String, Object, String) to contain the name of the argument, the argument value, and a user-defined message. ```c# if (amount > m_balance) { throw new ArgumentOutOfRangeException(“amount”); }
if (amount < 0) { throw new ArgumentOutOfRangeException(“amount”); }
1 |
|
- Then modify the 2 conditional statements in the Debit method. ```c# if (amount > m_balance) { throw new System.ArgumentOutOfRangeException(“amount”, amount, DebitAmountExceedsBalanceMessage); }
if (amount < 0) { throw new System.ArgumentOutOfRangeException(“amount”, amount, DebitAmountLessThanZeroMessage); }
1 |
|
The last part
- Not really understatnd about adding Assert.Fail at the end of the test method.