boger
boger

Reputation: 623

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

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).

Upvotes: 45

Views: 48477

Answers (4)

matthewrdev
matthewrdev

Reputation: 12190

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

Upvotes: 69

Sumeshk
Sumeshk

Reputation: 1988

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
    }

Upvotes: 3

Joachim Isaksson
Joachim Isaksson

Reputation: 181077

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.

Upvotes: 3

Ross Patterson
Ross Patterson

Reputation: 9569

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.

Upvotes: 13

Related Questions