Exercise - Complete the "create unit tests" challenge
Code challenges reinforce what you've learned and help you gain some confidence before proceeding.
Challenge: Create unit tests
In this challenge exercise, you need to create xUnit unit tests for the methods of a class.
Prepare your coding environment
You are using the APL2007M4BankAccount sample application from the SampleApps folder. Open the APL2007M4BankAccount sample application in Visual Studio Code. Ensure that the application builds and runs successfully before you start the challenge.
The M4BankAccount sample app contains the following files:
- A
BankAccount
class that represents a bank account. This class has methods to get the account balance, credit or debit the account, transfer money to another account, and others. It also includes various account properties. - A
Program
class that contains aMain
method. The Main method creates bank accounts and simulates transactions using theBankAccount
class.
Challenge requirements
Create at least one xUnit unit test for each of the following methods in the
BankAccount
class:Credit
Debit
Transfer
GetBalance
CalculateInterest
Create a total of at least 12 unique xUnit unit tests.
All the tests must be visible in the Test Explorer.
At least one test for each method must pass when you run the tests.
Challenge instructions
Create xUnit unit tests for the methods in the BankAccount
class using GitHub Copilot Chat.
Use the following steps to complete this challenge:
Open the APL2007M4BankAccount folder in Visual Studio Code.
Review the project files, and then ensure that the solution builds and runs.
It can take 10-15 seconds before the app displays output.
Add an xUnit test project to your solution. Name the project BankAccount.UnitTests.
Create the required project references and class files.
Use GitHub Copilot Chat to create unit tests. See the challenge requirements for details.
Build the solution and ensure that the tests are displayed in the Test Explorer.
Run the tests and ensure that the required number of tests run successfully.