Is it possible to run [OneTimeSetup] tests for ALL?

I use NUnit to run some Selenium tests, and I have a little problem, I want to see if I can fix it. It happens that [OneTimeSetUp] and [OneTimeTearDown] start after each finish. I want to run [OneTimeSetUp] once when the tests run, and a stall that will be executed after ALL fixtures are complete.

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 } 
+17
source share
1 answer

OneTimeSetUpAttribute can be used in two ways.

In the first, he marks a method in a test fixture that runs once before any other tests in this fix. This is how you use it, inheriting from the base class. OneTimeSetUp, thanks to inheritance, appears in each of your derived fixtures, but it still runs several times, one for each fixture.

Second use in SetUpFixture . If you create a SetUpFixture in a specific namespace, its OneTimeSetUp method will run once before any other tests in that namespace. If you create a SetUpFixture outside of any namespace, its OneTimeSetUp will run once before any tests in the assembly.

UPDATE: Someone suggested that the last sentence would say “outside of any namespace containing TestFixture”. That would actually be wrong. SetUpFixture must be outside of any namespace in order to function at the assembly level. If there is a top-level namespace that contains all the test code, then you can also place SetUpFixture there, with roughly the same effect. But if it is in a namespace in which there are no tests , it will never be launched.

For more information about SetUpFixture, see the docs .

+22
source

Source: https://habr.com/ru/post/1264183/


All Articles