如何在Visual Studio控制台中输出在调试模式下运行的当前单元测试的名称

时间:2017-07-11 12:37:48

标签: c# .net visual-studio unit-testing integration-testing

我想调试几个测试的启动,并从控制台中查看消息。但是,我无法确定每条消息属于哪个测试。有一个[AssemblyInitialise]属性的方法,但它只执行一次。有[TestInitialize]属性的方法,但只对一个类的测试执行,我不知道如何在这个处理程序中获取当前测试的名称。

[澄清]

我使用MsTest进行集成测试。如果解决方案有必要,我可以切换到Nunit。

1 个答案:

答案 0 :(得分:1)

您应该检查此答案How to get the name of the current method from code

我认为它能满足您的需求。 为了知道记录输出的来源,只需在其中列出GetCurrentMethod的输出。为方便起见,我会在这里重新发布。

[MethodImpl(MethodImplOptions.NoInlining)]
public string GetCurrentMethod ()
{
    StackTrace st = new StackTrace ();
    StackFrame sf = st.GetFrame (1);

    return sf.GetMethod().Name;
}

如果您需要的只是查看导致测试失败的原因,请注意在Test Explorer中选择失败的测试将显示失败的堆栈跟踪。