Back

Explore Courses Blog Tutorials Interview Questions
0 votes
3 views
in DevOps and Agile by (19.7k points)

I'm using NUnit to run some Selenium tests and I've got a minor issue I want to see if I can get corrected. What's happening is that the [OneTimeSetUp] and [OneTimeTearDown] is running after each fixture finishes. What I want is to run [OneTimeSetUp] once when the tests are started, and the teardown to run once ALL fixtures have finished.

TestBaseClass.cs

public class TestBaseClass

{

    [OneTimeSetUp]

    public void Init()

    {

         // Login

    }

    [OneTimeTearDown]

    public void TearDown()

    {

        Driver.Close();

    }

}

NavigationTests

[TestFixture]

public class NavigationTests : TestBaseClass

{

    // Tests

}

MainPageTests

[TestFixture]

public class MainPageTests : TestBaseClass

{

    // Tests

}

 

1 Answer

0 votes
by (62.9k points)

OneTimeSetUpAttribute has 2 uses.

 

In the 1st, it marks a method in a test fixture that's run once before any other tests in that fixture. that is how you're using it, by inheriting from a base class. The OneTimeSetUp appears, thanks to an inheritance, in each of your derived fixtures but it's still run multiple times, once for each fixture.

 

The second use is in a SetUpFixture. If you create a SetUpFixture in a particular namespace, it's OneTimeSetUp method can run once, before the other tests in this namespace. If you create the SetUpFixture outside of any namespace, then its OneTimeSetUp will run once before any tests in the assembly.

 

For more info about SetUpFixture, see the docs.

Browse Categories

...