

It uses the Assert.AreEqual method to verify that the ending balance is as expected. The method is straightforward: it sets up a new BankAccount object with a beginning balance and then withdraws a valid amount. Bryan Walton", beginningBalance) Īssert.AreEqual(expected, actual, 0.001, "Account not debited correctly") Public void Debit_WithValidAmount_UpdatesBalance()īankAccount account = new BankAccount("Mr. Add the following method to that BankAccountTests class: The first test verifies that a valid amount (that is, one that is less than the account balance and greater than zero) withdraws the correct amount from the account. You can delete the default TestMethod1 method, because you won't use it in this walkthrough. If the debit amount is valid, the method subtracts the debit amount from the account balance. The method throws an ArgumentOutOfRangeException if the debit amount is less than zero. The method throws an ArgumentOutOfRangeException if the debit amount is greater than the balance. There are at least three behaviors that need to be checked: In this procedure, you'll write unit test methods to verify the behavior of the Debit method of the BankAccount class. You can call these other classes and methods from your test methods. You can have other classes in a unit test project that do not have the attribute, and you can have other methods in test classes that do not have the attribute. The attribute is required on any class that contains unit test methods that you want to run in Test Explorer.Įach test method that you want Test Explorer to recognize must have the attribute. The minimum requirements for a test class are: At the top of the class file, add: using BankAccountNS The BankAccountTests.cs file now contains the following code: using Īdd a using statement to the test class to be able to call into the project under test without using fully qualified names. Type in BankAccountTests and then press Enter.
Cmake add test example code#
To rename the class, position the cursor on UnitTest1 in the code editor, right-click, and then choose Rename (or press F2). From the right-click menu, choose Rename (or press F2), and then rename the file to BankAccountTests.cs. To rename the file, in Solution Explorer, select the UnitTest1.cs file in the BankTests project. You can use the UnitTest1.cs file that was generated by the project template, but give the file and class more descriptive names.

In the Reference Manager dialog box, expand Projects, select Solution, and then check the Bank item.Ĭreate a test class to verify the BankAccount class. In Solution Explorer, select Dependencies under the BankTests project and then choose Add Reference (or Add Project Reference) from the right-click menu.

In the BankTests project, add a reference to the Bank project. The BankTests project is added to the Bank solution. Name the project BankTests and click Next.Ĭhoose either the recommended target framework or. In Visual Studio 2019 version 16.9, the MSTest project template is Unit Test Project.

Throw new ArgumentOutOfRangeException("amount") Public BankAccount(string customerName, double balance) Replace the contents of Program.cs with the following C# code that defines a class, BankAccount: using System If Program.cs is not open in the editor, double-click the file Program.cs in Solution Explorer to open it.
