martavoi
martavoi

Reputation: 7082

nunit TestContext throws NullReferenceException

I have the following code:

[TestFixture]
public class LexicalTests
{
    [Test]
    public void LexicalTest1()
    {
        TestContext.CurrentContext.TestDirectory;
    }
}

CurrentContext throws an exception while attempting to get TestDirectory or WorkingDirectory property.

How can I solve this problem?

P.S.: On my home PC tests work perfectly (without strange exceptions).

Upvotes: 3

Views: 3547

Answers (1)

Malice
Malice

Reputation: 3977

It seems that some applications that offer the functionality to run NUnit unit tests have a problem with the TestContext class.

The test in class below should pass:

using NUnit.Framework;

namespace UnitTests
{
    [TestFixture]
    public class UnitTests
    {
        [Test]
        public void CurrentContextTest()
        {
            Assert.IsNotNull(TestContext.CurrentContext);
            Assert.IsNotNull(TestContext.CurrentContext.TestDirectory);
            Assert.IsNotNull(TestContext.CurrentContext.WorkDirectory);
        }
    }
}

If the test doesn't pass then, as Dmitry wrote in his comment above, change the NUnit version in the ReSharper menu. From within Visual Studio, go to ReSharper -> Options -> Tools -> NUnit. Click the Specified NUnit installation radio button and ensure that a folder with nunit.core.dll, nunit.core.interfaces.dll and nunit.util.dll is specified. An error will be displayed if the listed files cannot be found.

enter image description here

Once the NUnit version has been changed, re-run the test and it should pass.

Upvotes: 1

Related Questions