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

MSTEST0008: (non-abstract, non-generic) [TestInitialize] not allowed in abstract generic base class (or use case not explicitly forbidden) #3324

Closed
LukasGelke opened this issue Jul 24, 2024 · 2 comments

Comments

@LukasGelke
Copy link

Describe the bug

  • [TestInitialize] is allowed on abstract non-generic types
  • test inheritance allows multiple [TestInitialize] with one per class, which all run correctly

Steps To Reproduce

public abstract class MyTestBase<T>
{
  protected int a;

  [TestInitialize]
  public void Init() => a = 1; // MSTEST0008
}

[TestClass]
public sealed class MyTest : MyTestBase<int>
{
  private int b;

  [TestInitialize]
  public void Init2() => b = 2;

  [TestMethod]
  public void MyTestMethod()
  {
    Assert.AreEqual(1, a); // ok
    Assert.AreEqual(2, b); // ok
  }
}

Expected behavior

  • either runtime Exception a la UTA018: Example.Test.MyTest: Cannot define more than one method with the TestInitialize attribute.
  • or make analyzer value abstract more than generic-ness

Actual behavior

MSTEST0008 gets diagnosed, but unit tests run both Init-Methods; and Init-Methods from abstract generic base classes correctly

Additional context

I guess this would also be the same for TestCleanup.

But not sure how this relates to ClassInitialize/ClassCleanup, since it's also defined in 'this specific test-class' (contrary to AssemblyInitialize/ AssemblyCleanup).

@nohwnd
Copy link
Member

nohwnd commented Jul 24, 2024

either runtime Exception a la UTA018: Example.Test.MyTest: Cannot define more than one method with the TestInitialize attribute.

This would be problem for Playwright which relies on multiple test setups and teardowns.

@Evangelink
Copy link
Member

MSTEST0008 was buggy (see #3276), we got it fixed.

@Evangelink Evangelink closed this as not planned Won't fix, can't repro, duplicate, stale Jul 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants