【问题标题】:How to parameterized C# NUnit TestFixtures with multiple browsers如何使用多个浏览器参数化 C# NUnit TestFixtures
【发布时间】:2015-12-03 21:56:22
【问题描述】:

所以我对 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();
}

【问题讨论】:

  • 你有什么问题?你的样品不适合你吗?顺便说一句,欢迎来到 SO!
  • 谢谢!该示例适用于在多个浏览器中运行,我的问题是如何参数化固定装置,以便可以将不同的 URL 传递给测试。我们有几个不同的集成环境,我在其中工作,我想编写这些测试,所以当我启动它们时,我可以简单地将 URL1、URL2 或 URL3 传递给它,并让其余的测试针对该 URL 运行。跨度>

标签: c# webdriver nunit


【解决方案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)
    {
        // ...
    }
}

【讨论】:

    【解决方案2】:

    对您的问题最简单的答案是为您的测试方法使用[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() 
        {
    
        }
    }
    

    【讨论】:

    • 谢谢,这是有道理的。似乎很容易实现。讨厌征求其他问题。但是我目前被我在测试中遇到的一个问题所阻止,如果你不介意偷看的话。 stackoverflow.com/questions/34032608/….
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-10
    • 1970-01-01
    • 2015-12-27
    相关资源
    最近更新 更多