XUnit类治具(IClassFixture)被执行两次

问题描述 投票:2回答:1

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();

 }

[几天前,我编写了这段代码,它用来创建1个浏览器实例。我今天再次开始该项目,现在突然执行了两次夹具,并打开了两个单独的浏览器(这导致我的测试失败)。我以为IClassFixture应该只执行一次,就像NUnit中的[OneTimeSetUp]属性一样。为什么我的夹具执行两次?

[这会在我运行所有测试时发生(所有测试都运行ProfileTestHomeTest)。例如,如果我分别运行两个测试之一,则仅打开一个浏览器实例,测试通过。

我正在使用XUnit 2.4.0。

-编辑-

当我使用时:

VS 2019(运行所有测试):它同时打开2个浏览器,但失败。

VS 2019(调试所有测试):同时打开两个浏览器,但失败。

Jetbrain的Rider IDE(运行所有测试):它同时打开2个浏览器,但失败。

Jetbrain的Rider IDE(调试所有测试):它打开1个浏览器,直到HomeTest完成,然后打开另一个浏览器的ProfileTest,并且两个测试都通过了(包括LoginTest)。

这是最后的工作方式,以前我使用NUnit时就像这样。

c# selenium-webdriver xunit xunit.net xunit2
1个回答
0
投票

来自https://xunit.net/docs/shared-context#class-fixture

您可以使用xUnit.net的类夹具功能来共享一个对象实例在测试类中的所有测试中

注意在测试班中

如果您有两个分离的类HomeTestProfileTest,无论它们都是从同一个抽象类派生的,xUnit会将它们视为两个不同的测试类。

考虑改为使用Collection Fixtureshttps://xunit.net/docs/shared-context#collection-fixture

您可以使用xUnit.net的collection夹具功能来共享单个对象实例在多个测试类的测试中

© www.soinside.com 2019 - 2024. All rights reserved.