如何使用多个浏览器参数化 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()
{
}
}
所以我是 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()
{
}
}