3

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.

Drago
  • 1,755
  • 3
  • 19
  • 30
  • I don't think it's because of xUnit, It's a selenium problem. – itsMasoud Oct 22 '19 at 14:55
  • But if I change the code to make use of NUnit `[OneTimeSetUp]` rather than XUnit it does what I want. – Drago Oct 22 '19 at 15:03
  • After a close look I think your `TestFixture` method being recognised as a test too. Decorate it with a `Fact[(skip="some reason")]` attribute. – itsMasoud Oct 22 '19 at 15:15
  • Rider test executor will run tests one by one if debugger attached, in other cases different classes tests will be executed in parallel by default – Fabio Oct 23 '19 at 03:58

1 Answers1

9

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

Fabio
  • 31,528
  • 4
  • 33
  • 72