(二)Visual Studio测试方案

单元测试

参考资料:https://docs.microsoft.com/zh-cn/visualstudio/test/walkthrough-creating-and-running-unit-tests-for-managed-code?view=vs-2017

理论介绍

a.为什么使用单元测试?
把程序按照功能划分为独立单元,测试独立单元
单元:函数或功能块

具体步骤

1.创建被测项目

2.创建测试项目

3.测试项目添加对被测项目的引用

4.编写测试方法

注意:
a.一个测试方法针对一个函数或者一个功能块
b.提供函数或功能块的输入信息
c.获得函数或功能块的输出信息
d.把输出信息与期望的结果进行对比

5.执行测试项目


1.创建被测项目

BankAccount.cs

/* 被测项目 */
using System;
namespace BankAccountNS
{
    /// 
    /// Bank Account demo class.
    /// 
    public class BankAccount
    {
        private string m_customerName;

        private double m_balance;

        private bool m_frozen = false;
        /* 定义两个常量,用来显示异常信息 */
        public const string DebitAmountExceedsBalanceMessage = "Debit amount exceeds balance";
        public const string DebitAmountLessThanZeroMessage = "Debit amount is less than zero";
        /*----------------------------*/

        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; }
        }

        /* 取钱 */
            // Method to be tested.           
        public void Debit(double amount)
        {
        //if (amount > m_balance)
        //{
        //    throw new ArgumentOutOfRangeException("amount");
        //}
        //if (amount < 0)
        //{
        //    throw new ArgumentOutOfRangeException("amount");
        //}
        //m_balance -= amount;
        if (amount > m_balance)
        {
            throw new ArgumentOutOfRangeException("amount", amount, DebitAmountExceedsBalanceMessage);
        }

        if (amount < 0)
        {
            throw new ArgumentOutOfRangeException("amount", amount, DebitAmountLessThanZeroMessage);
        }



        }

        /* 存钱 */
        public void Credit(double amount)
        {
            if (m_frozen)
            {
                throw new Exception("Account frozen");
            }

            if (amount < 0)
            {
                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);
        }

    }
}

2.创建测试项目

BankAccountTest.cs

/* 测试项目 */
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
/* 引用被测项目 */
using BankAccountNS;
namespace BankTests
{
    [TestClass] /* 测试类标志 */
    public class BankAccountTests
    {
        [TestMethod] /* 测试方法标志 */
        public void Debit_WithValidAmount_UpdatesBalance()
        {
            // Arrange
            double beginningBalance = 11.99;
            double debitAmount = 4.55;
            double expected = 7.44;
            BankAccount account = new BankAccount("Mr. Bryan Walton", beginningBalance);

            // Act
            account.Debit(debitAmount);

            // Assert
            double actual = account.Balance;
            Assert.AreEqual(expected, actual, 0.001, "Account not debited correctly");
        }
        //创建方法验证借方金额小于零时的行为是否正确
        [TestMethod]
       [ExpectedException(typeof(ArgumentOutOfRangeException))]
        public void Debit_WhenAmountIsLessThanZero_ShouldThrowArgumentOutOfRange()
        {
            // Arrange
            double beginningBalance = 11.99;
            double debitAmount = 20.00;
            BankAccount account = new BankAccount("Mr. Bryan Walton", beginningBalance);

            // Act
            //account.Debit(debitAmount);
            try
            {
                account.Debit(debitAmount);
            }
            catch (ArgumentOutOfRangeException e)
            {
                StringAssert.Contains(e.Message, BankAccount.DebitAmountExceedsBalanceMessage);

            }

            // Assert is handled by the ExpectedException attribute on the test method.
        }
        [TestMethod]
        public void Debit_WhenAmountIsMoreThanBalance_ShouldThrowArgumentOutOfRange()
        {
            // Arrange
            double beginningBalance = 11.99;
            double debitAmount = 20.0;
            BankAccount account = new BankAccount("Mr. Bryan Walton", beginningBalance);

            // Act
            try
            {
                account.Debit(debitAmount);
            }
            catch (ArgumentOutOfRangeException e)
            {
                // Assert
                StringAssert.Contains(e.Message, BankAccount.DebitAmountExceedsBalanceMessage);
                return;
            }

            Assert.Fail("The expected exception was not thrown.");
        }

    }
}

未完待续…

你可能感兴趣的:(代码测试)