Run Perfecto Lab tests with NUnit in Visual Studio

The Perfecto templates support Microsoft Visual Studio Unit Testing out of the box. If you like some features like parallel testing and data-driven tests, you might want to give NUnit a try. This section explains how to transform a default Perfecto Remote Webdriver to NUnit, and how to execute tests using the Visual Studio Test Explorer.

The assumption is that you have already installed the Perfecto Lab Extension. If not, perform the steps in Use the Perfecto Lab windows in Visual Studio.

Important: This document includes references to third-party products, NUnit, Microsoft Visual Studio, and Microsoft NuGet. The user interface and usage of third-party products are subject to change without notice. For the latest published information about NUnit, see https://docs.nunit.org. For the latest published information about Microsoft Visual Studio, see https://learn.microsoft.com/en-us/visualstudio. For the latest published information about Microsoft NuGet, see https://learn.microsoft.com/en-us/nuget.

1 | Create a new Perfecto Selenium project in Visual Studio

Let's start by creating a new VS project.

  1. Click File > New > Project.
  2. Under Installed > Templates > Visual C# > Test > Perfecto Lab Selenium Test Project and select the Perfecto Lab Selenium Test Project.

When the project is installed, we see attributes like [TestClass][TestInitialize][TestCleanup] and [TestMethod], which are the test attributes of the MS UnitTesting.

2 | Add NUnit with NuGet

Now, let's add NUnit to the solution. The easiest way to do this by using NuGet.

At a later point, you may also want to install NUnit.Console or NUnit.ConsoleRunner. However, this is not necessary for this example.

  1. Click Tools > NuGet Package Manager > Manage NuGet Packages for Solution.

  2. Navigate to Browse at the top, search for NUnit, select the plain NUnit package, and install it to your project by selecting the project (for example PerfectoLabSelenium_SimpleNUnit) and clicking install.

    This will install the latest stable release.

  3. To prevent issues like tests not being recognized by the NUnit Test Explorer, also install the Test Adapter to Extensions and Updates, as follows:

    1. Go to Tools > Extensions and Updates.

    2. On the left, select Online.

    3. Search for NUnit Test Adapter and install it.

      NUnit is now ready to use.

3 | Substitute NUnit for MS UnitTesting 

Back at our project's RemoteWebDriverTest.cs, we can now substitute Nunit for MS UnitTesting.

Copy
// using Microsoft.VisualStudio.TestTools.UnitTesting;
using NUnit.Framework;

NUnit uses slightly different attributes to tell the framework what our test suite is, what the actual test is. and so on.

To substitute the original attributes with NUnit attributes:

  1. Replace:
    • [TestClass] with [TestFixture]
    • [TestInitialize] with [SetUp]
    • [TestCleanup] with [TearDown]
    • [TestMethod] with [TestCase]
  2. To parse all attributes [Test] or [TestCase] so they will be visible in the Test Explorer, click Build > BuildSolution.

    You are now ready to start the test with NUnit.

4 | Test with NUnit

To test with NUnit:

  1. Add your cloud credentials to the PerfectoOpenConnection() method.
  2. Execute the test by doing any of the following. This should result in a passed test in the Test Explorer (if the connection was correct, because the test itself does not contain any actions yet).
    • Press CTRL-R followed by A
    • Click Test > Run > AllTests.
    • In the TestExplorer, click Run All.  
  3. Add the line Console.WriteLine(reportUrl); to the TearDown to have the output be part of the test results when you click Output when the test is highlighted. For example:

    Copy
    Console.WriteLine(reportUrl);
    driver.Close();