Buster
Buster

Reputation: 715

How to parameterized C# NUnit TestFixtures with multiple browsers

So I am pretty new to webdriver and nunit, I am building out regression tests for my legacy products and have the need to run my tests in multiple browsers and I would like them to be configurable to different integration environments. I have the multiple browsers working but am unsure how to parameterize the test fixtures.

[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();
}

Upvotes: 1

Views: 1624

Answers (2)

Denis Koreyba
Denis Koreyba

Reputation: 3718

The most simple answer to your question is to use [TestCase] attributes for your test methods.

Use the next example:

[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() 
    {

    }
}

Upvotes: 0

Patrick Quirk
Patrick Quirk

Reputation: 23747

I would just use the TestCaseSource attribute to specify the values to each test:

[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)
    {
        // ...
    }
}

Upvotes: 1

Related Questions