OneTimesetup is getting invoked for each Test,I thought OneTimesetup is like BeforeSuite,TestNG annotation












0















In Nunit C# Im trying to open the application only once and make modification to it, for example i created this demo of Notepad application. In my real time project,i just have to log in to the application just once and execute all 100 test cases and only then close the Desktop application. Kindly tell me what am i doing wrong here,thanks a lot! Btw,im new to C#



using NUnit.Framework;
using OpenQA.Selenium.Remote;
using System;
using OpenQA.Selenium;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;

namespace Notepad
{ }
[SetUpFixture]
public class BaseClass

{
public static IWebDriver driver;

[OneTimeSetUp]
public static void AssemblyInitalize()
{
var dc = new DesiredCapabilities();
dc.SetCapability("app", @"C:\Windows\System32\notepad.exe");
driver = new RemoteWebDriver(new Uri("http://localhost:9999"), dc);
Thread.Sleep(5000);
}

[OneTimeTearDown]
public static void oneTearDown()
{
driver.FindElement(By.Id("Close")).Click();

}
}


---First Test---



namespace Notepad
{ [TestFixture]
public class Notepad2:BaseClass
{

[Test]
public void test2()
{
driver.FindElement(By.Id("15")).SendKeys("My Teacher ");
}

}
}


---- Second Test Class ----



 using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenQA.Selenium;

namespace Notepad
{ [TestFixture]
public class NoteTest : BaseClass

{


[Test]
public void Test()
{

driver.FindElement(By.Id("15")).SendKeys("...is Coming now");
}
}
}









share|improve this question





























    0















    In Nunit C# Im trying to open the application only once and make modification to it, for example i created this demo of Notepad application. In my real time project,i just have to log in to the application just once and execute all 100 test cases and only then close the Desktop application. Kindly tell me what am i doing wrong here,thanks a lot! Btw,im new to C#



    using NUnit.Framework;
    using OpenQA.Selenium.Remote;
    using System;
    using OpenQA.Selenium;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Threading;

    namespace Notepad
    { }
    [SetUpFixture]
    public class BaseClass

    {
    public static IWebDriver driver;

    [OneTimeSetUp]
    public static void AssemblyInitalize()
    {
    var dc = new DesiredCapabilities();
    dc.SetCapability("app", @"C:\Windows\System32\notepad.exe");
    driver = new RemoteWebDriver(new Uri("http://localhost:9999"), dc);
    Thread.Sleep(5000);
    }

    [OneTimeTearDown]
    public static void oneTearDown()
    {
    driver.FindElement(By.Id("Close")).Click();

    }
    }


    ---First Test---



    namespace Notepad
    { [TestFixture]
    public class Notepad2:BaseClass
    {

    [Test]
    public void test2()
    {
    driver.FindElement(By.Id("15")).SendKeys("My Teacher ");
    }

    }
    }


    ---- Second Test Class ----



     using NUnit.Framework;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using OpenQA.Selenium;

    namespace Notepad
    { [TestFixture]
    public class NoteTest : BaseClass

    {


    [Test]
    public void Test()
    {

    driver.FindElement(By.Id("15")).SendKeys("...is Coming now");
    }
    }
    }









    share|improve this question



























      0












      0








      0








      In Nunit C# Im trying to open the application only once and make modification to it, for example i created this demo of Notepad application. In my real time project,i just have to log in to the application just once and execute all 100 test cases and only then close the Desktop application. Kindly tell me what am i doing wrong here,thanks a lot! Btw,im new to C#



      using NUnit.Framework;
      using OpenQA.Selenium.Remote;
      using System;
      using OpenQA.Selenium;
      using System.Collections.Generic;
      using System.Linq;
      using System.Text;
      using System.Threading.Tasks;
      using System.Threading;

      namespace Notepad
      { }
      [SetUpFixture]
      public class BaseClass

      {
      public static IWebDriver driver;

      [OneTimeSetUp]
      public static void AssemblyInitalize()
      {
      var dc = new DesiredCapabilities();
      dc.SetCapability("app", @"C:\Windows\System32\notepad.exe");
      driver = new RemoteWebDriver(new Uri("http://localhost:9999"), dc);
      Thread.Sleep(5000);
      }

      [OneTimeTearDown]
      public static void oneTearDown()
      {
      driver.FindElement(By.Id("Close")).Click();

      }
      }


      ---First Test---



      namespace Notepad
      { [TestFixture]
      public class Notepad2:BaseClass
      {

      [Test]
      public void test2()
      {
      driver.FindElement(By.Id("15")).SendKeys("My Teacher ");
      }

      }
      }


      ---- Second Test Class ----



       using NUnit.Framework;
      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Text;
      using System.Threading.Tasks;
      using OpenQA.Selenium;

      namespace Notepad
      { [TestFixture]
      public class NoteTest : BaseClass

      {


      [Test]
      public void Test()
      {

      driver.FindElement(By.Id("15")).SendKeys("...is Coming now");
      }
      }
      }









      share|improve this question
















      In Nunit C# Im trying to open the application only once and make modification to it, for example i created this demo of Notepad application. In my real time project,i just have to log in to the application just once and execute all 100 test cases and only then close the Desktop application. Kindly tell me what am i doing wrong here,thanks a lot! Btw,im new to C#



      using NUnit.Framework;
      using OpenQA.Selenium.Remote;
      using System;
      using OpenQA.Selenium;
      using System.Collections.Generic;
      using System.Linq;
      using System.Text;
      using System.Threading.Tasks;
      using System.Threading;

      namespace Notepad
      { }
      [SetUpFixture]
      public class BaseClass

      {
      public static IWebDriver driver;

      [OneTimeSetUp]
      public static void AssemblyInitalize()
      {
      var dc = new DesiredCapabilities();
      dc.SetCapability("app", @"C:\Windows\System32\notepad.exe");
      driver = new RemoteWebDriver(new Uri("http://localhost:9999"), dc);
      Thread.Sleep(5000);
      }

      [OneTimeTearDown]
      public static void oneTearDown()
      {
      driver.FindElement(By.Id("Close")).Click();

      }
      }


      ---First Test---



      namespace Notepad
      { [TestFixture]
      public class Notepad2:BaseClass
      {

      [Test]
      public void test2()
      {
      driver.FindElement(By.Id("15")).SendKeys("My Teacher ");
      }

      }
      }


      ---- Second Test Class ----



       using NUnit.Framework;
      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Text;
      using System.Threading.Tasks;
      using OpenQA.Selenium;

      namespace Notepad
      { [TestFixture]
      public class NoteTest : BaseClass

      {


      [Test]
      public void Test()
      {

      driver.FindElement(By.Id("15")).SendKeys("...is Coming now");
      }
      }
      }






      c# nunit winium






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 25 '18 at 7:39







      Im Nobody

















      asked Nov 25 '18 at 7:27









      Im NobodyIm Nobody

      186




      186
























          2 Answers
          2






          active

          oldest

          votes


















          2














          From the documentation, OneTimeSetup is called once before all tests in a TestFixture. Your example has two fixtures, so the set up is called twice. You need to have all your tests in the same Fixture.



          This behaviour is different to the old [Setup] on SetupFixture attribute, which ran once for all tests in a namespace.






          share|improve this answer


























          • Thanks a lot for the reply, how can i put them in the same Fixture, do i 've to bunk all test cases in the same class?

            – Im Nobody
            Nov 25 '18 at 9:00











          • or Make the baseClass as a TestFixture and extend all the test cases.

            – Im Nobody
            Nov 25 '18 at 9:09













          • I just now tried by making a BaseClass a TestFixture and extend two test cases, removed the TestFixture attribute from the both tests, still the Notepad is opening twice and tearing down twice. its working only when i put thetest cases inside the same class i.e Base Class. Order is like this 1)OneTimeSetup 2)DerivedTestClass 3) OneTimeTeardown for every test case. But when test cases are in the same class i.e BaseClass, oneTimeSetup and OneTimeTearDown occurring only once before and after all the test cases respectively.

            – Im Nobody
            Nov 25 '18 at 9:23













          • It's quite normal to have all the tests that rely on one setup in the same class, even if there are 100 of them.

            – iakobski
            Nov 25 '18 at 9:43











          • Thanks for the reply again, test cases are not really small, each test case as you may already know containing more than 50 steps at least. The code will be too lengthy and would become unreadable and very difficult to debug.

            – Im Nobody
            Nov 25 '18 at 9:56



















          1














          Regarding SetUpFixture, the first answer is incorrect.



          SetUpFixture works exactly the same way as it has always worked. If you have a SetUpFixture, it's OneTimeSetUp and OneTimeTearDown methods will run once for all the fixtures under the same namespace.



          On the other hand, inside a TestFixture the OneTimeSetUp and OneTimeTearDown methods run just once for the fixture itself.



          In other words, the methods run "one time" within the particular scope in which they are used, namespace or fixture.



          The problem is that you are using the same class as both a SetUpFixture and as the base class for all your TestFixtures. That means if you have n test fixtures, it will run n + 1 times! That's what you are seeing.



          SetUpFixtures do not (have never had) anything to do with fixture inheritance. You should either make your class a SetUpFixture or a TestFixture base class. If, for whatever reason, you need both, then use two classes. In this case, you need only the SetUpFixture, without the inheritance.



          When to inherit from a base class: When you want the same code to execute many times, once for each fixture. Note that your base class, when used for one-time setup and teardown in this way, should normally not be marked as a TestFixture.



          When to use a SetUpFixture: When you want some code to execute only once, before every any fixture runs and again once after all the fixtures have run.



          In your example, you are using a SetUpFixture to control the timing of the initialization. You are using inheritance to allow you to share the driver. Problem is that the OneTimeSetUp in the base class is actually part of each test fixture and you don't want that.



          I'd be concerned about a hundred or so tests all using the same driver. I've seen some people use one driver per test and others one driver per fixture_. Using one for __everything implies that you are being extraordinarily careful that each test cleans up after itself and restores the driver to the same state. I doubt that's possible.



          However, as an exercise, here is how to do it if you really want to:
          1. Have a base class with nothing but the driver member.
          2. Derive the SetUpFixture from the base class and create / destroy the driver there.
          3. Derive the TestFixtures from the same base class. They use but don't change the driver.






          share|improve this answer


























          • Thanks for taking time to educate me much appreciated. :) Even when i made the BaseClass as a TestFixture, im still unable to call the tests which are in the the different classes. Order is like this 1)OneTimeSetup 2)DerivedTestClass 3) OneTimeTeardown for every test case. Only when i put all test cases inside the same class it seems to be working in order as expected. which is 1) OneTimeSetup 2)all test cases and then 3) OneTimeTeardown. All i want is to open the application just once and run all test cases ONLY then close the application. how can i do it? thanks once again.

            – Im Nobody
            Nov 25 '18 at 13:25













          • As I wrote "in this case you need only the SetUpFixture, without the inheritance". I'll add a bit more to the answer...

            – Charlie
            Nov 25 '18 at 18:14











          • Let's try this... why do you want all your fixtures to use the same driver? I would be concerned that one test could malfunction in a way to break all the other tests.

            – Charlie
            Nov 25 '18 at 18:20











          • Thanks for writing back. SetUpFixture without inheritance, how the driver of the test cases will get initialize, it will throw a null object reference,wouldn't it?

            – Im Nobody
            Nov 25 '18 at 18:41











          • Yes you're right, but Company asked me to log into the application just once and execute all test cases then close the application. What would you suggest, im open to the good advice as im new to writing the test cases in c#. I quite welcome your advice.

            – Im Nobody
            Nov 25 '18 at 18:47











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53465501%2fonetimesetup-is-getting-invoked-for-each-test-i-thought-onetimesetup-is-like-bef%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2














          From the documentation, OneTimeSetup is called once before all tests in a TestFixture. Your example has two fixtures, so the set up is called twice. You need to have all your tests in the same Fixture.



          This behaviour is different to the old [Setup] on SetupFixture attribute, which ran once for all tests in a namespace.






          share|improve this answer


























          • Thanks a lot for the reply, how can i put them in the same Fixture, do i 've to bunk all test cases in the same class?

            – Im Nobody
            Nov 25 '18 at 9:00











          • or Make the baseClass as a TestFixture and extend all the test cases.

            – Im Nobody
            Nov 25 '18 at 9:09













          • I just now tried by making a BaseClass a TestFixture and extend two test cases, removed the TestFixture attribute from the both tests, still the Notepad is opening twice and tearing down twice. its working only when i put thetest cases inside the same class i.e Base Class. Order is like this 1)OneTimeSetup 2)DerivedTestClass 3) OneTimeTeardown for every test case. But when test cases are in the same class i.e BaseClass, oneTimeSetup and OneTimeTearDown occurring only once before and after all the test cases respectively.

            – Im Nobody
            Nov 25 '18 at 9:23













          • It's quite normal to have all the tests that rely on one setup in the same class, even if there are 100 of them.

            – iakobski
            Nov 25 '18 at 9:43











          • Thanks for the reply again, test cases are not really small, each test case as you may already know containing more than 50 steps at least. The code will be too lengthy and would become unreadable and very difficult to debug.

            – Im Nobody
            Nov 25 '18 at 9:56
















          2














          From the documentation, OneTimeSetup is called once before all tests in a TestFixture. Your example has two fixtures, so the set up is called twice. You need to have all your tests in the same Fixture.



          This behaviour is different to the old [Setup] on SetupFixture attribute, which ran once for all tests in a namespace.






          share|improve this answer


























          • Thanks a lot for the reply, how can i put them in the same Fixture, do i 've to bunk all test cases in the same class?

            – Im Nobody
            Nov 25 '18 at 9:00











          • or Make the baseClass as a TestFixture and extend all the test cases.

            – Im Nobody
            Nov 25 '18 at 9:09













          • I just now tried by making a BaseClass a TestFixture and extend two test cases, removed the TestFixture attribute from the both tests, still the Notepad is opening twice and tearing down twice. its working only when i put thetest cases inside the same class i.e Base Class. Order is like this 1)OneTimeSetup 2)DerivedTestClass 3) OneTimeTeardown for every test case. But when test cases are in the same class i.e BaseClass, oneTimeSetup and OneTimeTearDown occurring only once before and after all the test cases respectively.

            – Im Nobody
            Nov 25 '18 at 9:23













          • It's quite normal to have all the tests that rely on one setup in the same class, even if there are 100 of them.

            – iakobski
            Nov 25 '18 at 9:43











          • Thanks for the reply again, test cases are not really small, each test case as you may already know containing more than 50 steps at least. The code will be too lengthy and would become unreadable and very difficult to debug.

            – Im Nobody
            Nov 25 '18 at 9:56














          2












          2








          2







          From the documentation, OneTimeSetup is called once before all tests in a TestFixture. Your example has two fixtures, so the set up is called twice. You need to have all your tests in the same Fixture.



          This behaviour is different to the old [Setup] on SetupFixture attribute, which ran once for all tests in a namespace.






          share|improve this answer















          From the documentation, OneTimeSetup is called once before all tests in a TestFixture. Your example has two fixtures, so the set up is called twice. You need to have all your tests in the same Fixture.



          This behaviour is different to the old [Setup] on SetupFixture attribute, which ran once for all tests in a namespace.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 25 '18 at 17:05

























          answered Nov 25 '18 at 8:47









          iakobskiiakobski

          1564




          1564













          • Thanks a lot for the reply, how can i put them in the same Fixture, do i 've to bunk all test cases in the same class?

            – Im Nobody
            Nov 25 '18 at 9:00











          • or Make the baseClass as a TestFixture and extend all the test cases.

            – Im Nobody
            Nov 25 '18 at 9:09













          • I just now tried by making a BaseClass a TestFixture and extend two test cases, removed the TestFixture attribute from the both tests, still the Notepad is opening twice and tearing down twice. its working only when i put thetest cases inside the same class i.e Base Class. Order is like this 1)OneTimeSetup 2)DerivedTestClass 3) OneTimeTeardown for every test case. But when test cases are in the same class i.e BaseClass, oneTimeSetup and OneTimeTearDown occurring only once before and after all the test cases respectively.

            – Im Nobody
            Nov 25 '18 at 9:23













          • It's quite normal to have all the tests that rely on one setup in the same class, even if there are 100 of them.

            – iakobski
            Nov 25 '18 at 9:43











          • Thanks for the reply again, test cases are not really small, each test case as you may already know containing more than 50 steps at least. The code will be too lengthy and would become unreadable and very difficult to debug.

            – Im Nobody
            Nov 25 '18 at 9:56



















          • Thanks a lot for the reply, how can i put them in the same Fixture, do i 've to bunk all test cases in the same class?

            – Im Nobody
            Nov 25 '18 at 9:00











          • or Make the baseClass as a TestFixture and extend all the test cases.

            – Im Nobody
            Nov 25 '18 at 9:09













          • I just now tried by making a BaseClass a TestFixture and extend two test cases, removed the TestFixture attribute from the both tests, still the Notepad is opening twice and tearing down twice. its working only when i put thetest cases inside the same class i.e Base Class. Order is like this 1)OneTimeSetup 2)DerivedTestClass 3) OneTimeTeardown for every test case. But when test cases are in the same class i.e BaseClass, oneTimeSetup and OneTimeTearDown occurring only once before and after all the test cases respectively.

            – Im Nobody
            Nov 25 '18 at 9:23













          • It's quite normal to have all the tests that rely on one setup in the same class, even if there are 100 of them.

            – iakobski
            Nov 25 '18 at 9:43











          • Thanks for the reply again, test cases are not really small, each test case as you may already know containing more than 50 steps at least. The code will be too lengthy and would become unreadable and very difficult to debug.

            – Im Nobody
            Nov 25 '18 at 9:56

















          Thanks a lot for the reply, how can i put them in the same Fixture, do i 've to bunk all test cases in the same class?

          – Im Nobody
          Nov 25 '18 at 9:00





          Thanks a lot for the reply, how can i put them in the same Fixture, do i 've to bunk all test cases in the same class?

          – Im Nobody
          Nov 25 '18 at 9:00













          or Make the baseClass as a TestFixture and extend all the test cases.

          – Im Nobody
          Nov 25 '18 at 9:09







          or Make the baseClass as a TestFixture and extend all the test cases.

          – Im Nobody
          Nov 25 '18 at 9:09















          I just now tried by making a BaseClass a TestFixture and extend two test cases, removed the TestFixture attribute from the both tests, still the Notepad is opening twice and tearing down twice. its working only when i put thetest cases inside the same class i.e Base Class. Order is like this 1)OneTimeSetup 2)DerivedTestClass 3) OneTimeTeardown for every test case. But when test cases are in the same class i.e BaseClass, oneTimeSetup and OneTimeTearDown occurring only once before and after all the test cases respectively.

          – Im Nobody
          Nov 25 '18 at 9:23







          I just now tried by making a BaseClass a TestFixture and extend two test cases, removed the TestFixture attribute from the both tests, still the Notepad is opening twice and tearing down twice. its working only when i put thetest cases inside the same class i.e Base Class. Order is like this 1)OneTimeSetup 2)DerivedTestClass 3) OneTimeTeardown for every test case. But when test cases are in the same class i.e BaseClass, oneTimeSetup and OneTimeTearDown occurring only once before and after all the test cases respectively.

          – Im Nobody
          Nov 25 '18 at 9:23















          It's quite normal to have all the tests that rely on one setup in the same class, even if there are 100 of them.

          – iakobski
          Nov 25 '18 at 9:43





          It's quite normal to have all the tests that rely on one setup in the same class, even if there are 100 of them.

          – iakobski
          Nov 25 '18 at 9:43













          Thanks for the reply again, test cases are not really small, each test case as you may already know containing more than 50 steps at least. The code will be too lengthy and would become unreadable and very difficult to debug.

          – Im Nobody
          Nov 25 '18 at 9:56





          Thanks for the reply again, test cases are not really small, each test case as you may already know containing more than 50 steps at least. The code will be too lengthy and would become unreadable and very difficult to debug.

          – Im Nobody
          Nov 25 '18 at 9:56













          1














          Regarding SetUpFixture, the first answer is incorrect.



          SetUpFixture works exactly the same way as it has always worked. If you have a SetUpFixture, it's OneTimeSetUp and OneTimeTearDown methods will run once for all the fixtures under the same namespace.



          On the other hand, inside a TestFixture the OneTimeSetUp and OneTimeTearDown methods run just once for the fixture itself.



          In other words, the methods run "one time" within the particular scope in which they are used, namespace or fixture.



          The problem is that you are using the same class as both a SetUpFixture and as the base class for all your TestFixtures. That means if you have n test fixtures, it will run n + 1 times! That's what you are seeing.



          SetUpFixtures do not (have never had) anything to do with fixture inheritance. You should either make your class a SetUpFixture or a TestFixture base class. If, for whatever reason, you need both, then use two classes. In this case, you need only the SetUpFixture, without the inheritance.



          When to inherit from a base class: When you want the same code to execute many times, once for each fixture. Note that your base class, when used for one-time setup and teardown in this way, should normally not be marked as a TestFixture.



          When to use a SetUpFixture: When you want some code to execute only once, before every any fixture runs and again once after all the fixtures have run.



          In your example, you are using a SetUpFixture to control the timing of the initialization. You are using inheritance to allow you to share the driver. Problem is that the OneTimeSetUp in the base class is actually part of each test fixture and you don't want that.



          I'd be concerned about a hundred or so tests all using the same driver. I've seen some people use one driver per test and others one driver per fixture_. Using one for __everything implies that you are being extraordinarily careful that each test cleans up after itself and restores the driver to the same state. I doubt that's possible.



          However, as an exercise, here is how to do it if you really want to:
          1. Have a base class with nothing but the driver member.
          2. Derive the SetUpFixture from the base class and create / destroy the driver there.
          3. Derive the TestFixtures from the same base class. They use but don't change the driver.






          share|improve this answer


























          • Thanks for taking time to educate me much appreciated. :) Even when i made the BaseClass as a TestFixture, im still unable to call the tests which are in the the different classes. Order is like this 1)OneTimeSetup 2)DerivedTestClass 3) OneTimeTeardown for every test case. Only when i put all test cases inside the same class it seems to be working in order as expected. which is 1) OneTimeSetup 2)all test cases and then 3) OneTimeTeardown. All i want is to open the application just once and run all test cases ONLY then close the application. how can i do it? thanks once again.

            – Im Nobody
            Nov 25 '18 at 13:25













          • As I wrote "in this case you need only the SetUpFixture, without the inheritance". I'll add a bit more to the answer...

            – Charlie
            Nov 25 '18 at 18:14











          • Let's try this... why do you want all your fixtures to use the same driver? I would be concerned that one test could malfunction in a way to break all the other tests.

            – Charlie
            Nov 25 '18 at 18:20











          • Thanks for writing back. SetUpFixture without inheritance, how the driver of the test cases will get initialize, it will throw a null object reference,wouldn't it?

            – Im Nobody
            Nov 25 '18 at 18:41











          • Yes you're right, but Company asked me to log into the application just once and execute all test cases then close the application. What would you suggest, im open to the good advice as im new to writing the test cases in c#. I quite welcome your advice.

            – Im Nobody
            Nov 25 '18 at 18:47
















          1














          Regarding SetUpFixture, the first answer is incorrect.



          SetUpFixture works exactly the same way as it has always worked. If you have a SetUpFixture, it's OneTimeSetUp and OneTimeTearDown methods will run once for all the fixtures under the same namespace.



          On the other hand, inside a TestFixture the OneTimeSetUp and OneTimeTearDown methods run just once for the fixture itself.



          In other words, the methods run "one time" within the particular scope in which they are used, namespace or fixture.



          The problem is that you are using the same class as both a SetUpFixture and as the base class for all your TestFixtures. That means if you have n test fixtures, it will run n + 1 times! That's what you are seeing.



          SetUpFixtures do not (have never had) anything to do with fixture inheritance. You should either make your class a SetUpFixture or a TestFixture base class. If, for whatever reason, you need both, then use two classes. In this case, you need only the SetUpFixture, without the inheritance.



          When to inherit from a base class: When you want the same code to execute many times, once for each fixture. Note that your base class, when used for one-time setup and teardown in this way, should normally not be marked as a TestFixture.



          When to use a SetUpFixture: When you want some code to execute only once, before every any fixture runs and again once after all the fixtures have run.



          In your example, you are using a SetUpFixture to control the timing of the initialization. You are using inheritance to allow you to share the driver. Problem is that the OneTimeSetUp in the base class is actually part of each test fixture and you don't want that.



          I'd be concerned about a hundred or so tests all using the same driver. I've seen some people use one driver per test and others one driver per fixture_. Using one for __everything implies that you are being extraordinarily careful that each test cleans up after itself and restores the driver to the same state. I doubt that's possible.



          However, as an exercise, here is how to do it if you really want to:
          1. Have a base class with nothing but the driver member.
          2. Derive the SetUpFixture from the base class and create / destroy the driver there.
          3. Derive the TestFixtures from the same base class. They use but don't change the driver.






          share|improve this answer


























          • Thanks for taking time to educate me much appreciated. :) Even when i made the BaseClass as a TestFixture, im still unable to call the tests which are in the the different classes. Order is like this 1)OneTimeSetup 2)DerivedTestClass 3) OneTimeTeardown for every test case. Only when i put all test cases inside the same class it seems to be working in order as expected. which is 1) OneTimeSetup 2)all test cases and then 3) OneTimeTeardown. All i want is to open the application just once and run all test cases ONLY then close the application. how can i do it? thanks once again.

            – Im Nobody
            Nov 25 '18 at 13:25













          • As I wrote "in this case you need only the SetUpFixture, without the inheritance". I'll add a bit more to the answer...

            – Charlie
            Nov 25 '18 at 18:14











          • Let's try this... why do you want all your fixtures to use the same driver? I would be concerned that one test could malfunction in a way to break all the other tests.

            – Charlie
            Nov 25 '18 at 18:20











          • Thanks for writing back. SetUpFixture without inheritance, how the driver of the test cases will get initialize, it will throw a null object reference,wouldn't it?

            – Im Nobody
            Nov 25 '18 at 18:41











          • Yes you're right, but Company asked me to log into the application just once and execute all test cases then close the application. What would you suggest, im open to the good advice as im new to writing the test cases in c#. I quite welcome your advice.

            – Im Nobody
            Nov 25 '18 at 18:47














          1












          1








          1







          Regarding SetUpFixture, the first answer is incorrect.



          SetUpFixture works exactly the same way as it has always worked. If you have a SetUpFixture, it's OneTimeSetUp and OneTimeTearDown methods will run once for all the fixtures under the same namespace.



          On the other hand, inside a TestFixture the OneTimeSetUp and OneTimeTearDown methods run just once for the fixture itself.



          In other words, the methods run "one time" within the particular scope in which they are used, namespace or fixture.



          The problem is that you are using the same class as both a SetUpFixture and as the base class for all your TestFixtures. That means if you have n test fixtures, it will run n + 1 times! That's what you are seeing.



          SetUpFixtures do not (have never had) anything to do with fixture inheritance. You should either make your class a SetUpFixture or a TestFixture base class. If, for whatever reason, you need both, then use two classes. In this case, you need only the SetUpFixture, without the inheritance.



          When to inherit from a base class: When you want the same code to execute many times, once for each fixture. Note that your base class, when used for one-time setup and teardown in this way, should normally not be marked as a TestFixture.



          When to use a SetUpFixture: When you want some code to execute only once, before every any fixture runs and again once after all the fixtures have run.



          In your example, you are using a SetUpFixture to control the timing of the initialization. You are using inheritance to allow you to share the driver. Problem is that the OneTimeSetUp in the base class is actually part of each test fixture and you don't want that.



          I'd be concerned about a hundred or so tests all using the same driver. I've seen some people use one driver per test and others one driver per fixture_. Using one for __everything implies that you are being extraordinarily careful that each test cleans up after itself and restores the driver to the same state. I doubt that's possible.



          However, as an exercise, here is how to do it if you really want to:
          1. Have a base class with nothing but the driver member.
          2. Derive the SetUpFixture from the base class and create / destroy the driver there.
          3. Derive the TestFixtures from the same base class. They use but don't change the driver.






          share|improve this answer















          Regarding SetUpFixture, the first answer is incorrect.



          SetUpFixture works exactly the same way as it has always worked. If you have a SetUpFixture, it's OneTimeSetUp and OneTimeTearDown methods will run once for all the fixtures under the same namespace.



          On the other hand, inside a TestFixture the OneTimeSetUp and OneTimeTearDown methods run just once for the fixture itself.



          In other words, the methods run "one time" within the particular scope in which they are used, namespace or fixture.



          The problem is that you are using the same class as both a SetUpFixture and as the base class for all your TestFixtures. That means if you have n test fixtures, it will run n + 1 times! That's what you are seeing.



          SetUpFixtures do not (have never had) anything to do with fixture inheritance. You should either make your class a SetUpFixture or a TestFixture base class. If, for whatever reason, you need both, then use two classes. In this case, you need only the SetUpFixture, without the inheritance.



          When to inherit from a base class: When you want the same code to execute many times, once for each fixture. Note that your base class, when used for one-time setup and teardown in this way, should normally not be marked as a TestFixture.



          When to use a SetUpFixture: When you want some code to execute only once, before every any fixture runs and again once after all the fixtures have run.



          In your example, you are using a SetUpFixture to control the timing of the initialization. You are using inheritance to allow you to share the driver. Problem is that the OneTimeSetUp in the base class is actually part of each test fixture and you don't want that.



          I'd be concerned about a hundred or so tests all using the same driver. I've seen some people use one driver per test and others one driver per fixture_. Using one for __everything implies that you are being extraordinarily careful that each test cleans up after itself and restores the driver to the same state. I doubt that's possible.



          However, as an exercise, here is how to do it if you really want to:
          1. Have a base class with nothing but the driver member.
          2. Derive the SetUpFixture from the base class and create / destroy the driver there.
          3. Derive the TestFixtures from the same base class. They use but don't change the driver.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 25 '18 at 20:04

























          answered Nov 25 '18 at 12:54









          CharlieCharlie

          6,93411518




          6,93411518













          • Thanks for taking time to educate me much appreciated. :) Even when i made the BaseClass as a TestFixture, im still unable to call the tests which are in the the different classes. Order is like this 1)OneTimeSetup 2)DerivedTestClass 3) OneTimeTeardown for every test case. Only when i put all test cases inside the same class it seems to be working in order as expected. which is 1) OneTimeSetup 2)all test cases and then 3) OneTimeTeardown. All i want is to open the application just once and run all test cases ONLY then close the application. how can i do it? thanks once again.

            – Im Nobody
            Nov 25 '18 at 13:25













          • As I wrote "in this case you need only the SetUpFixture, without the inheritance". I'll add a bit more to the answer...

            – Charlie
            Nov 25 '18 at 18:14











          • Let's try this... why do you want all your fixtures to use the same driver? I would be concerned that one test could malfunction in a way to break all the other tests.

            – Charlie
            Nov 25 '18 at 18:20











          • Thanks for writing back. SetUpFixture without inheritance, how the driver of the test cases will get initialize, it will throw a null object reference,wouldn't it?

            – Im Nobody
            Nov 25 '18 at 18:41











          • Yes you're right, but Company asked me to log into the application just once and execute all test cases then close the application. What would you suggest, im open to the good advice as im new to writing the test cases in c#. I quite welcome your advice.

            – Im Nobody
            Nov 25 '18 at 18:47



















          • Thanks for taking time to educate me much appreciated. :) Even when i made the BaseClass as a TestFixture, im still unable to call the tests which are in the the different classes. Order is like this 1)OneTimeSetup 2)DerivedTestClass 3) OneTimeTeardown for every test case. Only when i put all test cases inside the same class it seems to be working in order as expected. which is 1) OneTimeSetup 2)all test cases and then 3) OneTimeTeardown. All i want is to open the application just once and run all test cases ONLY then close the application. how can i do it? thanks once again.

            – Im Nobody
            Nov 25 '18 at 13:25













          • As I wrote "in this case you need only the SetUpFixture, without the inheritance". I'll add a bit more to the answer...

            – Charlie
            Nov 25 '18 at 18:14











          • Let's try this... why do you want all your fixtures to use the same driver? I would be concerned that one test could malfunction in a way to break all the other tests.

            – Charlie
            Nov 25 '18 at 18:20











          • Thanks for writing back. SetUpFixture without inheritance, how the driver of the test cases will get initialize, it will throw a null object reference,wouldn't it?

            – Im Nobody
            Nov 25 '18 at 18:41











          • Yes you're right, but Company asked me to log into the application just once and execute all test cases then close the application. What would you suggest, im open to the good advice as im new to writing the test cases in c#. I quite welcome your advice.

            – Im Nobody
            Nov 25 '18 at 18:47

















          Thanks for taking time to educate me much appreciated. :) Even when i made the BaseClass as a TestFixture, im still unable to call the tests which are in the the different classes. Order is like this 1)OneTimeSetup 2)DerivedTestClass 3) OneTimeTeardown for every test case. Only when i put all test cases inside the same class it seems to be working in order as expected. which is 1) OneTimeSetup 2)all test cases and then 3) OneTimeTeardown. All i want is to open the application just once and run all test cases ONLY then close the application. how can i do it? thanks once again.

          – Im Nobody
          Nov 25 '18 at 13:25







          Thanks for taking time to educate me much appreciated. :) Even when i made the BaseClass as a TestFixture, im still unable to call the tests which are in the the different classes. Order is like this 1)OneTimeSetup 2)DerivedTestClass 3) OneTimeTeardown for every test case. Only when i put all test cases inside the same class it seems to be working in order as expected. which is 1) OneTimeSetup 2)all test cases and then 3) OneTimeTeardown. All i want is to open the application just once and run all test cases ONLY then close the application. how can i do it? thanks once again.

          – Im Nobody
          Nov 25 '18 at 13:25















          As I wrote "in this case you need only the SetUpFixture, without the inheritance". I'll add a bit more to the answer...

          – Charlie
          Nov 25 '18 at 18:14





          As I wrote "in this case you need only the SetUpFixture, without the inheritance". I'll add a bit more to the answer...

          – Charlie
          Nov 25 '18 at 18:14













          Let's try this... why do you want all your fixtures to use the same driver? I would be concerned that one test could malfunction in a way to break all the other tests.

          – Charlie
          Nov 25 '18 at 18:20





          Let's try this... why do you want all your fixtures to use the same driver? I would be concerned that one test could malfunction in a way to break all the other tests.

          – Charlie
          Nov 25 '18 at 18:20













          Thanks for writing back. SetUpFixture without inheritance, how the driver of the test cases will get initialize, it will throw a null object reference,wouldn't it?

          – Im Nobody
          Nov 25 '18 at 18:41





          Thanks for writing back. SetUpFixture without inheritance, how the driver of the test cases will get initialize, it will throw a null object reference,wouldn't it?

          – Im Nobody
          Nov 25 '18 at 18:41













          Yes you're right, but Company asked me to log into the application just once and execute all test cases then close the application. What would you suggest, im open to the good advice as im new to writing the test cases in c#. I quite welcome your advice.

          – Im Nobody
          Nov 25 '18 at 18:47





          Yes you're right, but Company asked me to log into the application just once and execute all test cases then close the application. What would you suggest, im open to the good advice as im new to writing the test cases in c#. I quite welcome your advice.

          – Im Nobody
          Nov 25 '18 at 18:47


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53465501%2fonetimesetup-is-getting-invoked-for-each-test-i-thought-onetimesetup-is-like-bef%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          404 Error Contact Form 7 ajax form submitting

          How to know if a Active Directory user can login interactively

          Refactoring coordinates for Minecraft Pi buildings written in Python