In Solution Explorer, double-click the Class1.cs file in the Bank project.
This opens the source file for viewing and editing.
In the BankAccount class in theClass1.cs file, scroll to the Debit() method.
Right-click the Debit() method and select Create Unit Tests.
This displays the Create Unit Tests dialog box.
Under Current selection, a tree structure shows the class and member hierarchy of the assembly that houses the BankAccount class. You can use this page to generate unit tests for any selection of those members, and to choose a test project into which you want the generated unit tests to be placed.
In the tree structure, only the Debit() method is selected. Leave it selected and also select the Credit() method.
For Output project, select Create a new Visual C# test project.
Click Settings.
The Test Generation Settings dialog box appears. Under Naming settings, you can change the way test files, test classes, and test methods are named as they are generated. Under General, you can change other aspects of test generation. Leave the default values for these settings and then click OK.
In the Create Unit Tests dialog box, click OK.
The New Test Project dialog box is displayed.
Accept the default name and then click Create.
This creates a project named TestProject1, which is displayed in Solution Explorer.
A file named BankAccountTest.cs, which contains a test class is added to TestProject1. The class is populated with a TestContext property and with methods to test the Debit() and Credit() methods.
Note: |
|---|
Every test method is automatically assigned the TestMethod() attribute. Each test corresponds to a single method in the code under test that you want to test. Test methods are housed in a test class that is assigned the TestClass() attribute. |
In BankAccountTest.cs, specify values for the variables to be tested. Scroll to the DebitTest method, where you see // TODO lines that indicate variables to set. What values should you use? To answer that, you must know the values that will be used when the application is run. We determine those values in the following step.
Open the Class1.cs file and scroll to the Main method. Notice that the customer name is initialized to Mr. Bryan Walton, the account balance is initialized to 11.99, the Credit method is called with a parameter of 5.77, and the Debit method is called with a parameter of 11.22. Therefore, if this account starts with a Balance of 11.99, a call to the Debit method while passing 11.22 should give you a new Balance of 0.77.
In the BankAccountTest.cs file, scroll to the DebitTest method.
Set the following values:
BankAccount target = new BankAccount("Mr. Bryan Walton", 11.99);
double amount = 11.22;
In the BankAccountTest.cs file, make these same changes in the CreditTest method.
Save the BankAccountTest.cs file.
You have created a source-code file that contains tests for the Bank project. You are now ready to run the tests in the BankAccountTest class on the code of the Bank project.