Getting started

This guide provides a quick tutorial on setting up and running an OpenTAP project using the OpenTAP Runner and NATS Server. The tutorial will walk you through installing necessary packages, setting up a basic C# Console project, and interacting with the Runner.

Prerequisites

  • An OpenTAP installation

Starting the Runner

In an OpenTAP installation, install the two OpenTAP Packages, “Runner” and “NATS Server”:

./tap image install "Runner,NATS Server" --merge

Then start the Runner to start the OpenTAP Runner and the NATS Server:

./tap runner start

The NATS Server process will by default start on nats://127.0.0.1:20111 and the Runner will listen on the OpenTap.Runner.{MachineName} subject.

Creating a C# Console Project

  1. Create a new C# Console project:
mkdir MyApp
cd MyApp
dotnet new console
  1. Add the Runner Client package:
dotnet add package OpenTAP.Runner.Client --version 2.0.2
  1. Modify Program.cs to interact with the Runner. Add the following code:
using NATS.Client;
using OpenTap.Runner.Client;

// Create a NATS connection
IConnection connection = new ConnectionFactory().CreateConnection("nats://127.0.0.1:20111");
// Create a Runner Client
RunnerClient runnerClient = new RunnerClient(Environment.MachineName, connection);
// Create a Session on the Runner
SessionClient sessionClient = runnerClient.StartSession();
sessionClient.ConnectSessionLogs(handleSessionLogs);

void handleSessionLogs(LogList logList)
{
    foreach (var log in logList.Logs)
        Console.WriteLine($"{log.Source,-12}: {log.Message}");
}

// Get the available step types (Similar to "Add new step dialog")
var stepTypes = sessionClient.GetStepTypes();
// Get the current testplan from the session
var testplan = sessionClient.GetTestPlan(null);
// Add a delay step to the testplan
testplan.ChildTestSteps.Add(stepTypes.FirstOrDefault(s => s.TypeDisplay.Name == "Delay"));
// Update the testplan on the session
testplan = sessionClient.SetTestPlan(testplan);
// Give the TestPlan a name
sessionClient.SetTestPlanName("My Testplan");
// Start the testplan
RunStatus runStatus = sessionClient.RunTestPlan(new List<Parameter>());
// Wait for the testplan to finish
while (runStatus.ExecutionState != ExecutionState.Idle)
{
    runStatus = sessionClient.GetStatus();
    Thread.Sleep(250);
}
// Shutdown the session
runnerClient.ShutdownSession(sessionClient.Id);

This code connects, starts a Session, connects to sessionlogs, creates a testplan and executes it. After execution finishes, the session is closed.

  1. Run the project:
dotnet run

The expected output of dotnet run:

TestPlan    : -----------------------------------------------------------------
TestPlan    : Starting TestPlan 'My Testplan' on 04/05/2023 14:36:25, 1 of 1 TestSteps enabled.
TestPlan    : Saved Test Plan XML [1.66 ms]
Settings    : No settings file exists for TestPlanRunPackageParameterMonitor. A new instance with default values has been created. [200 us]
TestPlan    : PrePlanRun Methods completed [2.46 ms]
TestPlan    : "Delay" started.
TestPlan    : "Delay" completed. [107 ms]
TestPlan    : Test step runs finished. [119 ms]
Summary     : ----- Summary of test plan started 04/05/2023 14:36:25 -----
Summary     :  Delay                                             107 ms
Summary     : ------------------------------------------------------------
Summary     : -------- Test plan completed successfully in 199 ms --------

With these steps, you have successfully set up and run an application that remotely executed a testplan using the OpenTAP Runner and NATS Server.

We can now stop the OpenTAP Runner (and NATS Server) by sending CTRL+C in the command prompt with the Runner.