gpt4 book ai didi

c# - XUnit 类夹具 (IClassFixture) 正在执行两次

转载 作者:行者123 更新时间:2023-12-05 00:58:12 24 4
gpt4 key购买 nike

TextFixture 和测试

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

家庭测试

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

}

个人资料测试

 [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)时会发生这种情况。例如,如果我单独运行两个测试之一,那么只会打开 1 个浏览器实例并且测试通过。

我正在使用 XUnit 2.4.0。

- 编辑-

当我使用时:

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

VS 2019(调试所有测试):它同时打开2个浏览器并失败。

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

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

最后一个是它应该如何工作的,当我以前使用 NUnit 时它曾经是这样的。

最佳答案

来自 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

注意在一个测试类

在您的情况下,您有两个单独的类 HomeTestProfileTest,无论它们都派生自同一个抽象类,xUnit 将它们视为两个不同的测试类。

考虑改用Collection Fixtures
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

关于c# - XUnit 类夹具 (IClassFixture) 正在执行两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58505734/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com