获取在Xunit中运行测试的名称 [英] Get name of running test in Xunit
本文介绍了获取在Xunit中运行测试的名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用Xunit,我如何获得当前正在运行的测试的名称?
Using Xunit, how can I get the name of the currently running test?
public class TestWithCommonSetupAndTearDown : IDisposable
{
public TestWithCommonSetupAndTearDown ()
{
var nameOfRunningTest = "TODO";
Console.WriteLine ("Setup for test '{0}.'", nameOfRunningTest);
}
[Fact]
public void Blub ()
{
}
public void Dispose ()
{
var nameOfRunningTest = "TODO";
Console.WriteLine ("TearDown for test '{0}.'", nameOfRunningTest);
}
}
修改:
特别是,我正在寻找NUnits TestContext.CurrentContext.Test.Name
属性的替代品.
推荐答案
您可以使用 BeforeAfterTestAttribute
来解决您的问题.有一些方法可以使用Xunit解决您的问题,这将成为TestClassCommand或FactAttribute和TestCommand的子类,但是我认为 BeforeAfterTestAttribute
是最简单的方法.请查看下面的代码.
You can use BeforeAfterTestAttribute
to resolve your case. There are some ways to address your issue using Xunit, which would be to make sub-class of TestClassCommand, or FactAttribute and TestCommand, but I think that BeforeAfterTestAttribute
is the simplest way. Check out the code below.
public class TestWithCommonSetupAndTearDown
{
[Fact]
[DisplayTestMethodName]
public void Blub()
{
}
private class DisplayTestMethodNameAttribute : BeforeAfterTestAttribute
{
public override void Before(MethodInfo methodUnderTest)
{
var nameOfRunningTest = "TODO";
Console.WriteLine("Setup for test '{0}.'", methodUnderTest.Name);
}
public override void After(MethodInfo methodUnderTest)
{
var nameOfRunningTest = "TODO";
Console.WriteLine("TearDown for test '{0}.'", methodUnderTest.Name);
}
}
}
这篇关于获取在Xunit中运行测试的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文