Search code examples
c#unit-testingloggingvs-unit-testing-frameworktestcontext

testContextInstant value is always Null when it was called from logger file to test case file


I have read some information regarding TestContext of MStest and can use it accordingly.

Now my task about TestContext is a little bit different and am confusing how it could work.

The situation related to three files:

  1. In testcase.cs file, TestContext property is in [Test Class]. But in [TestMehtod], I don't want to use like testContextInstance.WriteLine("WRITE TEST PARAMETERS") directly, it will be put in another file named TestLogger.cs.

    [TestClass]
    public class Test1 : BaseTestTemplate
    {
        public TestContext TestContext { get; set; }
    
        public static void FixtureSetUp(TestContext testContext)
        {
        }
    
        public static void FixtureTearDown(TestContext testContext)
        {
        }
    
        [TestInitialize]
        public override void SetUp()
        {
        }
    
        [TestMethod]
        {
            Logger.BeginSection("WRITE TEST PARAMETERS"); // instead of testContextInstance.WriteLine("WRITE TEST PARAMETERS");
        }
    }
    
  2. In AssemblySetup.cs file, in [AssemblyInitialize], public static void AssemblySetUp(TestContext testContext) is done and it include one function that is InitializeLogging(); In this function, I initialize TestLogger with Logger.RegisterLogChannel(LogChannel.TestLog, new TestLogger()).

    [TestClass]
    public static class AssemblySetUpClass
    {
        public static void InitializeLogging()
        {
            string testContext = "";
            Logger.RegisterLogChannel(LogChannel.TestLog, new TestLogger(testContext));
       }
    
        [AssemblyInitialize]
        public static void AssemblySetUp(TestContext testContext)
        {
            InitializeLogging();
        }
    }
    
  3. Following I add testContextInstance.WriteLine(title) in TestLogger.cs. But in debug, testContextInstance is always null.

    public sealed class TestLogger : LoggerBase
    {
        private TestContext testContextInstance;
    
        public TestLogger(string testContext)
        {
        }
    
        public override void BeginSection(string title)
        {
            testContextInstance.WriteLine(title);
            base.BeginSection(title);
        }
    }
    

I am trying to modify Logger.RegisterLogChannel(LogChannel.TestLog, new TestLogger(testContext)) The purpose is to tell TestLogger, testContext will be called. In TestLogger, I also add private TestContext testContextInstance; and public TestLogger(string testContext)

The problem is still the same, in Warning it said, testContextInstance is never assigned to, will always have its default value null

I hope you could understand my problem. Please give me some idea or solutions on how to handle with it, thank you very much.


Solution

  • You've got this code:

     private TestContext testContextInstance;
     public TestLogger(string testContext)
     {
     }
    

    Is the constructor to TestLogger really supposed to be empty? The problem might be as simple as you never assigning your parameter testContext to anything in the constructor body. I would assume that you're supposed to assign it to testContextInstance in the constructor.

     private TestContext testContextInstance;
     public TestLogger(string testContext)
     {
         testContextInstance = testContext;
     }