C# Automated Testing with NUnit

NUnit is a unit-test framework designed for all the .NET languages. It is written in C# and built to use many of the .NET language features. For example: it allows you to use custom attributes and other reflection-related capabilities. Please visit the official NUnit website for more documentation about the framework.

See our NUnit example repository for a simple example on how to run NUnit tests on TestingBot.

Prerequisites

To get started, you need to download and install these components:

TestingBot has created a sample Visual Studio project to help you get started with C# and .NET Selenium testing.

Download ZIP

You can import this project into Visual Studio and run the tests directly.

To run your tests with C# and .NET, you need to have the .NET SDK installed on your machine.

  • On Windows:
    • Download the .NET SDK installer and run it.
    • Or use the winget command: winget install Microsoft.DotNet.SDK.9
  • On MacOS: brew install dotnet
  • On Linux: sudo apt-get install dotnet-sdk-9.0

After installing the .NET SDK, you can create a new C# project using the command line:

dotnet new console -n SeleniumTest

Next, add the necessary Selenium WebDriver packages to your project:

cd SeleniumTest
dotnet add package Selenium.WebDriver
dotnet add package Selenium.Support
dotnet add package NUnit

Example NUnit test

Below is a simple example of an NUnit test that uses Selenium WebDriver to open Google and search for "TestingBot".

using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Remote;
using OpenQA.Selenium.Safari;

namespace NUnit_TestingBot_Sample;

public class Tests
{
    [TestFixture("chrome", "latest", "Windows 10")]
    public class TbNUnit_Test
    {
        private IWebDriver driver;
        private string browser;
        private string version;
        private string os;

        public TbNUnit_Test(String browser, String version, String os)
        {
            this.browser = browser;
            this.version = version;
            this.os = os;
        }

        [SetUp]
        public void Init()
        {
            DriverOptions browserOptions = new ChromeOptions()
            {
                BrowserVersion = this.version,
                PlatformName = this.os
            };

            var tbOptions = new Dictionary<string, string>
            {
                ["key"] = "api_key",
                ["secret"] = "api_secret",
                ["name"] = TestContext.CurrentContext.Test.Name,
                ["selenium-version"] = "3.14.0"
            };

            browserOptions.AddAdditionalOption("tb:options", tbOptions);

            driver = new RemoteWebDriver(new Uri("https://hub.testingbot.com/wd/hub"), browserOptions.ToCapabilities(), TimeSpan.FromSeconds(600));
        }

        [Test]
        public void GoogleTest()
        {
            driver.Navigate().GoToUrl("http://www.google.com");
            Assert.That(driver.Title, Is.EqualTo("Google"));
            IWebElement query = driver.FindElement(By.Name("q"));
            query.SendKeys("TestingBot");
            query.Submit();
        }

        [TearDown]
        public void CleanUp()
        {
            bool passed = TestContext.CurrentContext.Result.Outcome.Status == NUnit.Framework.Interfaces.TestStatus.Passed;
            try
            {
                // Logs the result to TestingBot
                ((IJavaScriptExecutor)driver).ExecuteScript("tb:test-result=" + (passed ? "passed" : "failed"));
            }
            finally
            {
                // Terminates the remote webdriver session
                driver.Quit();
                driver.Dispose();
            }
        }
    }
}

Specify Browsers & Devices

To let TestingBot know on which browser/platform/device you want to run your test on, you need to specify the browsername, version, OS and other optional options in the capabilities field.

var chromeOptions = new ChromeOptions()
{
    BrowserVersion = "latest",
    PlatformName = "Windows 10"
};

var tbOptions = new Dictionary<string, string>
{
    ["key"] = "api_key",
    ["secret"] = "api_secret",
    ["name"] = TestContext.CurrentContext.Test.Name,
    ["selenium-version"] = "3.14.0"
};

Depending on the browser you want to test on, you might need to specify different browser options in your code:

Testing Internal Websites

We've built TestingBot Tunnel, to provide you with a secure way to run tests against your staged/internal webapps.
Please see our TestingBot Tunnel documentation for more information about this easy to use tunneling solution.

The example below shows how to easily run a C# test with our Tunnel:

1. Download our tunnel and start the tunnel:

java -jar testingbot-tunnel.jar key secret

2. Adjust your test: instead of pointing to 'hub.testingbot.com/wd/hub' like the example above - change it to point to your tunnel's IP address.
Assuming you run the tunnel on the same machine you run your tests, change to 'localhost:4445/wd/hub'. localhost is the machine running the tunnel, 4445 is the default port of the tunnel.

This way your test will go securely through the tunnel to TestingBot and back:

using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Remote;
using OpenQA.Selenium.Safari;

namespace NUnit_TestingBot_Sample;

public class Tests
{
    [TestFixture("chrome", "latest", "Windows 10")]
    public class TbNUnit_Test
    {
        private IWebDriver driver;
        private string browser;
        private string version;
        private string os;

        public TbNUnit_Test(String browser, String version, String os)
        {
            this.browser = browser;
            this.version = version;
            this.os = os;
        }

        [SetUp]
        public void Init()
        {
            DriverOptions browserOptions = new ChromeOptions()
            {
                BrowserVersion = this.version,
                PlatformName = this.os
            };

            var tbOptions = new Dictionary<string, string>
            {
                ["key"] = "api_key",
                ["secret"] = "api_secret",
                ["name"] = TestContext.CurrentContext.Test.Name,
                ["selenium-version"] = "3.14.0"
            };

            browserOptions.AddAdditionalOption("tb:options", tbOptions);

            driver = new RemoteWebDriver(new Uri("http://localhost:4445/wd/hub"), browserOptions.ToCapabilities(), TimeSpan.FromSeconds(600));
        }

        [Test]
        public void GoogleTest()
        {
            driver.Navigate().GoToUrl("http://www.google.com");
            Assert.That(driver.Title, Is.EqualTo("Google"));
            IWebElement query = driver.FindElement(By.Name("q"));
            query.SendKeys("TestingBot");
            query.Submit();
        }

        [TearDown]
        public void CleanUp()
        {
            bool passed = TestContext.CurrentContext.Result.Outcome.Status == NUnit.Framework.Interfaces.TestStatus.Passed;
            try
            {
                // Logs the result to TestingBot
                ((IJavaScriptExecutor)driver).ExecuteScript("tb:test-result=" + (passed ? "passed" : "failed"));
            }
            finally
            {
                // Terminates the remote webdriver session
                driver.Quit();
                driver.Dispose();
            }
        }
    }
}

Run tests in Parallel

Parallel Testing means running the same test, or multiple tests, simultaneously. This greatly reduces your total testing time.

You can run the same tests on all different browser configurations or run different tests all on the same browser configuration. TestingBot has a large grid of machines and browsers, which means you can use our service to do efficient parallel testing. It is one of the key features we provide to greatly cut down on your total testing time.

NUnit provides a [assembly: Parallelizable(ParallelScope.Fixtures)] option to specify tests should run in parallel. More information is available in the NUnit Parallelizable documentation.

Queuing

Every plan we provide comes with a limited amount of parallel tests.
If you exceed the number of parallel tests assigned to your account, TestingBot will queue the additional tests (for up to 6 minutes) and run the tests as soon as slots become available.

Mark tests as passed/failed

To see if a test passed or not in our member area, or to send additional meta-data to TestingBot, you can use our API.

Please see the example below on how to notify TestingBot about the test success state:

[TearDown]
public void CleanUp()
{
    bool passed = TestContext.CurrentContext.Result.Outcome.Status == NUnit.Framework.Interfaces.TestStatus.Passed;
    try
    {
        // Logs the result to TestingBot
        ((IJavaScriptExecutor)driver).ExecuteScript("tb:test-result=" + (passed ? "passed" : "failed"));
    }
    finally
    {
        // Terminates the remote webdriver session
        driver.Quit();
    }
}

Other C# Framework examples

  • NUnit

    An unit testing framework that is open source written in C#.

  • PNunit

    With PNUnit you can run several tests in parallel.

  • SpecFlow

    SpecFlow allows you to run Automated .NET tests using Cucumber-compatible Gherkin syntax.

  • MSTest

    MSTest framework is a test framework which is included, by default, with Microsoft Visual Studio.

  • MbUnit

    MbUnit is a generative test unit framework, built for C sharp testing.

Was this page helpful?

Looking for More Help?

Have questions or need more information?
You can reach us via the following channels: