如何使用多个浏览器参数化C#NUnit TestFixtures

时间:2015-12-01 18:38:42

标签: c# webdriver nunit

所以我对webdriver和nunit都很陌生,我正在为我的旧产品构建回归测试,并且需要在多个浏览器中运行我的测试,并且我希望它们可以配置到不同的集成环境。我有多个浏览器工作但不确定如何参数化测试夹具。

[TestFixture(typeof(FirefoxDriver))]
[TestFixture(typeof(ChromeDriver))]
[TestFixture(typeof(InternetExplorerDriver))]
public class UnitTest1<TWebDriver> where TWebDriver: IWebDriver, new()
{
PTGeneral General;
[TestFixtureSetUp]
public void SetUp()
{
General = new PTGeneral();
General.Driver = new TWebDriver();
}

2 个答案:

答案 0 :(得分:1)

我只想使用TestCaseSource属性为每个测试指定值:

[TestFixture(typeof(FirefoxDriver))]
[TestFixture(typeof(ChromeDriver))]
[TestFixture(typeof(InternetExplorerDriver))]
public class UnitTest1<TWebDriver> where TWebDriver: IWebDriver, new()
{
    // ...

    public IEnumerable<string> UrlsToTest
    {
        get
        {
            yield return "http://www.example.com/1";
            yield return "http://www.example.com/2";
            yield return "http://www.example.com/3";
        }
    }

    [TestCaseSource("UrlsToTest")]
    public void Test1(string url)
    {
        // ...
    }

    [TestCaseSource("UrlsToTest")]
    public void Test2(string url)
    {
        // ...
    }
}

答案 1 :(得分:0)

您问题的最简单答案是为您的测试方法使用[TestCase]属性。

使用下一个示例:

[TestFixture("sendSomethingToConstructor")]
class TestClass
{
    public TestClass(string parameter){}

    [TestCase(123)] //for parameterized methods
    public void TestMethod(int number){}

    [Test] //for methods without parameters
    public void TestMethodTwo(){}

    [TearDown]//after each test run
    public void CleanUp()
    {

    }

    [SetUp] //before each test run
    public void SetUp() 
    {

    }
}