在Xunit中进行Suite初始化之前(C#)

时间:2014-04-08 15:25:11

标签: c# xunit.net

更新了问题::

我想在使用xUnit.net运行测试套件中的任何测试之前设置数据。我尝试了IUseFixture,但它在运行任何测试类之前运行(而不是在测试套件之前)

考虑你的套件有2个测试类有2个测试/类,当试用IUseFixture时,SetFixture运行4次(每次测试一次)。

当需要同时运行所有四个测试时(每个测试套件一次),我需要运行一次的东西......,这是示例(使用WebDriver / C#/ xunit)::

第1课:

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

第2课:

 public class Class2 : IUseFixture<BrowserFixture>
{
    private IWebDriver driver;

    public void SetFixture(BrowserFixture data)
    {
        driver = data.InitiateDriver();
        Console.WriteLine("SetFixture Called");
    }

    public Class2()
    {
        Console.WriteLine("Test Constructor is Called");
    }

    [Fact]
    public void Test3()
    {
        driver.Navigate().GoToUrl("http://www.google.com");
        driver.FindElement(By.Id("gbqfq")).SendKeys("Testing");
    }

    [Fact]
    public void Test4()
    {
        driver.Navigate().GoToUrl("http://www.google.com");
        driver.FindElement(By.Id("gbqfq")).SendKeys("Testing again");
    }
}

Fixture Class ::

 public  class BrowserFixture
{
    private  IWebDriver driver;
     public BrowserFixture()
     {

         driver = new FirefoxDriver();

     }

    public  IWebDriver InitiateDriver()
    {
        return driver;
    }

}

现在,当我同时运行Test,Test2,Test3,Test4时,SetFixture被调用4次,我需要在任何测试运行之前运行一次(每个测试套件一次),或者我可以说任何只运行一次的东西TestSuite中测试类的初始化,类似于TestNG ::

中的BeforeSuite

http://testng.org/javadoc/org/testng/annotations/BeforeSuite.html http://testng.org/doc/documentation-main.html#annotations

1 个答案:

答案 0 :(得分:3)

灯具的ctor只运行一次: -

public class Facts : IUseFixture<MyFixture>
{
    void IUseFixture<MyFixture>.SetFixture( MyFixture dummy){}
    [Fact] void T(){}
    [Fact] void T2(){}
}

class MyFixture
{
    public MyFixture() 
    {
        // runs once
    }
}