Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 以编程方式从编码的UI/单元测试获取错误消息/堆栈跟踪_C#_.net_Unit Testing_Logging_Visual Studio 2013 - Fatal编程技术网

C# 以编程方式从编码的UI/单元测试获取错误消息/堆栈跟踪

C# 以编程方式从编码的UI/单元测试获取错误消息/堆栈跟踪,c#,.net,unit-testing,logging,visual-studio-2013,C#,.net,Unit Testing,Logging,Visual Studio 2013,我正在为一个编码的UI项目编写一个自定义记录器(尽管它可能也适用于单元测试项目)。在我的Logger类的FinishTestLog()方法中,我能够访问TestContext(使用别处提供的引用并存储在私有字段中),并获取CurrentTestOutput,它将写入日志: public static void FinishTestLog() { } 有时测试失败,在这种情况下,测试资源管理器将显示错误消息和堆栈跟踪,并将其写入TRX文件(如果已配置)。最好能够以与TestContext.cu

我正在为一个编码的UI项目编写一个自定义记录器(尽管它可能也适用于单元测试项目)。在我的Logger类的FinishTestLog()方法中,我能够访问TestContext(使用别处提供的引用并存储在私有字段中),并获取CurrentTestOutput,它将写入日志:

public static void FinishTestLog()
{

}


有时测试失败,在这种情况下,测试资源管理器将显示错误消息和堆栈跟踪,并将其写入TRX文件(如果已配置)。最好能够以与TestContext.currentTestOutput相同的方式获取错误消息和堆栈跟踪,而不是将每个测试方法包装在try/catch块中。此信息是否隐藏在TestContext或其他对象中?

很抱歉回答得太晚,但这可能会帮助其他人。我们通过在TestContext对象上使用反射来获得它。如果有人知道如何使用NUnit2.x,请告诉我

    /// <summary>
    /// Returns Error Stack Trace Details extracted from TestContext
    /// </summary>
    /// <param name="testContext"></param>
    /// <returns></returns>
    public static string GetErrorStackTraceFromTestContext(TestContext testContext)
    {
        const BindingFlags privateGetterFlags = System.Reflection.BindingFlags.GetField |
                                                System.Reflection.BindingFlags.GetProperty |
                                                System.Reflection.BindingFlags.NonPublic |
                                                System.Reflection.BindingFlags.Instance |
                                                System.Reflection.BindingFlags.FlattenHierarchy;

        var m_message = string.Empty; // Returns empty if TestOutcome is not failed
        if (testContext.CurrentTestOutcome == UnitTestOutcome.Failed)
        {
            // Get hold of TestContext.m_currentResult.m_errorInfo.m_stackTrace (contains the stack trace details from log)
            var field = testContext.GetType().GetField("m_currentResult", privateGetterFlags);
            var m_currentResult = field.GetValue(testContext);
            field = m_currentResult.GetType().GetField("m_errorInfo", privateGetterFlags);
            var m_errorInfo = field.GetValue(m_currentResult);
            field = m_errorInfo.GetType().GetField("m_stackTrace", privateGetterFlags);
            m_message = field.GetValue(m_errorInfo) as string;
        }

        return m_message;
    }
//
///返回从TestContext提取的错误堆栈跟踪详细信息
/// 
/// 
/// 
公共静态字符串GetErrorStackTraceFromTestContext(TestContext TestContext)
{
const BindingFlags privateGetterFlags=System.Reflection.BindingFlags.GetField|
System.Reflection.BindingFlags.GetProperty|
System.Reflection.BindingFlags.NonPublic|
System.Reflection.BindingFlags.Instance|
System.Reflection.BindingFlags.FlatterHierarchy;
var m_message=string.Empty;//如果测试结果未失败,则返回空
if(testContext.CurrentTestOutput==UnitTestOutput.Failed)
{
//获取TestContext.m_currentResult.m_errorInfo.m_stackTrace(包含日志中的堆栈跟踪详细信息)
var field=testContext.GetType().GetField(“m_currentResult”,privateGetterFlags);
var m_currentResult=field.GetValue(testContext);
field=m_currentResult.GetType().GetField(“m_errorInfo”,privateGetterFlags);
var m_errorInfo=field.GetValue(m_currentResult);
field=m_errorInfo.GetType().GetField(“m_stackTrace”,privategatterflags);
m_message=field.GetValue(m_errorInfo)作为字符串;
}
返回m_消息;
}
要获取错误消息,类似于:

    /// <summary>
    /// Returns Error Message Details extracted from TestContext
    /// </summary>
    /// <param name="testContext"></param>
    /// <returns></returns>
    public static string GetErrorMessageFromTestContext(TestContext testContext)
    {
        const BindingFlags privateGetterFlags = System.Reflection.BindingFlags.GetField |
                                                System.Reflection.BindingFlags.GetProperty |
                                                System.Reflection.BindingFlags.NonPublic |
                                                System.Reflection.BindingFlags.Instance |
                                                System.Reflection.BindingFlags.FlattenHierarchy;

        var m_message = string.Empty; // Returns empty if TestOutcome is not failed
        if (testContext.CurrentTestOutcome == UnitTestOutcome.Failed)
        {
            // Get hold of TestContext.m_currentResult.m_errorInfo.m_message (contains the exception text that was thrown)
            var field = testContext.GetType().GetField("m_currentResult", privateGetterFlags);
            var m_currentResult = field.GetValue(testContext);
            field = m_currentResult.GetType().GetField("m_errorInfo", privateGetterFlags);
            var m_errorInfo = field.GetValue(m_currentResult);
            field = m_errorInfo.GetType().GetField("m_message", privateGetterFlags);
            m_message = field.GetValue(m_errorInfo) as string;
        }

        return m_message;
    }
//
///返回从TestContext提取的错误消息详细信息
/// 
/// 
/// 
公共静态字符串GetErrorMessageFromTestContext(TestContext TestContext)
{
const BindingFlags privateGetterFlags=System.Reflection.BindingFlags.GetField|
System.Reflection.BindingFlags.GetProperty|
System.Reflection.BindingFlags.NonPublic|
System.Reflection.BindingFlags.Instance|
System.Reflection.BindingFlags.FlatterHierarchy;
var m_message=string.Empty;//如果测试结果未失败,则返回空
if(testContext.CurrentTestOutput==UnitTestOutput.Failed)
{
//获取TestContext.m_currentResult.m_errorInfo.m_消息(包含引发的异常文本)
var field=testContext.GetType().GetField(“m_currentResult”,privateGetterFlags);
var m_currentResult=field.GetValue(testContext);
field=m_currentResult.GetType().GetField(“m_errorInfo”,privateGetterFlags);
var m_errorInfo=field.GetValue(m_currentResult);
field=m_errorInfo.GetType().GetField(“m_消息”,privategatterflags);
m_message=field.GetValue(m_errorInfo)作为字符串;
}
返回m_消息;
}

双击失败的测试。测试运行者通常将运行上下文包装在try/catch中,以便为您获取该消息。我应该指出,此方法是从标记为[TestCleanup]attribute.OMG的测试类中的方法调用的。你救了我的命:)-我已经寻找这个答案很久了。最后我可以抓住错误!
    /// <summary>
    /// Returns Error Message Details extracted from TestContext
    /// </summary>
    /// <param name="testContext"></param>
    /// <returns></returns>
    public static string GetErrorMessageFromTestContext(TestContext testContext)
    {
        const BindingFlags privateGetterFlags = System.Reflection.BindingFlags.GetField |
                                                System.Reflection.BindingFlags.GetProperty |
                                                System.Reflection.BindingFlags.NonPublic |
                                                System.Reflection.BindingFlags.Instance |
                                                System.Reflection.BindingFlags.FlattenHierarchy;

        var m_message = string.Empty; // Returns empty if TestOutcome is not failed
        if (testContext.CurrentTestOutcome == UnitTestOutcome.Failed)
        {
            // Get hold of TestContext.m_currentResult.m_errorInfo.m_message (contains the exception text that was thrown)
            var field = testContext.GetType().GetField("m_currentResult", privateGetterFlags);
            var m_currentResult = field.GetValue(testContext);
            field = m_currentResult.GetType().GetField("m_errorInfo", privateGetterFlags);
            var m_errorInfo = field.GetValue(m_currentResult);
            field = m_errorInfo.GetType().GetField("m_message", privateGetterFlags);
            m_message = field.GetValue(m_errorInfo) as string;
        }

        return m_message;
    }