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

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


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


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.


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    }