简体   繁体   English

在NUnit测试期间的拆解事件中,如何获取应用于刚测试的方法的属性?

[英]In the teardown event during an NUnit test how can I get to the attribute applied to the method that was just tested?

I have a test method that is run. 我有一个运行的测试方法。 When the method generates an exception I want to know what the name of the test was and the exception content. 当该方法生成异常时,我想知道测试的名称和异常内容。

In the teardown for the test I want to get access to this information. 在测试的拆解中,我希望能够访问这些信息。 How would I get access to it from the [TearDown] attributed method? 如何从[TearDown]归因方法访问它?

I don't think there's a good way built in to nunit, but it's not a hard problem to resolve. 我不认为nunit有一个很好的方法,但解决这个问题不是一个难题。 Just wrap your tests in a try/catch block, catch any exceptions, and save them (and the test name) to a private member variable in your test class. 只需将测试包装在try / catch块中,捕获任何异常,并将它们(和测试名称)保存到测试类中的私有成员变量中。 Then you've got access from your TearDown method. 然后您就可以从TearDown方法访问了。

Not particularly elegant, but it works. 不是特别优雅,但它的工作原理。

You can access text context objects in test tear down method 您可以在测试拆卸方法中访问文本上下文对象

[TearDown]
public void TestTearDown()
{
  // inc. class name
  var fullNameOfTheMethod = NUnit.Framework.TestContext.CurrentContext.Test.FullName; 
  // method name only
  var methodName = NUnit.Framework.TestContext.CurrentContext.Test.Name;
  // the state of the test execution
  var state = NUnit.Framework.TestContext.CurrentContext.Result.State; // TestState enum
}

I don't know which version was first to support it, but mine is 24. 我不知道哪个版本是第一个支持它,但我的是24。

Another solution would be to use a template method and run all tests using this method. 另一种解决方案是使用模板方法并使用此方法运行所有测试。 For example: 例如:

// template method
void Execute(Action test)
{
    try
    {
        test();
    }
    catch (Exception e)
    {
        // handle exception here
        throw;
    }
}

[Test]
public void Test()
{
    Execute(() =>
        {
            // your test here
        });
}

This pattern is particularly useful when your test uses some resources that must be initialized before test and disposed after test (eg temporary file). 当您的测试使用一些必须在测试之前初始化并在测试之后处理的资源(例如临时文件)时,此模式特别有用。 In that case, you can use a type parameter in test delegate. 在这种情况下,您可以在测试委托中使用类型参数。

Another advantage is that you can easily let the test run on different thread, using different culture etc. 另一个优点是您可以轻松地让测试在不同的线程上运行,使用不同的文化等。

Disadvantage is clear: it forces you to use lambda method in every test. 缺点很明显:它迫使你在每次测试中使用lambda方法。

OPTION 1: I don't think you can. 选项1:我认为你不能。 Or rather, I don't know that you can. 或者说,我不知道你可以。 How I approach this need is to use a try/catch on the specific tests, do what I want with the exception and then throw again within the catch block so that the test could fail. 我如何处理这种需求是在特定测试中使用try / catch,使用异常执行我想要的操作,然后再次在catch块中抛出,以便测试可能失败。

try{
    // do something that can potentially throw;
}
catch(Exception ex){
    // do something interesting with the ex;
    throw;
}

OPTION 2: If you've not gone too far along, you may want to use xUnit which has a different exception expectation model and may provide some of the control you are looking for. 选项2:如果你没有走得太远,你可能想要使用具有不同异常期望模型的xUnit,并且可能提供你正在寻找的一些控件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM