Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MSTest: [ClassInitialize] and [ClassCleanup] result in 'Not Run'

Tags:

mstest

Update 2021-07-10: solved

Older versions of Visual Studio and MSTest.Framework nicely state what is wrong, the latest version combination (listed below) of VS2019 and MSTest just puts an exclamation mark in front of the test without stating what is the problem.

The actual problem is that the signatures aren't correct. Both methods must be static and the method flagged with [ClassInitialize] takes a parameter of type TestContext.

    [ClassInitialize]
    public static void TestInitialize(TestContext _)
    {
        ...
    }

    [ClassCleanup]
    public static void TestCleanup()
    {
        ..
    }

Original post:

To be able to perform MSTests in combination with a PLC (industrial realtime control), I need to establish the connection to the PLC before all tests start and disconnect and cleanup after all tests are finished. This can be achieved with the MSTests attributes [AssemblyInitialize] before all test classes or [ClassInitialize] before each indiviual test class (Yes, I'm aware that parallel testing is not possible with such a construct). However, these attributes result in test status 'Not Run' (the blue exclamation mark). So the tests are discovered by the Test Explorer but not executed. Strange enough do the attributes [TestInitialize] and [TestCleanup] work but establishing and cleaning up the PLC connection for each test is too time consuming.

As a workaround I could use a static constructor but to cleanup the connection I need a static Finalizer which doesn't exist in C#. I prefer plain code anyway above such workarounds.

I broke down the problem to its minimum:

    [TestClass]
    public class DebugTest
    {
        [ClassInitialize] 
        public void TestInitialize()
        {
            ...
        }

        [ClassCleanup] 
        public void TestCleanup()
        {
            ...
        }

        [TestMethod]
        public void Test1()
        {
            Assert.IsTrue(true);
        }

        [TestMethod]
        public void Test2()
        {
            Assert.IsFalse(false);
        }

    }

Here is the relevant version information:

  • .NET 5.0
  • All projects are compiled for 'Any CPU', including test project
  • Microsoft Visual Studio Professional 2019: v16.9.4
  • MSTest.TestAdapter: v2.2.5 MSTest.TestFramework: v2.2.5
  • Microsoft.NET.Test.Sdk: v16.10.0

Strange enough I can't find a simular problem in the internet. Does anybody has a solution?

like image 306
user7482328 Avatar asked Sep 11 '25 07:09

user7482328


1 Answers

OP answered their own question but I felt it should be down here

Older versions of Visual Studio and MSTest.Framework nicely state what is wrong, the latest version combination (listed below) of VS2019 and MSTest just puts an exclamation mark in front of the test without stating what is the problem.

The actual problem is that the signatures aren't correct. Both methods must be static and the method flagged with [ClassInitialize] takes a parameter of type TestContext.

[TestClass]
public class UnitTests
{
    [ClassInitialize]
    public static void TestInitialize(TestContext context)
    {
        // Class Setup
    }

    [ClassCleanup]
    public static void TestCleanup()
    {
        // Class Cleanup
    }

    [TestMethod]
    public void UnitTest1()
    {
        Assert.Fail();
    }
}

Additionally, this is the error message you can find in the "Output" window if you choose "Tests" in the drop down.

MSTestAdapter failed to discover tests in class 'UnitTests.YourTestClass' of assembly 'C:......\YourTestClass.Setup has wrong signature. The method must be static, public, does not return a value and should take a single parameter of type TestContext.

like image 94
GER Avatar answered Sep 15 '25 10:09

GER