static TestFixtureSetUp/TestFixtureTearDown methods in base classes are not run

Bug #590970 reported by Scott Hunter
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
NUnit Framework
High
Charlie Poole
NUnit V2
Fix Released
High
Charlie Poole

Bug Description

NUnit 2.5.5. Even though TestFixtureSetUp/TestFixtureTearDown methods can be static, if they are defined them in a base class, they won't be executed. From looking at the code, it seems like the problem is that the reflection code to find fixture setup/teardown methods does not search base classes.

To reproduce:

using System;
using NUnit.Framework;

public class TestBase
{
 [TestFixtureSetUp]
 public static void TestBaseTestFixtureSetUp()
 {
  Console.Out.WriteLine("TestBase TestFixtureSetUp");
 }

 [TestFixtureTearDown]
 public static void TestBaseTestFixtureTearDown()
 {
  Console.Out.WriteLine("TestBase TestFixtureTearDown");
 }
}

public class TestDerived : TestBase
{
 [TestFixtureSetUp]
 public static void TestFixtureSetUp()
 {
  Console.Out.WriteLine("TestDerived TestFixtureSetUp");
 }

 [TestFixtureTearDown]
 public static void TestFixtureTearDown()
 {
  Console.Out.WriteLine("TestDerived TestFixtureTearDown");
 }

 [Test]
 public void TestMethod()
 {
  Console.Out.WriteLine("TestMethod");
 }
}

When you run this test, it only prints messages for the TestDerived class, not for TestBase.

Related branches

Revision history for this message
Jv (jv-ravichandran) wrote : Re: [Bug 590970] [NEW] static TestFixtureSetUp/TestFixtureTearDown methods in base classes are not run
Download full text (3.5 KiB)

Hi Scott,

PFA a screenshot of a test method in the base class. The console does show
the print statements from the base class. In your example, there was no test
method in the class...the reason why the base class' fixtures were not
getting executed.

Looking at it, would you call this a bug?

Jv

On Tue, Jun 8, 2010 at 2:12 AM, Scott Hunter <email address hidden> wrote:

> Public bug reported:
>
> NUnit 2.5.5. Even though TestFixtureSetUp/TestFixtureTearDown methods
> can be static, if they are defined them in a base class, they won't be
> executed. From looking at the code, it seems like the problem is that
> the reflection code to find fixture setup/teardown methods does not
> search base classes.
>
> To reproduce:
>
> using System;
> using NUnit.Framework;
>
> public class TestBase
> {
> [TestFixtureSetUp]
> public static void TestBaseTestFixtureSetUp()
> {
> Console.Out.WriteLine("TestBase TestFixtureSetUp");
> }
>
> [TestFixtureTearDown]
> public static void TestBaseTestFixtureTearDown()
> {
> Console.Out.WriteLine("TestBase TestFixtureTearDown");
> }
> }
>
> public class TestDerived : TestBase
> {
> [TestFixtureSetUp]
> public static void TestFixtureSetUp()
> {
> Console.Out.WriteLine("TestDerived TestFixtureSetUp");
> }
>
> [TestFixtureTearDown]
> public static void TestFixtureTearDown()
> {
> Console.Out.WriteLine("TestDerived TestFixtureTearDown");
> }
>
> [Test]
> public void TestMethod()
> {
> Console.Out.WriteLine("TestMethod");
> }
> }
>
> When you run this test, it only prints messages for the TestDerived
> class, not for TestBase.
>
> ** Affects: nunitv2
> Importance: Undecided
> Status: New
>
> --
> static TestFixtureSetUp/TestFixtureTearDown methods in base classes are not
> run
> https://bugs.launchpad.net/bugs/590970
> You received this bug notification because you are a member of NUnit
> Developers, which is subscribed to NUnit V2.
>
> Status in NUnit V2 Test Framework: New
>
> Bug description:
> NUnit 2.5.5. Even though TestFixtureSetUp/TestFixtureTearDown methods can
> be static, if they are defined them in a base class, they won't be executed.
> From looking at the code, it seems like the problem is that the reflection
> code to find fixture setup/teardown methods does not search base classes.
>
> To reproduce:
>
> using System;
> using NUnit.Framework;
>
> public class TestBase
> {
> [TestFixtureSetUp]
> public static void TestBaseTestFixtureSetUp()
> {
> Console.Out.WriteLine("TestBase TestFixtureSetUp");
> }
>
> [TestFixtureTearDown]
> public static void TestBaseTestFixtureTearDown()
> {
> Console.Out.WriteLine("TestBase TestFixtureTearDown");
> }
> }
>
> public class TestDerived : TestBase
> {
> [TestFixtureSetUp]
> public static void TestFixtureSetUp()
> {
> Console.Out.WriteLine("TestDerived TestFixtureSetUp");
> }
>
> [TestFixtureTearDown]
> public ...

Read more...

Revision history for this message
Scott Hunter (shunter) wrote :

Well, the documentation for TestFixtureSetUp indicates that:

"TestFixtureSetUp methods may be either static or instance methods"

and also:

"The TestFixtureSetUp attribute is inherited from any base class. Therefore, if a base class has defined a SetFixtureSetUp method, that method will be called after each test method in the derived class."

(although the last sentence I've quoted from the documentation has a typo, and seems to be copy/pasted incorrectly from the documentation for TearDown.)

The problem comes in when both are combined, as in my example.

If you go back to my original example, but change the TestBaseTestFixtureSetUp & TearDown methods to be non-static, you can see that they do get inherited by the derived test and executed, without any tests in the base, which is indeed what the documentation says is supposed to happen:

TestBase TestFixtureSetUp
TestDerived TestFixtureSetUp
***** TestDerived.TestMethod
TestMethod
TestDerived TestFixtureTearDown
TestBase TestFixtureTearDown

As additional context, in JUnit, @BeforeClass methods must always be static, and the equivalent code works as expected there:

public class TestBase {
 @BeforeClass
 public static void testBaseBeforeClass() {
  System.out.println("TestBase");
 }
}

public class TestDerived extends TestBase {
 @BeforeClass
 public static void testDerivedBeforeClass() {
  System.out.println("TestDerived");
 }

 @Test
 public void testMethod() throws Exception {
  System.out.println("testMethod");
 }
}

Running testMethod() prints:

TestBase
TestDerived
testMethod

as expected.

Revision history for this message
Charlie Poole (charlie.poole) wrote : Re: [Bug 590970] Re: static TestFixtureSetUp/TestFixtureTearDown methods in base classes are not run

Hi Scott,

This is definitely a bug and was accepted as such - or at least not
rejected. When I'm back from my vacation, I'll prioritize it along with a
number of others we have received. :-)

Charlie

On Tue, Jun 8, 2010 at 3:51 PM, Scott Hunter <email address hidden> wrote:

> Well, the documentation for TestFixtureSetUp indicates that:
>
> "TestFixtureSetUp methods may be either static or instance methods"
>
> and also:
>
> "The TestFixtureSetUp attribute is inherited from any base class.
> Therefore, if a base class has defined a SetFixtureSetUp method, that
> method will be called after each test method in the derived class."
>
> (although the last sentence I've quoted from the documentation has a
> typo, and seems to be copy/pasted incorrectly from the documentation for
> TearDown.)
>
> The problem comes in when both are combined, as in my example.
>
> If you go back to my original example, but change the
> TestBaseTestFixtureSetUp & TearDown methods to be non-static, you can
> see that they do get inherited by the derived test and executed, without
> any tests in the base, which is indeed what the documentation says is
> supposed to happen:
>
> TestBase TestFixtureSetUp
> TestDerived TestFixtureSetUp
> ***** TestDerived.TestMethod
> TestMethod
> TestDerived TestFixtureTearDown
> TestBase TestFixtureTearDown
>
>
> As additional context, in JUnit, @BeforeClass methods must always be
> static, and the equivalent code works as expected there:
>
> public class TestBase {
> @BeforeClass
> public static void testBaseBeforeClass() {
> System.out.println("TestBase");
> }
> }
>
> public class TestDerived extends TestBase {
> @BeforeClass
> public static void testDerivedBeforeClass() {
> System.out.println("TestDerived");
> }
>
> @Test
> public void testMethod() throws Exception {
> System.out.println("testMethod");
> }
> }
>
> Running testMethod() prints:
>
> TestBase
> TestDerived
> testMethod
>
> as expected.
>
> --
> static TestFixtureSetUp/TestFixtureTearDown methods in base classes are not
> run
> https://bugs.launchpad.net/bugs/590970
> You received this bug notification because you are a member of NUnit
> Developers, which is subscribed to NUnit V2.
>

tags: added: confirm
Changed in nunitv2:
importance: Undecided → High
milestone: none → 2.5.6
status: New → Triaged
Changed in nunit-3.0:
status: New → Triaged
importance: Undecided → High
Changed in nunitv2:
assignee: nobody → Charlie Poole (charlie.poole)
status: Triaged → Fix Committed
Changed in nunitv2:
status: Fix Committed → Fix Released
tags: added: v2port
tags: removed: confirm
Changed in nunit-3.0:
assignee: nobody → Charlie Poole (charlie.poole)
milestone: none → 2.9.5
status: Triaged → Fix Committed
Changed in nunit-3.0:
status: Fix Committed → Fix Released
Revision history for this message
Jv (jv-ravichandran) wrote :
To post a comment you must log in.
This report contains Public information  Edit
Everyone can see this information.

Other bug subscribers