Dra*_*ago 3 c# xunit xunit.net selenium-webdriver xunit2
文本夹具和测试
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>
{
}
Run Code Online (Sandbox Code Playgroud)
验证测试
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();
}
}
Run Code Online (Sandbox Code Playgroud)
家庭测试
[TestCaseOrderer(PriorityOrderer.Name, PriorityOrderer.Assembly)]
public sealed class HomeTest : AuthTest
{
}
Run Code Online (Sandbox Code Playgroud)
轮廓测试
[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();
}
Run Code Online (Sandbox Code Playgroud)
几天前,我编写了这段代码,它用于创建 1 个浏览器实例。我今天再次开始该项目,现在夹具突然执行了两次,并打开了两个单独的浏览器(这导致我的测试失败)。我认为IClassFixture应该像 NUnit 中的属性一样只执行一次[OneTimeSetUp]。为什么我的装置执行了两次?
当我运行所有测试(所有测试都运行ProfileTest和HomeTest)时会发生这种情况。例如,如果我单独运行两个测试之一,则仅打开 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
您可以使用 xUnit.net 的类固定功能在测试类中的所有测试之间共享单个对象实例
测试课中的通知
在你的情况下,你有两个独立的班级HomeTest,并且ProfileTest,无论两者都是从同一个抽象类派生的,xUnit 都会将它们视为两个不同的测试类。
考虑使用Collection Fixtures代替。
https://xunit.net/docs/shared-context#collection-fixture
您可以使用 xUnit.net 的集合夹具功能在多个测试类中的测试之间共享单个对象实例
| 归档时间: |
|
| 查看次数: |
13937 次 |
| 最近记录: |