Once you have followed the Getting Started guide and have started to build and execute some tests against your project, the natural next step would be to add these tests to your build process to validate functionality or build sanity in an automated way. Eventually, you will want to wire up your tests to run from your CI/CD system such as Jenkins, and validate each build automatically. That process is outlined in one of our partner blogs here.


But before reaching that phase, you need to run your tests from a command line. This is to ensure they can run headlessly and in a consistent manner across supported build platforms and take in the necessary parameters to control execution modes and any external arguments needed to control test flow.


To do this, you will need to add the "NUnit Console" package to your test project, which is available via the NuGet package manager.



Next, you will need to make some minor additions to your test to allow parameters to be passed in via the command-line utility. An example of these is included in the Getting Started guide, with a comment indicating which lines to include:

public string testMode = TestContext.Parameters.Get("Mode", "IDE");
public string testHost = TestContext.Parameters.Get("Host", "localhost");
public string executablePath = TestContext.Parameters.Get("executablePath", @"C:\Users\user\Builds\UnityBuild.exe");

In this example, there are three parameters; testMode, testHost, and pathToExe. The TextContext.Parameters.Get method allows you to define a parameter name, which is passed in via the command line, as well as a default value in the event that no value is given.


You can then use these parameters in your test to control the way the test is run, for example:

if (executablePath != null && testMode == "standalone") // launches a standalone executable
{
ApiClient.Launch(pathToExe);
api.Wait(5000);
Console.WriteLine($"Launching standalone executable with PID: {PID}");
api.Connect(testHost, 19734, false, 30);
}
else if (executablePath == null && testMode == "IDE") // connects to the IDE and starts Play mode
{
api.Connect(testHost, 19734, true, 30);
}
else api.Connect(testHost, 19734, false, 30); // connects to an already running instance of the editor or standalone build, mobile device, etc...

In the code above, we have an if, else if, else block that compares these parameters with some known values, and either launch a standalone executable with the path provided or connects to the locally running Unity editor and enables Play mode or failing those two options simply connects to any running instance of the project on the local machine.

Additional parameters might be added like this to connect to a mobile device, set desired capabilities for an Appium server, or connect to a remote IP address.


Finally, to execute your test using the NUnit console runner, use the following syntax in reference to the previous examples. For Windows:

.\NewAPI_Toolkit_Test\packages\NUnit.ConsoleRunner.3.12.0\tools\nunit3-console.exe .\NewAPI_Toolkit_Test\NewAPI_Toolkit_Test\bin\Debug\NewAPI_Toolkit_Test.dll -testparam:Mode=standalone --testparam:pathToExe=C:\Users\neoev\Desktop\GDIOToolkit\GDIOToolkit.exe


and for macOS:

mono nunit3-console.exe ~/Documents/tests/unitytestcases/NewAPI_2DDemo_Test/NewAPI_2DDemo_Test/bin/Debug/NewAPI_2DDemo_Test.dll --testparam:Mode=standalone --testparam:pathToExe=/Users/shane/Desktop/2DDemo.app/Contents/MacOS/2DDemo

Additional execution options for the NUnit Console Runner can be found here.