(MSTest)扩展ExpectedExceptionBaseAttribute隐藏测试失败的解释 [英] (MSTest) Extending ExpectedExceptionBaseAttribute hides test failure explanation

查看:99
本文介绍了(MSTest)扩展ExpectedExceptionBaseAttribute隐藏测试失败的解释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

运行此测试时:

    [TestMethod]
    [ExpectedException(typeof(SpecialException))]
    public void Test1()
    {
        throw new NotImplementedException();
    }

Visual Studio告诉我为什么失败:

Visual Studio tells me why it fails:

测试方法[...].Test1抛出 System.NotImplementedException异常,但异常 [...].SpecialException是预期的.例外 消息:System.NotImplementedException:方法或操作为 未实现.

Test method [...].Test1 threw exception System.NotImplementedException, but exception [...].SpecialException was expected. Exception message: System.NotImplementedException: The method or operation is not implemented.

但是当我尝试扩展ExpectedExceptionBaseAttribute时(期望在SpecialException中出现错误代码),像这样:

But when I try to extend ExpectedExceptionBaseAttribute (to expect for error code in SpecialException) like that:

[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
class ExpectedSpecialException : ExpectedExceptionBaseAttribute
{
    protected override void Verify(Exception exception)
    {
        SpecialException se = exception as SpecialException;

        if (se == null)
        {
            RethrowIfAssertException(exception);

            throw new Exception("SpecialException was expected but test method threw another one");
        }
    }
}

并像这样使用它:

    [TestMethod]
    [ExpectedSpecialException]
    public void Test1()
    {
        throw new NotImplementedException();
    }

VS产生的信息不是很丰富:

VS produces a not very informative message:

调用的目标已引发异常.

Exception has been thrown by the target of an invocation.

所有示例( 1

All examples (1,2,3) of extending ExpectedExceptionBaseAttribute I found on the internet have the same attempt to provide more info about failure in the thrown exception, but with no result.

我做错什么了吗?有没有办法提供有关此类失败测试的更多信息?

Am I doing something wrong? Is there's a way to provide more information about such failed tests?

推荐答案

我认为您在问题中引用的所有帖子均未正确实现自定义ExpectedException属性.我并不是说它们不是有效的解决方案,而是要解决不同的问题.我认为您遇到的是一个不同的问题,所有这些帖子都未解决.

I don't think any of those posts you have referenced in your question has a proper implementation of a custom ExpectedException attribute. I 'm not saying that they are not valid solutions, I'm saying that they address different issues. I think what you are experiencing is a different problem which has not been addressed by any of those posts.

由于 您可以通过查看

You can verify this by looking at the implementation of ExpectedExceptionAttribute. You can use the reflector and you can see that it internally use the TestContext.

如果您查看 ExpectedExceptionBaseAttribute

[AttributeUsage(AttributeTargets.Method, AllowMultiple = false,
Inherited = true)]
public abstract class ExpectedExceptionBaseAttribute : Attribute
{
     protected internal TestContext TestContext { get; internal set; }

TestContext在运行时在内部设置 ,并且不能注入到 ExpectedSpecialException 属性中. MSTest的 ExpectedExceptionAttribute 可以访问由测试运行程序内部设置的 TestContext .需要特别注意的是,由于缺少MSTest的可扩展性,我们无法真正访问和设置所需的TestContext.

The TestContext sets internally at runtime, and cannot be injected into ExpectedSpecialException attribute. MSTest's ExpectedExceptionAttribute can access the TestContext which has been internally set by the test runner. Important to note that due to the lack of extensibility of the MSTest we cannot really access and set the TestContext as desired.

不确定这是否有帮助,但是您可以使用

Not sure this would help but you can customize your exceptions and pin-point where the exception is thrown using an approach like this. I think this a better approach.

这篇关于(MSTest)扩展ExpectedExceptionBaseAttribute隐藏测试失败的解释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆