Home > Enterprise >  .NET Core 6.0 MSTest project - ClassInititalize/ClassCleanUp prevents testrunner from finding tests
.NET Core 6.0 MSTest project - ClassInititalize/ClassCleanUp prevents testrunner from finding tests

Time:08-02

I have a quite courious behavior of my test project. I have one test class like this:

[TestClass]
public class TestClass
{
    [TestMethod]
    public void Test1()
    {
        ...
    }

    [TestMethod]
    public void Test2()
    {
        ...
    }

    [TestInitialize]
    public void Init()
    {
        ...
    }

    [TestCleanUp]
    public void Clean()
    {
        ...
    }
}

This works all fine, but when I change from TestInitialize to ClassInititalize, respectively from TestCleanUp to ClassCleanUp, the tests are not executed anymore. They are still visible in the Test-Explorer window, but when I execute the tests from the test tree, directly from the test tree branch leaf, from the code window or from the project context menu, then nothing happens: the output windows just says "0 tests found". The same happens when I try to work with AssemblyInitialize/AssemblyCleanUp. Visual Studio restart, project rebuild or Nuget update does not solve the problem.

When I switch back to TestCleanUp/TestInitialize all works fine again ...

Is there a hidden switch I have to use to be able to use ClassInitialize/ClassCleanUp/AssemblyCleanUp/AssemblyInitialize?

CodePudding user response:

With the help of JeroenMostert (see comments of question), the issue has been solved.

I rewrote the initialize method as follow:

[ClassInitialize]
static public void Initialize(TestContext context)
{
    ...;
}

Then all tests are found and therefore executed again.

  • Related