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:
Strange enough I can't find a simular problem in the internet. Does anybody has a solution?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With