C# XUnit类Fixture(IClassFixture)正在执行两次
text夹具和测试C# XUnit类Fixture(IClassFixture)正在执行两次,c#,selenium-webdriver,xunit,xunit.net,xunit2,C#,Selenium Webdriver,Xunit,Xunit.net,Xunit2,text夹具和测试 public class TestFixture : IDisposable { public TestFixture() { var options = new ChromeOptions(); options.AddExcludedArgument("enable-automation"); options.AddAdditionalCapability("useAutomationExtension", t
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>
{
}
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();
}
ProfileTest
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>
{
}
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();
}
[TestCaseOrderer(PriorityOrderer.Name,PriorityOrderer.Assembly)]
公共密封类ProfileTest:AuthTest
{
[事实,优先权(0)]
public void OpenProfilePageTest()
{
var profile=GetPage();
profile.GoTo();
profile.IsAt();
}
[事实、优先权(1)]
public void LogoutTest()=>GetPage().Logout();
}
几天前,我编写了这段代码,它用于创建一个浏览器实例。我今天又开始了这个项目,现在突然这个装置被执行了两次,它打开了两个不同的浏览器(这导致我的测试失败)。我认为IClassFixture
应该只执行一次,就像NUnit中的[OneTimeSetUp]
属性一样。为什么我的固定装置执行两次
当我运行所有测试时会发生这种情况(所有测试都运行ProfileTest
和HomeTest
)。例如,如果我单独运行两个测试中的一个,那么只会打开一个浏览器实例,并且测试通过
我正在使用XUnit2.4.0
-编辑-
当我使用:
VS 2019(运行所有测试):它同时打开两个浏览器,但失败。VS 2019(调试所有测试):它同时打开两个浏览器并失败。
Jetbrain的Rider IDE(运行所有测试):它同时打开两个浏览器,但失败。
Jetbrain的Rider IDE(调试所有测试):它打开一个浏览器,直到
HomeTest
完成,然后打开另一个浏览器进行ProfileTest
,两个测试都通过(包括LoginTest
)。最后一个是它的工作原理,我以前使用NUnit时是这样的。From 您可以使用xUnit.net的类fixture特性来共享单个 测试类中所有测试中的对象实例 在测试类中注意 在您的例子中,您有两个独立的类
HomeTest
和ProfileTest
,不管它们是从同一个抽象类派生的,xUnit都将它们视为两个不同的测试类
考虑改用收集装置。
您可以使用xUnit.net的collection fixture功能来共享
多个测试类中的测试中的单个对象实例
从
您可以使用xUnit.net的类fixture特性来共享单个
测试类中所有测试中的对象实例
在测试类中注意
在您的例子中,您有两个独立的类HomeTest
和ProfileTest
,不管它们是从同一个抽象类派生的,xUnit都将它们视为两个不同的测试类
考虑改用收集装置。
您可以使用xUnit.net的collection fixture功能来共享
多个测试类中的测试中的单个对象实例
我不认为这是因为xUnit,这是一个selenium问题。但是,如果我更改代码以使用NUnit[OneTimeSetUp]
而不是xUnit,它会满足我的需要。仔细观察后,我认为您的TestFixture
方法也会被识别为测试。用事实[(skip=“some reason”)]
属性修饰它。如果附加了调试器,Rider test executor将逐个运行测试,在其他情况下,不同的类测试将由默认值并行执行。我不认为这是因为xUnit,这是一个selenium问题。但是如果我更改代码以使用NUnit[OneTimeSetUp]
而不是XUnit它做了我想要的。仔细看后,我认为您的TestFixture
方法也被认为是一种测试。用Fact[(skip=“some reason”)]
属性修饰它。如果附加了调试器,附加测试执行器将逐个运行测试,在其他情况下,默认情况下,不同的类测试将并行执行