MSTest测试背景异常处理 [英] MSTest Test Context Exception Handling

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

问题描述

有没有办法可以得到由MSTest框架使用TestContext或基本测试类上的其他方法处理的异常?



如果在我的一个测试中发生了一个未处理的异常,我想旋转exception.Data字典中的所有项目,并将它们显示给测试结果,以帮助我找出测试失败的原因(我们通常将数据添加到异常中)以帮助我们调试生产环境,所以我想做同样的测试)。



注意:我没有测试一个例外是支持HAPPEN (我有其他测试),我正在测试一个有效的情况,我只需要看到异常数据。



这是一个代码示例,我是讨论。

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

//如何在文本上下文中捕获这一点,以便我可以在测试结果的异常中显示所有数据
//

var expected = 100;
try
{
Assert.AreEqual(expected,target.PerformSomeCalculationThatMayDivideByZero());
}
catch(Exception ex)
{
ex.Data.Add(SomethingImportant,我想在测试结果中看到这个,因为它的重要);
ex.Data.Add(预期,预期);
throw ex;
}

}

我明白有什么问题我可能不应该有这样的封装方法,但是我们也有子测试来测试PerformSomeCalculation的所有功能...



但是,如果测试失败,99 %的时间,我重新运行它通过,所以我无法调试任何没有这个信息。我也想在GLOBAL级别上这样做,所以如果任何测试失败,我会得到测试结果中的信息,而不是为每个单独的测试。



  public void AddDataFromExceptionToResults(Exception ex)
{
StringBuilder whereAmI = new StringBuilder();
var holdException = ex;
while(holdException!= null)
{
Console.WriteLine(whereAmI.ToString()+ - + holdException.Message);
foreach(varExpression.Data.Keys中的var item)
{
Console.WriteLine(whereAmI.ToString()+--Data--+ item +:+ holdException。数据项]);
}

holdException = holdException.InnerException;
}
}


解决方案

已经遇到同样的问题,似乎没有得到支持。你甚至不能使用ApplicationDomain的未处理的异常钩子,因为如果MSTEST在冒泡之前没有捕获异常,它本身就会崩溃。



可能的解决方法: p>

  private delegate void TestImplDelegate(); 

private void RunTestWithExceptionLogging(TestImplDelegate testImpl)
{
try
{
testImpl();
}
catch(异常e)
{
string message = e.Message; //不要警告关于未使用的变量

//在这里记录
}
}

[TestMethod]
public void test1 ()
{
RunTestWithExceptionLogging(test1Impl);
}
private void test1Impl()
{
//测试代码到这里

抛出新异常(这应该被测试包装器记录。);
}

[TestMethod]
public void test2()
{
RunTestWithExceptionLogging(test2Impl);
}
private void 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天全站免登陆