We recently started to use WebDriver (in favor of Selenium 1) for performing browser tests, using the NUnit framework. Since we want to run the tests in a variety of browsers, we define drivers for each and put them in a list during fixture set up:
[TestFixtureSetUp]
public void SetupTest()
{
// Load drivers
Drivers = new List<IWebDriver>
{
new ChromeDriver(),
...
};
In every single test we iterate through the list like this:
[Test]
public void SomeTest()
{
foreach (var driver in Drivers)
{
driver.Navigate().GoToUrl("...");
...
It feels wrong to do this in all the test methods. The test methods should not be concerned with what driver they should work on. Ideally we would have something like this:
public void SomeTest(IWebDriver driver)
{
driver.Navigate().GoToUrl("...");
...
One way we could solve this is by using TestCases:
[TestCase(new ChromeDriver())]
[TestCase(new FireFoxDriver())]
...
But this is a lot of duplication and shifts the problem of correct initialization of the drivers into the attributes of every single tests. Not really a gain.
Is there any way the NUnit framework can be told to execute the whole suite of tests and inject a different parameter to the individual tests in every run? Or is there any other good solution to this?
You should be able to use the TestCaseSourceAttribute. First create a commonly accessible class that provides the collection of web drivers:
Next, implement your web driver dependent unit tests like this:
Optionally, to reduce typing when implementing each unit test, also define a new Attribute class that inherits from
TestCaseSourceAttributeand that only implements a default constructor:Using the inherited
WedDriverSourceattribute, the unit tests can now be simplified to: