在developer命令提示符下运行单元测试

时间:2018-06-12 16:01:21

标签: c# unit-testing command-line mstest

我在VS 2017中创建了一个项目,其中一个类是从here复制的     

namespace BankAccountNS
{
    /// <summary>   
    /// Bank Account demo class.   
    /// </summary>   
    public class BankAccount
    {
        public const string DebitAmountExceedsBalanceMessage = "Debit amount exceeds balance";
        public const string DebitAmountLessThanZeroMessage = "Debit amount less than zero";
        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 > m_balance)
            {
                throw new ArgumentOutOfRangeException("amount", amount, DebitAmountExceedsBalanceMessage);
            }

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

            m_balance -= amount;
        }

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

此外,我还使用单一类和两种测试方法创建了单元测试项目

using Microsoft.VisualStudio.TestTools.UnitTesting;
using BankAccountNS;
using System;

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()]
    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("No exception was thrown.");
    }
}

它在VS中运行良好,但是当我尝试在控制台中运行相同时它返回

  

没有要执行的测试。

enter image description here

我检查了之前帖子中的共享解决方案,但不幸的是,它对运行测试没有任何帮助。有人可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:2)

我不知道内置测试资源管理器的例外,但我确实将这个框架与不同的测试运行器一起使用,它被称为smartrunner。 你为什么不试试Typemock的Isolator,它有TmockRunner可以从命令行触发测试,它完全支持像你这样的异常/错误。它有免费试用,所以如果我的建议不起作用,没有造成任何伤害。 由于与MSTest frameworkefork的集成,它将100%适用于您,因此您无需更改代码中的任何内容。