【问题标题】:NUnit TestContext.CurrentContext null reference exceptionNUnit TestContext.CurrentContext 空引用异常
【发布时间】:2011-03-19 17:32:07
【问题描述】:

今天早上我一直在玩 watiN / Nunit 来捕获失败的 UI 测试的屏幕截图。但是,我在访问 Nunits TestContext.CurrentContext 时遇到了 NRE...

关于我做错了什么有什么想法吗?

[TestFixture]
class SomePageTest
{
    [Test]
    [STAThread]
    public void Page_IsAvailable()
    {
        var browser = new SomePage();

        Assert.IsTrue(browser.ContainsText("Something"));            

        if (TestContext.CurrentContext.Result.Status == TestStatus.Failed)
        {
            browser.CaptureWebPageToFile(@"X:\location\" + TestContext.CurrentContext.Test.FullName);
        }
    }
}

public class SomePage: IE
{
    public static string SomePageUrl = "http://somepage.com/someurl";
    public SomePage() : base(SomePageUrl)
    {
    }
}

【问题讨论】:

    标签: c# nunit watin


    【解决方案1】:

    嗯...在潜入这个异常没有成功后,我发现了这篇文章:http://www.barebonescoder.com/2010/10/nunit-and-the-new-testcontext-class/

    从 nunit 的测试运行器运行我的测试是成功的...现在要弄清楚如何使用 resharpers 测试运行器进行这项工作?

    【讨论】:

    • R# 拥有(或拥有,无论如何,它当然曾经)自己的 NUnit 测试运行器实现——它可能只是不支持该功能。
    • Resharper 带有 NUnit 2.5.7。不确定是否可以将 resharper 设置为使用 2.5.9?不管 r# 6 即将到来,我都可以通过使用 nunits 测试运行器来获得。
    【解决方案2】:

    是 NULL 的 CurrentContext 属性还是 Result 属性?可能是因为测试还没有完成,所以没有设置Result。我正在使用 WatiN/NUnit 处理项目,我已经能够毫无问题地使用 TestContext 类,但我不得不说我没有注意到 Result 属性的状态。

    如果 Result 属性为 NULL,那么可以尝试将浏览器的初始化移动到 TestSetUp 方法中,并在处理浏览器实例之前将屏幕捕获到 TestTearDown。

    【讨论】:

    • 感谢您的回复。结果属性为 NULL。将尝试重新安排测试并查看结果如何:State = 'NUnit.Framework.TestContext.CurrentContext.Result.State' 引发了“System.NullReferenceException”类型的异常
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-05-16
    • 2013-09-21
    • 2013-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多