Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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).

like image 240
Dzmitry Martavoi Avatar asked Mar 20 '26 17:03

Dzmitry Martavoi


1 Answers

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.

like image 120
Malice Avatar answered Mar 23 '26 07:03

Malice



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!