Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ClassCleanup not called in base class #1889

Open
pi3k14 opened this issue Dec 7, 2023 · 5 comments
Open

ClassCleanup not called in base class #1889

pi3k14 opened this issue Dec 7, 2023 · 5 comments
Assignees
Milestone

Comments

@pi3k14
Copy link

pi3k14 commented Dec 7, 2023

Describe the bug

MSTest.TestFramework 3.0.4

ClassCleanup method in base class not called

[TestClass]
public class BaseClass
{
    [ClassCleanup]
    public static void ClassCleanupBase()
    {
        Console.WriteLine(nameof(BaseClass) + nameof(ClassCleanupBase));
    }
}

[TestClass]
public class UnitTest1 : BaseClass
{
    [TestMethod]
    public void TestMethod1()
    {
        Console.WriteLine(nameof(TestMethod1));
    }
/*
    [ClassCleanup]
    public static void ClassCleanup()
    {
        Console.WriteLine(nameof(UnitTest1) + nameof(ClassCleanup));
    }
*/
}

Additional context

Verified in debuger.
Also mentioned in Class Initialize inheritance #143
The commented method works.

@Evangelink
Copy link
Member

Hi @pi3k14,

I confirm that something is not working fine here. I have tried the various options and I confirm that the base is never called.

@nohwnd
Copy link
Member

nohwnd commented Dec 14, 2023

I cannot find a single place where this works (I am writing to Trace.WriteLine, to make sure the output collector is simply not missing it). Looking at the fixes from PRs the fix that was supposed to implement and fix this is in 2.0.0 and 2.1.0 ( #577 #660 ) , still that does not work for me, so maybe I am just doing it wrong?

```cs
// file GlobalUsings.cs
global using Microsoft.VisualStudio.TestTools.UnitTesting;
<!-- file mstest1.csproj -->
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>net7.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>

    <IsPackable>false</IsPackable>
    <IsTestProject>true</IsTestProject>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
    <PackageReference Include="MSTest.TestAdapter" Version="2.2.8" />
    <PackageReference Include="MSTest.TestFramework" Version="2.2.8" />
  </ItemGroup>

</Project>
// file UnitTest1.cs
using System.Diagnostics;

namespace mstest1;

[TestClass]
public class BaseClass
{
    [ClassCleanup(inheritanceBehavior: InheritanceBehavior.BeforeEachDerivedClass)]
    public static void ClassCleanupBase()
    {
        Trace.WriteLine("base cleanup");
    }
}

[TestClass]
public class UnitTest1 : BaseClass
{
    [TestMethod]
    public void TestMethod1()
    {
        Trace.WriteLine("Test method");
    }

    [ClassCleanup(inheritanceBehavior: InheritanceBehavior.BeforeEachDerivedClass)]
    public static void ClassCleanup()
    {
        Trace.WriteLine("class cleanup");
    }

}

No matter the combination of the class cleanup I simply don't see Trace.WriteLine("base cleanup"); in my output.

(in DebugView++:
image
)

@Evangelink
Copy link
Member

Interesting... I was looking at why our acceptance tests are working and noticed that we have also a [ClassInitialize] and it seems that having it causes the cleanup to be called while not having it results in the behavior observed in this issue.

@Evangelink Evangelink self-assigned this Dec 15, 2023
@Evangelink Evangelink added this to the 3.2.0 milestone Dec 15, 2023
@Evangelink
Copy link
Member

Ok so the problem is way bigger than expected... We need a huge refactoring of the class cleanup handling.

@Evangelink Evangelink modified the milestones: 3.2.0, 4.0.0 Jan 3, 2024
@pi3k14
Copy link
Author

pi3k14 commented Jan 5, 2024

As per @Evangelink comment above

Workaround

Declare your class cleanup method with BeforeEachDerivedClass inheritance behavior, and define a dummy class initialize method with same setting.

[ClassCleanup(InheritanceBehavior.BeforeEachDerivedClass)]
public static void ClassCleanup()
{
  ...
}

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants