-->

NUnit TestContext.CurrentContext null reference ex

2019-06-28 03:14发布

问题:

I've been playing around this morning with watiN / Nunit to capture a screenshot on failed UI tests. However, I'm running into NRE's when accessing Nunits TestContext.CurrentContext...

Any ideas as to what I'm doing wrong?

[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)
    {
    }
}

回答1:

Well...after no success diving into this exception I came across this post: http://www.barebonescoder.com/2010/10/nunit-and-the-new-testcontext-class/

Running my test from nunit's test runner is successful...now to figure out how to make this work with resharpers test runner?



回答2:

Is it the CurrentContext property or the Result property that is NULL? It may be that the Result hasn't been set because the test has not yet completed. I'm working on project in work using WatiN/NUnit and I've been able to use the TestContext class without any problems, but I have to say I've not noticed the state of the Result property.

If the Result property is NULL, then maybe try moving the initialisation of the browser into a TestSetUp method and perform the screen capture into the TestTearDown before disposing of the browser instance.



标签: c# nunit watin