Passing a browser as a parameter to the NUnit console runner

Whenever we decide to execute Selenium tests based on the NUnit, we always have several options to choose from (depends on a project, infrastructure and framework): to run tests from Visual Studio or any other IDE (1), to run tests locally from the command line (2), to run tests on a continuous integration system (3). The last two are almost the same because we need to call NUnit console test runner for both cases. The difference is only that for a continuous integration solution we need to setup an additional build step and run NUnit console from there. But it’s not a big deal. For example, here how it looks for TeamCity CI. Jenkins CI has almost the same approach, by the way.

The command line approach can bring some additional benefits. For example, we can easily pass a required browser to our Selenium WebDriver tests and thus manage to run different builds with tests on different browsers. Let’s see how it can be done through the NUnit 3 console test runner.

Challenge:

To provide a solution which helps to pass a necessary browser to Selenium WebDriver tests through the NUnit.

Remember, not so long ago I posted the article about the NUnit TestContext properties? Except the properties described in that post, TestContext provides access to its static Parameters member – the object of the TestParameters class. The TestParameters class itself holds any named parameters supplied to a test run and provides methods for accessing these parameters. Here how it looks like in the code:

Apparently, we need to pass the named Browser parameter with some value to the nunit3-console. The simplest example of the command with passing a parameter looks like:

The command above assumes that we have the nunit3-console binary location added to the PATH system variable. Otherwise, we need to specify the absolute path to the nunit3-console.exe. Don’t forget also to specify the absolute path to the DLL with the Selenium tests, if it’s not in the same folder with the nunit3-console binary.

Okay, the preparation is done! Let’s create a test class with some dummy test case and see how it can look like. For example:

As displayed in the code above, we can obtain the passed parameter pretty easy and then handle as it pleases. NUnit parameters are strings by design, so please take that into account while handling them in your code. For example, I created the very simple fabric which takes a browser string and returns an appropriate browser driver – BrowserProvider.CreateBrowser(browser). But you are free to do whatever you want there and implement your own fabrics or use third-party frameworks.

That’s all for now! Explore NUnit and try new solutions. Happy testing!

Like this post?

Subscribe to updates from my blog, if you don't want to miss more interesting future posts and materials

Please check your email and confirm subscription

Pin It on Pinterest

Share This