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

MSTEST0016 and base test class #2683

Open
avivanoff opened this issue Apr 9, 2024 · 4 comments
Open

MSTEST0016 and base test class #2683

avivanoff opened this issue Apr 9, 2024 · 4 comments

Comments

@avivanoff
Copy link

We have the following structure in some of our test projects:

[TestClass]
public abstract class TestBase
{
    public TestContext TestContext { get; set; }

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

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

[TestClass]
public sealed class SomeTest : TestBase
{
    ...
}

After upgrading to 3.3.1 we started getting the error:

error MSTEST0016: Test class 'TestBase' should have at least one test method or be 'static' with method(s) marked by '[AssemblyInitialization]' and/or '[AssemblyCleanup]'

Should MSTEST0016 be checking for this pattern and not trigger the warning/error, or is there a better approach?

@Evangelink
Copy link
Member

Hi @avivanoff,

As usual thank you for testing our analyzers and for your continuous feedback to improve MSTest :)

Assuming you don't have test methods in TestBase then you don't need the [TestClass] attribute on the base class.

Side note not related to MSTEST0016 but [ClassCleanup] is working in a counter intuitive way (I plan to fix this in v4 as it implies breaking changes). By default it is equivalent to [AssemblyCleanup] and runs after all tests have been executed and not all tests of current class or children classes. To change this you will need to pass ClassCleanupBehavior.EndOfClass

@avivanoff
Copy link
Author

Just to be sure. Does it mean that when you process classes marked with TestClassAttribute you include the whole hierarchy? Will DeploymentItemAttribute on TestBase be processed as well?

@avivanoff
Copy link
Author

avivanoff commented Apr 11, 2024

Assuming you don't have test methods in TestBase then you don't need the [TestClass] attribute on the base class.

Removal of TestClass from TestBase triggers MSTEST0004: Public types should be test classes.

@Evangelink
Copy link
Member

Just to be sure. Does it mean that when you process classes marked with TestClassAttribute you include the whole hierarchy?

As far as I can see yes, we consider all base types methods.

Will DeploymentItemAttribute on TestBase be processed as well?

From the simple test I did this is working.

Removal of TestClass from TestBase triggers MSTEST0004: Public types should be test classes.

Let me discuss with the team but I think this is a false positive.

@Evangelink Evangelink added this to the 3.5.0 milestone Apr 24, 2024
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

2 participants