“Woodgrove Bank”示例由可置入简单程序的代码组成。然后可以生成测试 Woodgrove Bank 程序各种方法(公共和私有方法)的单元测试。
这些示例代码是为以下演练而提供的:
演练:创建并运行单元测试。此演练指导您通过一系列步骤来创建和自定义单元测试、运行单元测试以及检查测试结果。
演练:运行测试并查看代码覆盖率。此演练说明如何查看代码覆盖率数据,这些数据显示正在测试的项目代码占所有代码的比例。
演练:使用命令行测试实用工具。在此演练中,使用 MSTest.exe 命令行实用工具来运行测试并查看结果。
using System; namespace BankAccountNS { /// <summary> /// Bank Account demo class. /// </summary> public class BankAccount { private string m_customerName; private double m_balance; private bool m_frozen = false; private BankAccount() { } public BankAccount(string customerName, double balance) { m_customerName = customerName; m_balance = balance; } public string CustomerName { get { return m_customerName; } } public double Balance { get { return m_balance; } } public void Debit(double amount) { if (m_frozen) { throw new Exception("Account frozen"); } if (amount < 0) { throw new ArgumentOutOfRangeException("amount"); } m_balance += amount; } public void Credit(double amount) { if (m_frozen) { throw new Exception("Account frozen"); } if (amount > m_balance) { throw new ArgumentOutOfRangeException("amount"); } m_balance += amount; } private void FreezeAccount() { m_frozen = true; } private void UnfreezeAccount() { m_frozen = false; } public static void Main() { BankAccount ba = new BankAccount("Mr. Bryan Walton", 11.99); ba.Credit(5.77); ba.Debit(11.22); Console.WriteLine("Current balance is ${0}", ba.Balance); } } } /* The example companies, organizations, products, domain names, e-mail addresses, logos, people, places, and events depicted herein are fictitious. No association with any real company, organization, product, domain name, email address, logo, person, places, or events is intended or should be inferred. */ |