JustCode's Unit Test Window lets you run, debug, filter and search tests within the Visual Studio environment.
In this How To you will learn to run tests through Unit Test Window in a simple application
that acts as a bank account. You are allowed to withdraw and deposit cash.
Before you begin, create a C# console application and add a BankAccount.cs class and a Test project to the solution.
Modify the class so that it resembles the following:
The property AvailableAmount will keep the amount available in the account. You have two methods for
modifying this amount - Withdraw and Deposit to manage the account.
As you see, Withdraw method takes single parameter - the amount to be withdrawn. If this amount is negative
number an exception is thrown, otherwise it is subtracted from AvailableAmount and returned as result.
Deposit method takes single parameter, too - the amount to be added to the account. Again, if it is
negative number, an exception is thrown.
Make two test classes to test both methods. Name them BankAccountWithdrawalTests and BankAccountDepositTests.
Within, define test methods as follows:
The suitable test code should be added to all of the methods in both classes. In order to produce correct
tests insert the following lines of code in your classes:
BankAccountWithdrawalTests.cs
BankAccountDepositTests.cs
To see all tests, execute the Unit Test Window by navigating through JustCode | Windows | Show Unit Test Window... or by pressing Ctrl+Shift+K.
You can run them by pressing
button.
You will get the following result:
If you want to get a failed test, you can change the code for some of methods to fail. I.e. get the Withdrawal_AvailableAmount method and modify its body like below:
This will fail as we expect ba.AvailableAmount to be 90, not 80. Run the tests in the Unit Test Window and you get the following result:
Refer to Unit Test Window and Unit Testing category for more information on testing.