簡體   English   中英

如何在visual studio test explorer中顯示自定義異常消息

[英]How can I show a custom exception message in visual studio test explorer

我有一個自定義異常類

public class CustomException : Exception
{
    string moreInfo;
    public CustomException(string message, string info) : base(message)
    {
        moreInfo = info;
    }

    public override string ToString()
    {
        return "Custom Exception Info:" + moreInfo;
    }
}

這可以在測試中拋出,所以我得到以下消息:

在此輸入圖像描述

有沒有辦法在Test Explorer中顯示更多信息(不修改Message屬性)

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

namespace UnitTestProject1
{
    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void TestMethod1()
        {
            throw new CustomException("aaa", "bla bla");
        }
    }
}

更新:
我的所有測試工作正常,我有正確的斷言語句。
如果由於某種原因雖然任何測試都被破壞了,我在測試資源管理器中看不到有意義的錯誤,所以我必須在try / catch中包裝每個測試並手動逐個調試它們......

由於我在拋出異常時需要所有錯誤信息,所以這似乎很愚蠢

為什么要在[TestMethod]拋出異常。 測試異常的正確方法是:

    [TestMethod]
    public void TestMethod1()
    {
        try
        {
            throw new CustomException("aaa", "bla bla");
        }
        catch(CustomException ex)
        {
            //some assert condition with proper message
            Assert.AreEqual(ex.Message, "some Stuff", "My message");
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM