Repository navigation
Description
There is quite an old discussion about the issue here: https://groups.google.com/forum/#!topicsearchin/nunit-discuss/testaction/nunit-discuss/IW7qXnYhIJ8
When test action attribute is applied on a class or base class level it should take precedence over methods marked with SetUp and OneTimeSetUp attributes.
Running a test in attached code block I get the following on the test level:
BaseSetUp
SetUp
BeforeTest (BaseFixture)
BeforeTest (Fixture)
BeforeTest (Test)
Test
AfterTest (Test)
AfterTest (Fixture)
AfterTest (BaseFixture)
TearDown
BaseTearDown
And the following on the fixture level:
BaseOneTimeSetUp
OneTimeSetUp
BeforeTest (BaseFixture - OneTime)
BeforeTest (Fixture - OneTime)
AfterTest (Fixture - OneTime)
AfterTest (BaseFixture - OneTime)
OneTimeTearDown
BaseOneTimeTearDown
This behaviour causes difficulties when you try to decompose a monolithic setup code in the base class and split it between reusable attributes and setup in the derived class. In this case there is no ability to execute test action code before SetUp. This should probably be changed to the following (or we at least need ability to override existing NUnit behaviour):
BeforeTest (BaseFixture)
BaseSetUp
BeforeTest (Fixture)
SetUp
BeforeTest (Test)
Test
AfterTest (Test)
TearDown
AfterTest (Fixture)
BaseTearDown
AfterTest (BaseFixture)
And:
BeforeTest (BaseFixture - OneTime)
BaseOneTimeSetUp
BeforeTest (Fixture - OneTime)
OneTimeSetUp
OneTimeTearDown
AfterTest (Fixture - OneTime)
BaseOneTimeTearDown
AfterTest (BaseFixture - OneTime)
Here is the test code:
public class Action : Attribute, ITestAction
{
private readonly string _className;
public Action(string className)
{
_className = className;
}
public void BeforeTest(ITest test)
{
Trace.Mark(PrepareMarker(test));
}
public void AfterTest(ITest test)
{
Trace.Mark(PrepareMarker(test));
}
private string PrepareMarker(ITest test)
{
string s = _className;
if (test.IsSuite)
{
s += " - OneTime";
}
return s;
}
public ActionTargets Targets { get { return ActionTargets.Suite | ActionTargets.Test; } }
}
[Action("BaseFixture")]
public class BaseFixture
{
[OneTimeSetUp]
public void BaseOneTimeSetUp()
{
Trace.Mark();
}
[SetUp]
public void BaseSetUp()
{
Trace.Mark();
}
[TearDown]
public void BaseTearDown()
{
Trace.Mark();
}
[OneTimeTearDown]
public void BaseOneTimeTearDown()
{
Trace.Mark();
}
}
[Action("Fixture")]
public class Fixture : BaseFixture
{
[OneTimeSetUp]
public void OneTimeSetUp()
{
Trace.Mark();
}
[SetUp]
public void SetUp()
{
Trace.Mark();
}
[TearDown]
public void TearDown()
{
Trace.Mark();
}
[OneTimeTearDown]
public void OneTimeTearDown()
{
Trace.Mark();
}
[Test]
[Action("Test")]
public void Test()
{
Trace.Mark();
}
}
public static class Trace
{
static Trace()
{
Debug.Listeners.Add(new ConsoleTraceListener());
}
public static void Mark(string marker = "")
{
string s = new StackTrace().GetFrame(1).GetMethod().Name;
if (!string.IsNullOrEmpty(marker))
{
s += " (" + marker + ")";
}
Debug.WriteLine(s);
}
}
Activity
Sebazzz commentedon Mar 7, 2017
I would like this as well. It is currently not possible for a test class SetUp to have a dependency on the attribute.
CharliePoole commentedon Mar 8, 2017
@Sebazz This has been sitting for a long time, so I'll try to kick off the design discussion I had hoped we would have. The sequence proposed by @47th in the description of this issue is what I had thought we should do originally - we just never implemented it. However, at this point, I think I would propose a simpler approach, as follows:
And:
Because it only requires making a distinction between attributes found on methods and those found on classes, this is a bit simpler to implement. It does meet the criterion of allowing SetUp or OneTimeSetUp to depend on the attribute. I think I would lean towards doing this first and then waiting to see if there are other use cases that require the more complex ordering.
CharliePoole commentedon Mar 13, 2017
Still looking for comments on my alternate proposal.
I have some of this working already and we could do an interim release of it to see how it works, but I'd still like the feedback!
ChrisMaddock commentedon Mar 13, 2017
As an API - I think I prefer the consistency of @47th's original proposal - however, say that with no view of the technical complications of either implementation! This is fairly niche functionality, so there's definitely value in going with the option which involves the least work, if that satisfies the requirements.
My only concern with that, would be that any changes in behaviour here will likely be breaking, no? So taking this easy route now with a view to potentially changing it later seems like it could cause two sets of breaking changes, rather than just one?
17 remaining items
CharliePoole commentedon Sep 22, 2017
@nunit/framework-team Nobody has answered @bronumski 's request to re-open this issue. He addressed his comment to me but since I'm no longer deciding such things, it would be nice for somebody to reply.
As far as I can tell, @bronumski and @jsgoupil are asking to have the change already made reversed. Is that right guys? The other options are to leave it as is or to make further changes as was originally planned.
Some team members have referred to this as a breaking change. Although I disagree, if you all think it is breaking, then I suppose backing it out makes sense... although that would probably break someone!
jsgoupil commentedon Sep 22, 2017
I had to refactor the hell out of my unit tests to support these changes. I don't think we need to rollback personally.
A lifecycle documentation page would be super helpful.
CharliePoole commentedon Sep 22, 2017
@jsgoupil Sorry, I don't know what a "lifecycle documentation page" is.
bronumski commentedon Sep 22, 2017
@CharliePoole - I am not seeing anything broken as such but then I have not moved to 3.7 yet. My comment was more about the sentiment of the original poster. From where I was I couldn't tell that a change had already been made so I was adding to the existing request to have the TestActions Before and After methods wrap the fixtures Setup and Teardown methods. So if that what has happened then I am happy.
However it sounds like this would cause others such as @jsgoupil issues. If that is the wider use case then I would understand if it had to be backed out. You did hint in another thread that there might be an alternative feature making its way down the pipeline that would replace the ITestAction implementation. Did I miss read that?
jsgoupil commentedon Sep 22, 2017
@CharliePoole I meant to have a documentation page explaining what functions will be called in which order.
CharliePoole commentedon Sep 22, 2017
@jsgoupil That sounds like a good idea. Probably belongs under the heading of Usage Notes in the docs.
CharliePoole commentedon Sep 22, 2017
@bronumski With the change, it's working as you requested. This is how it was designed and documented in the first place with NUnit 3.0. However, it was implemented wrong and stayed that way for several releases. Some people came to rely on the incorrect implementation. On the NUnit team there are differing views about how to handle the situation, to say the least. 😄
rominator1983 commentedon Nov 20, 2017
What I understood from your discussion is that the "new" interfaces (the ones listed here? https://github.com/nunit/docs/wiki/Custom-Attributes) should be used instead of ITestAction.
The question that arises for me is: How would I have to implement my attribute so that I have a hook for something to be done before every SetUp and after TearDown (not OneTimeSetup/OneTimeTearDown)?
I searched the documentation but honestly I don't have a clue since none of the interface I looked into do something similiar to ITestAction. It's sad to hear that you describe that as a kind of a deprecated edge case since I would very much like to implement something like this for an easier refactoring of hundrids/thousands of my tests just right now.
Can you help me out on that?
CharliePoole commentedon Nov 20, 2017
As my earlier comments were intended to indicate...
@nunit/framework-team It has been almost two months since my September 22 comment asking that somebody else take this on and decide whether to open a new issue, re-open this one, etc. I don't feel like I should be making the decision. Whatever is decided has to be designed, implemented and maintained by the new people on the team. If you decide to go ahead, I'm here for input.
jnm2 commentedon Nov 20, 2017
I can shepherd this one. To make discussion easier I'd like to open a new issue and direct everyone there.
Before I do that, I'm going to read through this issue and come up with a proposal. Tagging myself so it shows up in my bookmarked list and I don't forget.
CharliePoole commentedon Nov 20, 2017
@jnm2 I think it starts with one problem, about test actions, which is pretty much resolved, but then morphs to a new one, about extending the API so that interface methods like IApplyToTest can be called on children. But see what you think!
rominator1983 commentedon Nov 20, 2017
Thanks for pointing that out (again). I have only come recently to that discussion.
From an extensibility point of view (a developer using NUnit and running into some composition-limitations of C# and NUnit) I would like to see the following: The possibility to link the following hook points to a test by declaring an attribute at class/method level:
From what I know, the hooks should be implented as an interface (most probably on the attribute itself to simplify things further?)
I do not know, if it is necessary to have a before/after test hook too since after setup and before teardown should have that covered.
For consisteny it should not matter if the attribute is defined at class or method level. This would only define, for which test the linked hooks are called. (Otherwise it would be annoying.)
Of course I do not have any insights into the NUnit architecture. As most people I just want to use it for my daily development. So I cant argue on what the parameters for the methods of the interface should be.
I hope that helps you.
Please let me know what you think about this.
pljanot commentedon Dec 5, 2017
Recently I ran into quite hookup desire (maybe due to my bad design) although I would looking forward to see hookup points as another level of customization. Probably this desire could be workaround but why not to see native solution.