Calling OneTimeSetup and OneTimeTearDown from derived classes

816 views
Skip to first unread message

Tomáš Vymětal

unread,
Aug 1, 2017, 3:37:18 AM8/1/17
to NUnit-Discuss
Hi,

Is there any way I can run OneTimeSetup and OneTimeTearDown from derived classes really only once for all classes?

e.g. with:
    [Category("DerivedTest")]
    public class Base
    {
        public static void Log(string message)
        {
            YourLogging($"{message}");
        }
        [OneTimeSetUp]
        public void OTSetup() { Log("Onetime Setup"); }
        [OneTimeTearDown]
        public void OTTeardown() { Log("OneTime Teardown"); }
        [SetUp]
        public void Setup() { Log(" Setup"); }
        [TearDown]
        public void Teardown() { Log(" Teardown"); }
    }
    public class C1 : Base
    {
        [Test] public void T11() { Log("  T1"); }
        [Test] public void T12() { Log("  T2"); }
    }
    public class C2 : Base
    {
        [Test] public void T21() { Log("  T1"); }
        [Test] public void T22() { Log("  T2"); }
    }

I would like to see that OneTimeSetup is called first, then Setup and TearDown for each test and finally OneTimeTearDown. Curently, it's being called for every test class and that means steps are being performed repeatedly.
It's not related only to single inheritance, there are four levels of this. In first, global, users are created for usage by whole suite, then there are areas, which should have some common setup and teardown for tested environment and then test classes setup defines data checked by category. 
Actually, it's not breaking anything but still, creating records repeatedly is pretty time consuming operation and would be nice if could be performed only once.

Thanks

T.

Charlie Poole

unread,
Aug 1, 2017, 8:29:55 AM8/1/17
to NUnit-Discuss

--
You received this message because you are subscribed to the Google Groups "NUnit-Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nunit-discuss+unsubscribe@googlegroups.com.
To post to this group, send email to nunit-...@googlegroups.com.
Visit this group at https://groups.google.com/group/nunit-discuss.
For more options, visit https://groups.google.com/d/optout.

Tomáš Vymětal

unread,
Aug 1, 2017, 8:54:30 AM8/1/17
to NUnit-Discuss
Hi,
I'm aware of this attribute, but as we have multiple levels of setup and teardown, this is not solution.
With previous example, just imagine that there is another Base class and both of them are childs from some master base. And there are more master base classes as well.
I've workarounded lowest level by using partial test class for all area tests which helps me running base onetimes only once, however if we run more areas, advanced base onetimers are being run multiple times.
T.

Charlie Poole

unread,
Aug 1, 2017, 9:07:56 AM8/1/17
to NUnit-Discuss
Hi Tomas,

Well, that's how inherited setup and teardown are designed to work. We created SetUpFixture precisely for the purpose of solving the problem you are having. Essentially, using SetUpFixture for one-time actions over a group of classes replaces inheritance with inclusion. Of course, to use it, you have to design your namespace hierarchy appropriately.

Alternatively, if you prefer to stick with your current approach, you can program the setups to only operate the first time they are called. However, if you have significant teardowns, that approach won't work for you since you cannot determine whether a particular call is the last one.

Charlie

--

Tomáš Vymětal

unread,
Aug 1, 2017, 9:32:02 AM8/1/17
to NUnit-Discuss
Many thanks !!
this really helped, I didn't know that this attribute works in cascade .. I was thinking about it as single for whole namespace .. and unfortunatelly didn't ever check its behavior.
So seems like we'll need to make some sort of order in our namespaces hierarchy .. this will be hard week.
T.
Reply all
Reply to author
Forward
0 new messages