C# 在Xunit中初始化套件之前(C)
更新问题:: 我想在使用xUnit.net运行测试套件中的任何测试之前设置数据。我尝试了IUseFixture,但它在运行任何测试类之前运行,而不是在测试套件之前运行 假设您的套件有2个测试类,每个类有2个测试,当使用IUseFixture进行尝试时,SetFixture每个测试运行4次 我需要的东西只有一次运行时,所有四个测试同时运行一次每个测试套件,下面是使用WebDriver/C/xunit的示例: 第1类:C# 在Xunit中初始化套件之前(C),c#,xunit.net,C#,Xunit.net,更新问题:: 我想在使用xUnit.net运行测试套件中的任何测试之前设置数据。我尝试了IUseFixture,但它在运行任何测试类之前运行,而不是在测试套件之前运行 假设您的套件有2个测试类,每个类有2个测试,当使用IUseFixture进行尝试时,SetFixture每个测试运行4次 我需要的东西只有一次运行时,所有四个测试同时运行一次每个测试套件,下面是使用WebDriver/C/xunit的示例: 第1类: public class Class1 : IUseFixture<Br
public class Class1 : IUseFixture<BrowserFixture>
{
private IWebDriver driver;
public void SetFixture(BrowserFixture data)
{
driver = data.InitiateDriver();
Console.WriteLine("SetFixture Called");
}
public Class1()
{
Console.WriteLine("Test Constructor is Called");
}
[Fact]
public void Test()
{
driver.Navigate().GoToUrl("http://www.google.com");
driver.FindElement(By.Id("gbqfq")).SendKeys("Testing");
}
[Fact]
public void Test2()
{
driver.Navigate().GoToUrl("http://www.google.com");
driver.FindElement(By.Id("gbqfq")).SendKeys("Testing again");
}
}
现在,当我同时运行Test、Test2、Test3、Test4时,SetFixture被调用4次,我需要在任何测试之前只运行一次的东西,每个测试套件运行一次,或者我可以说在测试套件中的任何测试类初始化之前只运行一次的东西,比如TestNG中的BeforeSuite::
夹具的ctor仅运行一次:-
public class Facts : IUseFixture<MyFixture>
{
void IUseFixture<MyFixture>.SetFixture( MyFixture dummy){}
[Fact] void T(){}
[Fact] void T2(){}
}
class MyFixture
{
public MyFixture()
{
// runs once
}
}
SetFixture每个测试运行一次,而不是类。测试类的ctor在每个测试中运行一次。如果我的答案不充分,您需要解释更多您试图实现的总体目标,而不是您已经声明的低级别要求。特别要确保检查JaredKells的答案。我记得这是真的-appdomains是为测试程序集动态创建的。虽然我很久以前就在深入研究这个主题,但大约在我为VS2010开发xvsr10 xUnit runner的时候,它很可能从那时起就没有改变过。@quetzalcatl Good spot。我相信V2中的收集装置也可以实现类似的效果。在您的问题中,BrowserFixture的ctor只运行一次。我真的不明白你的问题,也不明白这个问题或我的答案是如何改进或@MarkSeemann的原始答案的
public class BrowserFixture
{
private IWebDriver driver;
public BrowserFixture()
{
driver = new FirefoxDriver();
}
public IWebDriver InitiateDriver()
{
return driver;
}
}
public class Facts : IUseFixture<MyFixture>
{
void IUseFixture<MyFixture>.SetFixture( MyFixture dummy){}
[Fact] void T(){}
[Fact] void T2(){}
}
class MyFixture
{
public MyFixture()
{
// runs once
}
}