TestContext is null when invoking method using Reflection

Aarti Jangid 0 Reputation points
2023-03-29T08:14:23.1533333+00:00

I am invoking a Data Driven Test Method from another service using Reflection. This Data Driven test read the data from file using TestContext and it is null. The purpose of this service is to run all the End to End tests available in the UnitTest1 class.

When I am running the same test using Test Explorer, it works fine.

Below is the code block for the Unit Test Class:

[TestClass]
class UnitTest1
{
   public TestContext TestContext {get; set;}
   [TestMethod] 
   [DeploymentItem("Data.xml")]       
   [DataSource(
            "Microsoft.VisualStudio.TestTools.DataSource.XML",
            "Data.xml",
            "DataParameters",
            DataAccessMethod.Sequential)]               
   [Timeout(60000)]        
   public void TestMethod1()
   {
     string field1 = this.TestContext.DataRow["Field1"].ToString();
     string field2 = this.TestContext.DataRow["Field2"].ToString();
   }
   [TestMethod]
   [DeploymentItem("Data.xml")]
   [Timeout(60000)]
   public void VerifyDataFile()
   {
            string file = "Data.xml";
            Assert.IsTrue(File.Exists(file), "deployment failed: " + file + " did not get deployed");
   }
}

The csproj of the Unit Test Project:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Library</OutputType>
    <TargetFramework>net472</TargetFramework>
    <RuntimeIdentifier>win7-x64</RuntimeIdentifier>
  </PropertyGroup>
  
  <-- Some dependencies -->
  <ItemGroup>
     <Content Include="..\..\..\folder\Data.xml" Link="Data.xml">
     <CopyToOutputDirectory>Always</CopyToOutputDirectory>
     </Content>
 </ItemGroup>
</Project>

Now the Project class code from where this Unit test is getting called:

public class ExecuteTests
{
        private UnitTest1 unitTestObj;
        public ExecuteTests()
        {
            this.unitTestObj = UnitTest1();
        }
        public void RunAll()
        {
           MethodInfo[] methods = unitTestObj.GetType().GetMethods();
           
           foreach (MethodInfo? method in methods)
            {
                var task = method.Invoke(unitTestObj, new object?[0]);
            }
        }
}

It gives the following error:

Object reference not set to an instance of an object.

while debugging I got to know TestContext is null when test method is getting called using reflection. Is there any way to set the expected TestContext so the reflection code does not fail?

.NET
.NET
Microsoft Technologies based on the .NET software framework.
4,009 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,155 questions
Visual Studio Testing
Visual Studio Testing
Visual Studio: A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.Testing: The act or process of applying tests as a means of analysis or diagnosis.
352 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.

    5 deleted comments

    Comments have been turned off. Learn more

  2. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.