Properties are not applied for parameterized tests

Bug #1208356 reported by Dunemaster
18
This bug affects 3 people
Affects Status Importance Assigned to Milestone
NUnit Framework
Triaged
Wishlist
Unassigned

Bug Description

NUnit 2.6.2
I have a custom attribute for my property

    [AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
    internal class NeedRecreateDataAttribute : PropertyAttribute
    {
        public const string PROPERTY_NAME = "NeedRecreateData";

        public NeedRecreateDataAttribute()
            : base(true)
        {

        }
    }

It is applied ok when used with [Test] attribute:

      [Test]
        [NeedRecreateData]
        public void Test_Assign_String()
        {
        ...
}

     [TearDown]
        public void TestTeadDown()
        {
            if (TestContext.CurrentContext.Test.Properties.Contains(NeedRecreateDataAttribute.PROPERTY_NAME))
            {
                PrepareFixtureData();
            }
        }

But when paired with [TestCase] attribute, the property is not applied
(TestContext.CurrentContext.Test.Properties.Contains(NeedRecreateDataAttribute.PROPERTY_NAME) == false)

Revision history for this message
Charlie Poole (charlie.poole) wrote : Re: [Bug 1208356] [NEW] Properties are not applied for parameterized tests

Actually, it is being applied to the method. However, in this situation,
the method corresponds to a suite containing all your test cases, rather
than to an individual test case. If we gave you the ability (we don't) to
access the containing suite via something like TestContext.ParentTest, you
could find the property there. However, that would involve writing methods
for test cases differently from simple tests and I think it should be
avoided.

I'm inclined to mark this bug "Won't Fix" but I'll give you a chance to
make some suggestions first. :-)

Charlie

On Mon, Aug 5, 2013 at 3:36 AM, Dunemaster <email address hidden> wrote:

> Public bug reported:
>
> NUnit 2.6.2
> I have a custom attribute for my property
>
> [AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
> internal class NeedRecreateDataAttribute : PropertyAttribute
> {
> public const string PROPERTY_NAME = "NeedRecreateData";
>
> public NeedRecreateDataAttribute()
> : base(true)
> {
>
> }
> }
>
> It is applied ok when used with [Test] attribute:
>
> [Test]
> [NeedRecreateData]
> public void Test_Assign_String()
> {
> ...
> }
>
>
> [TearDown]
> public void TestTeadDown()
> {
> if
> (TestContext.CurrentContext.Test.Properties.Contains(NeedRecreateDataAttribute.PROPERTY_NAME))
> {
> PrepareFixtureData();
> }
> }
>
>
> But when paired with [TestCase] attribute, the property is not applied
> (TestContext.CurrentContext.Test.Properties.Contains(NeedRecreateDataAttribute.PROPERTY_NAME)
> == false)
>
> ** Affects: nunitv2
> Importance: Undecided
> Status: New
>
> --
> You received this bug notification because you are subscribed to NUnit
> Extended Testing Platform.
> https://bugs.launchpad.net/bugs/1208356
>
> Title:
> Properties are not applied for parameterized tests
>
> To manage notifications about this bug go to:
> https://bugs.launchpad.net/nunitv2/+bug/1208356/+subscriptions
>

Revision history for this message
Dunemaster (vklava) wrote :

Well, I think applying the property to all test cases is reasonable behavior.
Otherwise, mentioning in [TestCase] and [Property] documentation that they do not work together will be a good thing, may it will save an hour for several people

Revision history for this message
Charlie Poole (charlie.poole) wrote : Re: [Bug 1208356] Re: Properties are not applied for parameterized tests

You're probably right about the docs. We didn't originally include the info
because it seemed "obvious" based on how .NET applies attributes - I guess
only "obvious" to the folks who wrote the code.

The change you suggest is possible. It's also logical for categories: that
is, putting a fixture in a category means putting all the test cases in
that category. However, it's not universally applicable to all attributes.
What this means is that NUnit would have to specify which attributes, when
placed on a method, apply to the suite that is created and which ones apply
to the individual test cases. We could probably do this by use of an
interface or alternatively by an attribute on the attribute definition.

I explain the complexity of the change so you'll understand why I'm
changing this to the NUnit 3.0 project!

Charlie

On Mon, Aug 5, 2013 at 9:01 AM, Dunemaster <email address hidden> wrote:

> Well, I think applying the property to all test cases is reasonable
> behavior.
> Otherwise, mentioning in [TestCase] and [Property] documentation that they
> do not work together will be a good thing, may it will save an hour for
> several people
>
> --
> You received this bug notification because you are subscribed to NUnit
> Extended Testing Platform.
> https://bugs.launchpad.net/bugs/1208356
>
> Title:
> Properties are not applied for parameterized tests
>
> To manage notifications about this bug go to:
> https://bugs.launchpad.net/nunitv2/+bug/1208356/+subscriptions
>

affects: nunitv2 → nunit-3.0
Changed in nunit-3.0:
importance: Undecided → Wishlist
Changed in nunit-3.0:
status: New → Triaged
Revision history for this message
robin9257 (xuanzhaopeng) wrote :

Hello, for version 2.6.3, I have a temporary solution based on TestCaseBuilders extensions.

If I understand well, the NUnit will use NUnitTestCaseBuilder.cs to build all kinds of test case, such as [Test], [TestCase]

However for each [TestCase], it is ParameterizedMethodSuite. So if you read the source code of NUnitTestCaseBuilder.cs , you could find two functions :
- BuildSingleTestMethod
- BuildParameterizedMethodSuite

Thus, when we build [TestCase], the function BuildParameterizedMethodSuite will call BuildSingleTestMethod, so we just need to transfer the "Property" of the TestMethod from "ParameterizedMethodSuite" to each sub "TestMethod", then it could apply all Property of [TestCase] into sub [Test].

Of cause for the steps above, you need to create your own TestCaseBuilder and install it.

Hope it could solve your problem temporary until the release of NUnit 3.0

To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.