This is a migrated thread and some comments may be shown as answers.

nUnit TestFixtureSetup runs for each test

1 Answer 272 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
sreekanth n
Top achievements
Rank 1
sreekanth n asked on 29 Aug 2013, 12:59 PM
I am trying to run Telerik Framework Test using NUnit (through visual studio). And I need to initialize some variables before the tests run. I am using [TestFixtureSetUp] and [TestFixtureTearDown] for this. But it seems that these are being executed for each and every [Test] Method ([TestFixtureSetUp] and [TestFixtureTearDown] should be only once!)
  
    [TestFixtureSetUp]
    public void MyTestInitialize() 
    {
        SetUpAndTearDown setup = new SetUpAndTearDown();
        myDriver = setup.SetupTestEnvironment("ie");
    }
  
    [Test]
    public void Grid()
    {
        //code for gird
    }
  
    [Test]
    public void Box()
    {
        //code for Box
    }
  
    [TestFixtureTearDown]
    public void CleanUp()
    {
        SetUpAndTearDown teardown = new SetUpAndTearDown();
        teardown.TearDownTestEnvironment(myDriver);
    }
I am using NUnit - 2.6.2 Visul studio 2010 ultimate

1 Answer, 1 is accepted

Sort by
0
Velin Koychev
Telerik team
answered on 03 Sep 2013, 10:50 AM
Hello Sreekanth,

I am sorry to hear that  you are experiencing this issue.

You are right that if you have structured correctly your tests, what is inside [TestFixtureSetUp] and should be executed only once prior to executing any of the tests in the fixture as you can read in NUnit documentation. Only what is inside [SetUp] and [TearDown] will be executed for every test.

Here's a the way we recommend structuring NUnit tests:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using ArtOfTest.WebAii.Controls.HtmlControls;
using ArtOfTest.WebAii.Controls.HtmlControls.HtmlAsserts;
using ArtOfTest.WebAii.Core;
using ArtOfTest.WebAii.ObjectModel;
using ArtOfTest.WebAii.TestAttributes;
using ArtOfTest.WebAii.TestTemplates;
using ArtOfTest.WebAii.Win32.Dialogs;

using ArtOfTest.WebAii.Silverlight;
using ArtOfTest.WebAii.Silverlight.UI;

using NUnit.Framework;
using Core = NUnit.Core;

namespace ConfirmDialog
{
    /// <summary>
    /// Summary description for TelerikNUnitTest1
    /// </summary>
    [TestFixture]
    public class TelerikNUnitTest1 : BaseTest
    {

        #region [Setup / TearDown]

        /// <summary>
        /// Initialization for each test.
        /// </summary>
        [SetUp]
        public void MyTestInitialize()
        {
            #region WebAii Initialization

            // Initializes WebAii manager to be used by the test case.
            // If a WebAii configuration section exists, settings will be
            // loaded from it. Otherwise, will create a default settings
            // object with system defaults.
            //
            // Note: We are passing in a delegate to the NUnit's TestContext.Out.
            // WriteLine() method. This way any logging
            // done from WebAii (i.e. Manager.Log.WriteLine()) is
            // automatically logged to same output as NUnit.
            //
            // If you do not care about unifying the log, then you can simply
            // initialize the test by calling Initialize() with no parameters;
            // that will cause the log location to be picked up from the config
            // file if it exists or will use the default system settings.
            // You can also use Initialize(LogLocation) to set a specific log
            // location for this test.

            // Pass in 'true' to recycle the browser between test methods
            Initialize(false, new TestContextWriteLine(Console.Out.WriteLine));

            // If you need to override any other settings coming from the
            // config section or you don't have a config section, you can
            // comment the 'Initialize' line above and instead use the
            // following:

            /*

            // This will get a new Settings object. If a configuration
            // section exists, then settings from that section will be
            // loaded

            Settings settings = GetSettings();

            // Override the settings you want. For example:
            settings.Web.DefaultBrowser = BrowserType.FireFox;

            // Now call Initialize again with your updated settings object
            Initialize(settings, new TestContextWriteLine(Console.Out.WriteLine));

            */

            #endregion

            //
            // Place any additional initialization here
            //
        }

        /// <summary>
        /// Clean up after each test.
        /// </summary>
        [TearDown]
        public void MyTestCleanUp()
        {
            //
            // Place any additional cleanup here
            //

            #region WebAii CleanUp

            // Shuts down WebAii manager and closes all browsers currently running
            this.CleanUp();

            #endregion
        }

        /// <summary>
        /// Called after all tests in this class are executed.
        /// </summary>
        [TestFixtureTearDown]
        public void FixtureCleanup()
        {
            // This will shut down all browsers if
            // recycleBrowser is turned on. Else
            // will do nothing.
            ShutDown();
        }

        #endregion
    }
}



In order to help you best, please provide us a sample tests against a public website that we can test and give you a solution for this issue.

Looking forward to hearing from you.

Regards,
Velin Koychev
Telerik
Quickly become an expert in Test Studio, check out our new training sessions!
Test Studio Trainings
Tags
General Discussions
Asked by
sreekanth n
Top achievements
Rank 1
Answers by
Velin Koychev
Telerik team
Share this question
or