簡體   English   中英

如何使用多個瀏覽器對C#NUnit TestFixtures進行參數化

[英]How to parameterized C# NUnit TestFixtures with multiple browsers

因此,對於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();
}

我只是使用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)
    {
        // ...
    }
}

您問題的最簡單答案是為測試方法使用[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() 
    {

    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM