NUnit3.0: OneTimeTearDown method executes after executing all test methods in first class and doesn't execute the test methods of other class in same namespace

1,889 views
Skip to first unread message

Pankaj Dubey

unread,
Feb 15, 2016, 3:21:16 AM2/15/16
to NUnit-Discuss
Scenario : I have one setup class which has OnTimeSetup and OneTimeTearDown methods and i have inherited this class in my all classes under same namespace as the Base setup class so once i click on Run All test cases the controller only executes the test methods under one class (in alphabetical order first class) and then executes the statement under OneTimeTearDown (of Setup class) whereas it shall execute all the test methods under another class of same namespace. please suggest if I'm doing anything wrong here or how to overcome this situation.

Charlie Poole

unread,
Feb 15, 2016, 3:25:26 AM2/15/16
to NUnit-Discuss
I'm not clear what you mean by a setup class. Is it a SetUpFixture?
Normally, you don't inherit from such a class.

If you can post an outline of the classes you have, including
attributes on each one, it may be easier to help you.

Charlie
> --
> 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-discus...@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.

Pankaj Dubey

unread,
Feb 15, 2016, 6:03:03 AM2/15/16
to NUnit-Discuss
Thanks for your response Charlie. Yes, I meant it's SetUpFixture class. Please see full description below:

Structure: Let's say there are three classes A, B and C. Class A contains [SetUpFixure] where I have initialized WebDriver (selenium) in [OneTimeSetup] attribute and in [OneTimeTearDown] attribute I've written the code for test cleanup (code to close the browser). Class B and C are my test classes labeled with [TestFixture] attribute and it contains test methods labeled with [Test] attribute. I have inherited Class A into class B and C to use the same web driver instance created in class A SetUp method.

Problem: When I run the test case by clicking on Run All from test Explorer then all the test methods in class B are executed and then control goes to OneTimeTearDown to quit the browser. After that it tries to run the test methods of Class C, this is where C# throws an error as the OneTimeTearDown is already executed and closed the browser (test cleanup process). Whereas my requirement is that once the test methods of Class B is executed then all test methods of Class C shall be executed instead of control passing to OneTimeTearDown for test cleanup.

May be I'm doing something wrong here or misinterpreted the documentation placed on Git. please suggest if you caught the issue.

Thankfully,
Pankaj Dubey

Charlie Poole

unread,
Feb 15, 2016, 11:19:57 AM2/15/16
to NUnit-Discuss
There are two ways to provide common initialization.

1. By inheritance from a base class. In this case the common setup and
teardown executes on a class by class basis.
2. By use of a SetUpFixture. In this case the common setup and
teardown runs only once for all the classes in the namespace.

You have combined the two by using both inheritance and marking the
base as a SetUpFixture. Your OneTimeSetUp will run once before all
classes plus an additional time before each class. Similarly for
OneTimeTearDown. I'm pretty sure that's not what you wanted.

Charlie
Message has been deleted

Pankaj Dubey

unread,
Feb 16, 2016, 1:05:48 AM2/16/16
to NUnit-Discuss
Thanks Charlie, I got the point. 

For the further reference of readers I'm posting here that what I did to overcome this problem that I was facing.

What I intended to do: I created a SetUpFixture class and initialized the driver and other components that I wanted to use throughout my all test classes containing TestFixture. The method that I followed to achieve it was by inheriting the SetUpFixture class in my all test classes to use the same driver and other components so that I had no need to create and initialize it every time when I add  new test class

Problem: problem occurred when the OneTimeTearDown of SetUpFixture class started executing as soon as it finishes the test execution of any one test class but OneTimeSetup did not initialized the components again and control passed to second test class (which was also inheriting the SetUpFixture class) to execute the test methods inside it but as the test cleanup was already processed by OneTimeTearDown there was no instance of driver to run the test methods of second test class and thats where all test cases started failing.

Solution: To overcome this situation what I did
     1. I made the SetUpFixture class as an individual class and did not inherit it any other test classes
     2. To reuse the drivers and other initialized components in SetupFixture I choose another way. I created a local driver in each test classes and initialized that with the driver      created in SetUpFixure class.
     3. I have some encapsulated methods in my SetUpFixture class so to reuse those methods I started calling them by prefixing the SetUpFixture class name

Understand It with the code snippet: A: BaseClass 
[SetUpFixture]
    public class TestSetup
    {
        public static IWebDriver driver; // created Webdriver instance
        public static Actions action;
        public static WebDriverWait wait;
        public static TopNavigation TopNav = new TopNavigation(); // Encapsulated the other class in TestSetup class to reuse it everywhere

        [OneTimeSetUp]
        public void testSetup()
        {
            if (driver == null)
            {
                //Local Tests
                driver = new FirefoxDriver();

                //Broswerstack Test 
                //driver = Browserstack.bs(driver);

                wait = new WebDriverWait(driver, TimeSpan.FromSeconds(3));
                action = new Actions(driver);
                driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(3));
                driver.Manage().Window.Maximize();
            }
        }

Class B: where I'm reusing this:
 [TestFixture]
    class SocialIcons //: TestSetup - earlier i was inheritingthe TestSetup class but now i have commented it
    {
        static IWebDriver driver = TestSetup.driver; // created a local driver and initialized it with the driver initialized in TestSetup class

        [Test]
        public void SocialIcons_Facebook()
        {
            TestSetup.TopNav.HoverOverSocialMegaNav(); // using the encapsulated method
            TestSetup.action.MoveToElement(driver.FindElement(By.XPath(HomePageOR.SocialIconDropDown)));
            driver.FindElement(By.LinkText(HomePageOR.FacebookLink)).Click();
            driver.SwitchTo().Window(driver.WindowHandles.Last());
            Assert.AreEqual("DK Books", driver.Title); // OR it may be 'Security Check Required'
        }
 and that's what i did in my all test classes and it works fine for me.

Charlie, please suggest If i have done something wrong here.
Any suggestions will be appreciated.



On Monday, 15 February 2016 13:51:16 UTC+5:30, Pankaj Dubey wrote:

Charlie Poole

unread,
Feb 16, 2016, 5:31:19 AM2/16/16
to NUnit-Discuss
It seems correct to me. :-)

In fact, using SetUpFixture as a completely separate class, not
inherited anywhere, is what you are expected to do. So apparently the
docs did not give you enough guidance. Can you suggest what we should
change on which pages?

Charlie

Pankaj Dubey

unread,
Feb 17, 2016, 6:16:29 AM2/17/16
to NUnit-Discuss
Thanks Charlie, your support is appreciated in all the way.

Everything looks fine for me, it was just that i was taking the reference of it in the selenium automation context.


On Monday, 15 February 2016 13:51:16 UTC+5:30, Pankaj Dubey wrote:

Jyoti Salve

unread,
Jul 23, 2019, 1:51:41 AM7/23/19
to NUnit-Discuss
Hi,

I am also facing the same problem I want to do some creation and deletion for all the tests run for all the classes.Currently I have created the base class with [SetUpFixure] and in that class I have created 2 functions for [OneTimeSetup] and [OneTimeTearDown]. In OneTimeSetup I am creating something and in oneTimeTearDown I am clearing it and I want, it should clear  when the all the tests of all the classes runs but it is clearing all the test runs of 1 class then when tests of other class is running it is not finding the created one becaus eit is deleted already. Please give me solution for teardown.

Tomáš Vymětal

unread,
Jul 23, 2019, 2:28:09 AM7/23/19
to NUnit-Discuss
Hi Jyoti,

the solution was already posted here.

Your behavior is correct. As you inherit from the base class, it's instanced for every test class you use. So setup and teardown are used for every single class. If you want to really one time "per namespace", separate test fixture to another class and DON'T inherit it from anywhere. It will be called automatically by namespace structure by NUnit.

T.

Jyoti Salve

unread,
Jul 26, 2019, 2:44:07 AM7/26/19
to NUnit-Discuss
Thanks Thomas got it.
Reply all
Reply to author
Forward
0 new messages