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
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);
}
}
}