I'm migrating an existing codebase to MSTest V2 and running into a problem with the TestContext
in the TestCleanup
method.
In MSTest V1 the TestContext
class was static but in V2 it is an instance. I tried to add a parameter to the TestCleanup
method, but then I get this message:
The method must be non-static, public, does not return a value and should not take any parameter.
Ultimately I want to know the name of the test that is being cleaned up and it's test outcome. If it is not possible to get the TestContext
, is there any other way to get to that information in the cleanup context?
Since TestCleanup method and TestContext are not static, then you can just use TestContext inside TestCleanup method without any parameters. Here is an example:
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace MyNamespace.Tests
{
[TestClass]
public class MyTestClass
{
public TestContext TestContext { get; set; }
[TestCleanup]
public void MyTestCleanup()
{
TestContext.WriteLine($"Test Cleanup for {TestContext.TestName}");
}
[TestMethod]
public void MyTestMethod1() { }
[TestMethod]
public void MyTestMethod2() { }
}
}