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

[Ignore] on base classes does not work on derived classes. #3681

Open
Bruhst opened this issue Aug 23, 2024 · 3 comments
Open

[Ignore] on base classes does not work on derived classes. #3681

Bruhst opened this issue Aug 23, 2024 · 3 comments

Comments

@Bruhst
Copy link

Bruhst commented Aug 23, 2024

In version 3.5.2 the [Ignore] attribute on base classes does no work on the derived classes.

Example:

[TestClass]

#if !DEBUG
[Ignore]
#endif
public abstract class DebugTestBase
{
...
}

[TestClass]
#if !RELEASE
[Ignore]
#endif
public abstract class ReleaseTestBase
{
...
}

[TestClass]
public class MyDebugTest1 : DebugTestBase
{
...
}

[TestClass]
public class MyReleaseTest1 : ReleaseTestBase
{
...
}

The expected behavior is that in RELEASE builds only unit tests deriving from ReleaseTestBase would be executed and in DEBUG builds only unit tests deriving from DebugTestBase.

In version 3.4.3 the behavior was exactly like expected but now we switched to version 3.5.2 and all unit tests are executed regardless which build configuration is used and regardless which is the base class of the unit test.

@Evangelink
Copy link
Member

Hi @Bruhst,

Would it be possible to get a full reproducer? I have tried with the following code:

TestProject1.csproj:

<Project Sdk="Microsoft.NET.Sdk">

	<PropertyGroup>
		<TargetFramework>net8.0</TargetFramework>
		<LangVersion>latest</LangVersion>
		<ImplicitUsings>enable</ImplicitUsings>
		<Nullable>enable</Nullable>
	</PropertyGroup>

	<ItemGroup>
		<PackageReference Include="MSTest" Version="3.4.3" />
	</ItemGroup>

</Project>

Test1.cs:

namespace TestProject1
{
    [Ignore]
    [TestClass]
    public abstract class Test1
    {
        [TestMethod]
        public void TestMethod1()
        {
        }
    }

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

and I do see the 2 tests executed in 3.4.3

Passed! - Failed: 0, Passed: 2, Skipped: 0, Total: 2, Duration: [521ms]

@pageyboy
Copy link

pageyboy commented Oct 24, 2024

Hi @Evangelink,
I ran into this issue today when upgrading from 3.2.2 to 3.6.1. I've traced it back to a change between 3.4.3 and 3.5.0.
I've created a minimum working example here: https://github.com/pageyboy/minimum-example.
As a bit of background we generally order tests of our classes by nesting test classes within classes e.g.:

using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Example.Tests
{
    [TestClass]
    [Ignore]
    public class SourceClassTests
    {
        [TestMethod]
        public void Constructor_Tests()
        {
            Assert.Fail(); // Skipped in all versions
        }

        [TestClass]
        public class MethodWithinSourceClass : SourceClassTests
        {
            [TestMethod]
            public void Should_DoSomething()
            {
                Assert.Fail(); // Fails from TestFramework 3.5.0
            }
        }
    }
}

I had previously thought that we might move away from this structure for other reasons but up until 3.5.0 the [Ignore] attribute on the SourceClassTests would prevent any of these tests executing. From 3.5.0 the nest test method now fails.

@Evangelink
Copy link
Member

I can reproduce. I wasn't aware of this behavior so we will need to do the following:

  1. Fix it
  2. Document the behavior on doc + code

Additionally, we could add a property/ctor deciding if the behavior should be inherited or not as it doesn't feel natural to me.

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