MSTest的测试上下文异常处理 [英] MSTest Test Context Exception Handling

查看:814
本文介绍了MSTest的测试上下文异常处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法,我可以去,是由使用的TestContext或在基础测试类的一些其他方法MSTest的框架处理的异常?



如果在我的测试之一发生未处理的异常,我想通过所有的项目在exception.Data词典旋转并显示给测试结果帮助我弄清楚为什么测试失败(我们通常数据异常增加。帮助我们调试生产ENV,所以我想要做的测试是相同的)



请注意:我没有测试的一个例外是应该发生的(我有其他的测试),我测试有效的情况下,我只需要看到异常的数据。



下面是我什么代码示例议论纷纷。

  [TestMethod的] 
公共无效IsFinanceDeadlineDateValid()
{
变种目标= BusinessObject的新();
SetupBusinessObject(目标);

//我怎么能抓住这个在文本上下文这样我就可以在测试结果异常显示所有数据
// ...

VAR预期= 100;
尝试
{
Assert.AreEqual(预期,target.PerformSomeCalculationThatMayDivideByZero());
}
赶上(异常前)
{
ex.Data.Add(SomethingImportant,我想看到这个测试结果,作为其重要的);
ex.Data.Add(预期,预计);
罚球前;
}

}



据我所知,周围为什么问题我可能不应该有这样的包封的方法,但我们也有子测试来测试PerformSomeCalculation ...



然而,如果测试失败,99的所有功能%的时候,我重新运行它传递,所以没有这些信息,我不能调试任何东西。我也想做到这一点在全球范围内,因此,如果任何测试失败,我得到的测试结果的信息,而不是做它的每个测试。



下面是将放在检测结果异常信息的代码。

 公共无效AddDataFromExceptionToResults(异常前)
{
StringBuilder的WHEREAMI =新的StringBuilder();
VAR holdException =前;
,而(holdException!= NULL)
{
Console.WriteLine(whereAmI.ToString()+ - + holdException.Message);
的foreach(在holdException.Data.Keys VAR项)
{
Console.WriteLine(whereAmI.ToString()+--Data--+项目+:+ holdException。数据项]);
}

holdException = holdException.InnerException;
}
}


解决方案

我一直在运行到同一个问题,似乎没有成为这种支持。你甚至无法使用ApplicationDomain中的未处理的异常挂钩,因为如果MSTEST没赶上例外,他们冒泡那么远,它会自己崩溃之前



可能的解决方法:

 私人委托无效TestImplDelegate(); 

私人无效RunTestWithExceptionLogging(TestImplDelegate testImpl)
{

{
testImpl();
}
赶上(例外五)
{
字符串消息= e.Message; //不警告未使用的变量

//在这里做记录
}
}

[TestMethod的]
公共无效TEST1 ()
{
RunTestWithExceptionLogging(test1Impl);
}
私人无效test1Impl()
{
//测试代码放在这里

抛出新的异常(这应该由测试包装得到记录。);
}

[TestMethod的]
公共无效test2的()
{
RunTestWithExceptionLogging(test2Impl);
}
私人无效test2Impl()
{
//测试代码放在这里

抛出新的异常(这应该由测试包装得到记录。);
}



这当然不是最优的,但至少这样您不必多异常处理程序代码的副本。



我会建议在的 http://connect.microsoft.com/ (或者看看别人已经申请了,并添加你的票。)


Is there a way that I can get to the exception that was handled by the MSTest framework using the TestContext or some other method on a base test class?

If an unhandled exception occurs in one of my tests, I'd like to spin through all the items in the exception.Data dictionary and display them to the test result to help me figure out why the test failed (we usually add data to the exception to help us debug in the production env, so I'd like to do the same for testing).

Note: I am not testing that an exception was SUPPOSED TO HAPPEN (I have other tests for that), I am testing a valid case, I just need to see the exception data.

Here is a code example of what I'm talking about.

[TestMethod]
public void IsFinanceDeadlineDateValid()
{
    var target = new BusinessObject();
    SetupBusinessObject(target);

    //How can I capture this in the text context so I can display all the data 
    //in the exception in the test result...

    var expected = 100;
    try
    {
        Assert.AreEqual(expected, target.PerformSomeCalculationThatMayDivideByZero());
    }
    catch (Exception ex)
    {
        ex.Data.Add("SomethingImportant", "I want to see this in the test result, as its important");
        ex.Data.Add("Expected", expected);
        throw ex;
    }

}

I understand there are issues around why I probably shouldn't have such an encapsulating method, but we also have sub tests to test all the functionality of PerformSomeCalculation...

However, if the test fails, 99% of the time, I rerun it passes, so I can't debug anything without this information. I would also like to do this on a GLOBAL level, so that if any test fails, I get the information in the test results, as opposed to doing it for each individual test.

Here is the code that would put the exception info in the test results.

    public void AddDataFromExceptionToResults(Exception ex)
    {
        StringBuilder whereAmI = new StringBuilder();
        var holdException = ex;
        while (holdException != null)
        {
            Console.WriteLine(whereAmI.ToString() + "--" + holdException.Message);
            foreach (var item in holdException.Data.Keys)
            {
                Console.WriteLine(whereAmI.ToString() + "--Data--" + item + ":" + holdException.Data[item]);
            }

            holdException = holdException.InnerException;
        }
    }

解决方案

I've been running into the same issue, there doesn't seem to be support for this. You can't even use ApplicationDomain's unhandled exception hook since if MSTEST didn't catch exceptions before they bubbled up that far, it would itself crash.

Possible workaround:

    private delegate void TestImplDelegate();

    private void RunTestWithExceptionLogging(TestImplDelegate testImpl)
    {
        try
        {
            testImpl();
        }
        catch (Exception e)
        {
            string message = e.Message; // don't warn about unused variables

            // do logging here
        }
    }

    [TestMethod]
    public void test1()
    {
        RunTestWithExceptionLogging(test1Impl);
    }
    private void test1Impl()
    {
        // test code goes here

        throw new Exception("This should get logged by the test wrapper.");
    }

    [TestMethod]
    public void test2()
    {
        RunTestWithExceptionLogging(test2Impl);
    }
    private void test2Impl()
    {
        // test code goes here

        throw new Exception("This should get logged by the test wrapper.");
    }

It's certainly not optimal, but at least this way you don't have multiple copies of the exception handler code.

I would recommend filing a feature request for this at http://connect.microsoft.com/ (or see if someone else has already requested it, and add your vote.)

这篇关于MSTest的测试上下文异常处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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