Search code examples
c#selenium-webdriverxunitxunit.netxunit2

XUnit Class Fixture (IClassFixture) is being executed twice


TextFixture and Test

public class TestFixture : IDisposable
{
    public TestFixture()
    {
        var options = new ChromeOptions();
        options.AddExcludedArgument("enable-automation");
        options.AddAdditionalCapability("useAutomationExtension", true);
        WebDriver.Init("https://localhost:44335/", Browser.Chrome, options);
        WebDriver.GetDriver.Manage().Window.Maximize();
    }

    public void Dispose()
    {
        WebDriver.Close();
    }
}

public abstract class Test : IClassFixture<TestFixture>
{

}

AuthTest

public abstract class AuthTest : Test
{
    [Fact, Priority(-1)]
    public void LoginTest()
    {
        var home = GetPage<HomePage>();
        home.LoginModal.Open();
        home.LoginModal.EnterLoginText(new Login("user", "pw"));
        home.LoginModal.Login();
        GetPage<DashboardPage>().IsAt();
    }
}

HomeTest

[TestCaseOrderer(PriorityOrderer.Name, PriorityOrderer.Assembly)]
public sealed class HomeTest : AuthTest
{

}

ProfileTest

 [TestCaseOrderer(PriorityOrderer.Name, PriorityOrderer.Assembly)]
 public sealed class ProfileTest : AuthTest
 {
     [Fact, Priority(0)]
     public void OpenProfilePageTest()
     {
         var profile = GetPage<ProfilePage>();
         profile.GoTo();
         profile.IsAt();
     }
     [Fact, Priority(1)]
     public void LogoutTest() => GetPage<DashboardPage>().Logout();

 }

A few days ago I wrote this code and it used to create 1 browser instance. I started the project today again and now suddenly the fixture gets executed twice and it opens two seperate browsers (which causes my tests to fail). I thought the IClassFixture was supposed to only execute once like [OneTimeSetUp] attribute in NUnit. Why is my fixture executing twice?

This happens when I run all tests (all tests runsProfileTest and HomeTest). If I run for example one of the two tests individually, then just 1 browser instance opens and the test passes.

I'm using XUnit 2.4.0.

- EDIT -

When I use:

VS 2019 (run all tests): It opens 2 browsers at same time and fails.

VS 2019 (debug all tests): It opens 2 browsers at same time and fails.

Jetbrain's Rider IDE (run all tests): It opens 2 browsers at same time and fails.

Jetbrain's Rider IDE (debug all tests): It opens 1 browser till HomeTest finishes and then another browser for ProfileTest, and both tests pass (including LoginTest).

This last is how it is supposed to work and it used to be like this when I used NUnit before.


Solution

  • From https://xunit.net/docs/shared-context#class-fixture

    You can use the class fixture feature of xUnit.net to share a single object instance among all tests in a test class

    Notice in a test class

    In you case you have two separated classes HomeTest and ProfileTest, regardless of both are derived from same abstracted class, they are treated by xUnit as two different test classes.

    Consider to use Collection Fixtures instead.
    https://xunit.net/docs/shared-context#collection-fixture

    You can use the collection fixture feature of xUnit.net to share a single object instance among tests in several test class