Skip to content

Inherited Test SetUp, TearDown, etc. are not executed in .NET Core if they are not public #2448

Description

Hello All,

In trying to port some NUnit tests to .NET Core I've found that methods with [OneTimeSetUp], [SetUp], [TearDown], and [OneTimeTearDown] attributes from a base class are not being called on [Test]s in derived classes. This is only occurring when running against .NET Core, for all versions of NUnit I've tried (3.7.1 and up).

Apologies if this has been documented elsewhere (I believe I checked thoroughly...) or if I'm doing something silly here!

To reproduce:
csproj file:

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

  <PropertyGroup>
    <TargetFramework>netcoreapp2.0</TargetFramework>
  </PropertyGroup>

  <!--<PropertyGroup>
    <TargetFramework>net46</TargetFramework>
  </PropertyGroup>
  <PropertyGroup Condition="'$(TargetFramework)' == 'net46'">
    <TargetFrameworkIdentifier>.NETFramework</TargetFrameworkIdentifier>
  </PropertyGroup>-->

  <ItemGroup>
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.3.0" />
    <PackageReference Include="NUnit" Version="3.8.1" />
    <PackageReference Include="NUnit3TestAdapter" Version="3.8.0" />
  </ItemGroup>

</Project>

Test:

public class BaseClass
    {
        // These methods all get called when running the "Inheritance" test in .NET framework,
        // but not in .NET Core
        [OneTimeSetUp]
        protected void OneTimeSetUp()
        {
            Console.WriteLine("OneTimeSetUp");
        }

        [SetUp]
        protected void SetUp()
        {
            Console.WriteLine("SetUp");
        }

        [TearDown]
        protected void TearDown()
        {
            Console.WriteLine("TearDown");
        }

        [OneTimeTearDown]
        protected void OneTimeTearDown()
        {
            Console.WriteLine("OneTimeTearDown");
        }

        public class DerivedClass : BaseClass
        {
            [Test]
            public void Inheritance()
            {
                Console.WriteLine("Test");
            }
        }
    }

EDIT: use XML, not C#, formatting for csproj

Activity

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

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions