0

I am trying to create a Unit test project in Visual studio 2017 . I want to use Testcontext class prorperties like TestName and etc in my test class and Test method . But when i run the project in debug mode i get null object reference for Testcontext object .

Below is the code :

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace UnitTestProject2
{

    [TestClass]
    public class UnitTest1
    {
        private TestContext _testcontext;
        public  TestContext Testcontext
        {
            get { return _testcontext; }
            set { _testcontext = value; }
        }

        [TestMethod]
        public void TestMethod2()
        {
            Console.WriteLine(Testcontext.TestName);
        }
    }    
}

I am not able to find out how to fix this problem using Coded UI project it works fine.

the exception

enter image description here

James Z
  • 12,209
  • 10
  • 24
  • 44

2 Answers2

1

You need to change the definition for TestContext property.

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace UnitTestProject2 
{
    [TestClass]
    public class UnitTest1
    {
        public TestContext TestContext { get; set; }

        [TestMethod]
        public void TestMethod2()
        {
            Console.WriteLine(Testcontext.TestName);
        }
    }
}
Tanveer Badar
  • 5,438
  • 2
  • 27
  • 32
  • using System; using Microsoft.VisualStudio.TestTools.UnitTesting; namespace UnitTestProject2 { [TestClass] public class UnitTest1 { // private TestContext _testcontext; public TestContext Testcontext { get; set; } [TestMethod] public void TestMethod2() { Console.WriteLine(Testcontext.TestName); } } } – shivam pathak Apr 25 '18 at 07:30
  • My example said `TestContext` not `Testcontext`. Did you try exactly as I wrote the code? – Tanveer Badar Apr 25 '18 at 07:43
  • Yes , it worked thanks a lot . But I did not get logic why TestContext will work and why Testcontext will not . Both are of type TestContext class . – shivam pathak Apr 25 '18 at 10:39
  • I think mstest uses reflection + case sensitive string comparison to manually set the property value. I had the same problem myself so I knew what would have worked here. – Tanveer Badar Apr 25 '18 at 11:32
  • i m uisng vs2017 /nunit3 and followed as mentioned above but it returns null – Salman Nov 08 '19 at 14:52
  • @Salman That answer specifically applies to mstest, as per the question tag. You will need to search or ask a new question for nunit. – Tanveer Badar Nov 08 '19 at 16:00
  • @TanveerBadar https://stackoverflow.com/questions/58768812/testcontext-property-is-null . any help will be appreciated – Salman Nov 09 '19 at 19:01
0

You haven't set value for the _testcontext in the code sample you have provided so you will get NullReferenceException.

danish
  • 5,550
  • 2
  • 25
  • 28
  • i have set it - check this piece of code in the initial question private TestContext _testcontext; public TestContext Testcontext { get { return _testcontext; } set { _testcontext = value; } } – shivam pathak Apr 25 '18 at 07:22