Is it possible to execute a method before and after all tests in the assembly?

32,637

Solution 1

If all your test fixtures are within the same namespace then you can use the [SetUpFixture] attribute to mark a class as the global setup and teardown. You can then put all your login/logout functionality in there.

NUNIT 2.x

namespace MyNamespace.Tests
{
    using System;
    using NUnit.Framework;

    [SetUpFixture]
    public class TestsSetupClass
    {
        [SetUp]
        public void GlobalSetup()
        {
            // Do login here.
        }

        [TearDown]
        public void GlobalTeardown()
        {
            // Do logout here
        }
    }
}

See: http://www.nunit.org/index.php?p=setupFixture&r=2.4

NUNIT 3.x

namespace MyNamespace.Tests
{
    using System;
    using NUnit.Framework;

    [SetUpFixture]
    public class TestsSetupClass
    {
        [OneTimeSetUp]
        public void GlobalSetup()
        {
            // Do login here.
        }

        [OneTimeTearDown]
        public void GlobalTeardown()
        {
            // Do logout here
        }
    }
}

See: https://github.com/nunit/docs/wiki/SetUpFixture-Attribute

Solution 2

Sure. That's what the [TestSetUp] and [TearDown] attributes are for. Don't confuse them with [TestFixtureSetUp] and [TestFixtureTearDown], which are executed before the first test and after the last.

Solution 3

Before executing each test cases [SetUp] section will executed

after completed the execution of each test cases [TearDown] section will executed.

if we want to initialize variables we often write in [SetUp] section like a constructor

if we want to dispose any object we often write in [TearDown] section

    [SetUp]
    protected void SetUp()
    {
             //initialize objects
    }

    [TearDown]
    public void TearDown()
    {
       //dispose objects
    }

Solution 4

The closest thing in nunit is the SetupFixture attribute, which allows you to tag a class to do setup/teardown for all test fixtures in a namespace;

The SetUp method in a SetUpFixture is executed once before any of the fixtures contained in its namespace. The TearDown method is executed once after all the fixtures have completed execution.

Share:
32,637
boger
Author by

boger

I am a C#, SOLID, TDD, Web Developer, Culture builder, AB testing, Analytics and Data Driven enthusiasts.

Updated on October 25, 2020

Comments

  • boger
    boger over 3 years

    I would like to built an nunit project for selenium ui automation. I would like to sign in to the site before running all tests (all of them) and to close the browser after running all tests (all of them).

    I can't use the SetUp since it related to fixtures and I want to do it before and after everything.

    Do you know who to execute it?


    I'm familiar with the SetUp and TearDown attribute. Let me explain it again.

    I need some logic to be executed before all tests from all fixtures starts (AKA - First test in the entire assembly) and also some logic to be executed after all tests from all fixtures ended (AKA - Last test in the entire assembly).