简体   繁体   English

如何在visual studio test explorer中显示自定义异常消息

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

I have a custom exception class 我有一个自定义异常类

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

This can be thrown inside a Test so I get the following message: 这可以在测试中抛出,所以我得到以下消息:

在此输入图像描述

Is there a way to show more info inside the Test Explorer (Without modifing the Message property) 有没有办法在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");
        }
    }
}

Update: 更新:
All my tests work fine and I have proper Assert statements. 我的所有测试工作正常,我有正确的断言语句。
If for some reason though any tests are broken I don't see a meaningful error in the Test Explorer, so I have to wrap each test in a try/catch and debug them one by one manually... 如果由于某种原因虽然任何测试都被破坏了,我在测试资源管理器中看不到有意义的错误,所以我必须在try / catch中包装每个测试并手动逐个调试它们......

It's seems silly having to do that since I have all the error info I need at the time the exception is thrown 由于我在抛出异常时需要所有错误信息,所以这似乎很愚蠢

Why you will throw an exception in [TestMethod] . 为什么要在[TestMethod]抛出异常。 Proper way of testing your exception will be: 测试异常的正确方法是:

    [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