Skip to content

Using the Testing Framework

Mark Abrams edited this page Jun 12, 2023 · 6 revisions

The WorkflowTestBase class is an abstract base class that contains functionality to set up and configure the testing framework. All test classes should inherit from this base class. This example uses test attributes for MSTest to define a test class and the test initialization and clean-up methods:

[TestClass]
public class MyWorkflowTest : WorkflowTestBase
{
    [TestInitialize]
    public void TestInitialize()
    {
        Initialize("../../../../LogicAppUnit.Samples.LogicApps", "my-test-workflow");
    }

    [ClassCleanup]
    public static void CleanResources()
    {
        Close();
    }
}

The Initialize() method is used to configure a workflow for testing. The path to the Logic App's root folder and the name of the workflow are passed as parameters. The actions performed by this method to prepare the workflow for testing are described in later sections.

The Close() method is used to free up the resources used by the testing framework, once all tests in the test class have completed.

Setting up a Test

A workflow test is executed using an implementation of ITestRunner. This is created using the CreateTestRunner() method from the base class:

[TestMethod]
public void WorkflowTest()
{
    using (ITestRunner testRunner = CreateTestRunner())
    {

An instance of ITestRunner should only be used for a single test.

The next step is to configure the responses for the requests that are sent to the mock HTTP server, using the TestRunner.AddApiMocks() property. This example mocks the responses for workflow actions that connect to SQL Server and Service Bus:

// Mock the SQL and Service Bus actions and customize responses
// For both types of actions, the URI in the request matches the action name
testRunner.AddApiMocks = (request) =>
{
    HttpResponseMessage mockedResponse = new HttpResponseMessage();
    if (request.RequestUri.AbsolutePath == "/Execute_Query_to_get_Language_Name")
    {
        mockedResponse.RequestMessage = request;
        mockedResponse.StatusCode = HttpStatusCode.OK;
        mockedResponse.Content = ContentHelper.CreateJsonStringContent(GetSqlExecuteResponseContent());
    }
    else if (request.RequestUri.AbsolutePath == "/Send_message_to_Topic")
    {
        // No response content for Service Bus actions
        mockedResponse.RequestMessage = request;
        mockedResponse.StatusCode = HttpStatusCode.OK;
    }
    return mockedResponse;
};

The ContentHelper class is part of the testing framework and contains methods that are useful when creating HTTP content (JSON, XML and plain text) for the mocked responses.

Creating Call-Back URLs

You may have a workflow which receives a message that contains a callback URL and the workflow then makes a request using the callback URL. For example, a workflow may be triggered by an event notification, and the workflow uses the callback URL in the notification to read the details of the changed object.

The callback URL must be a URL pointing at the mock HTTP server, otherwise the test framework cannot "see" the request (because it will be sent somewhere else). To implement the test correctly, the base URL and port in the callback URL must refer to the mock HTTP server. You can get the URL for the mock HTTP server using the WorkflowTestBase.MockTestWorkflowHostUri property.

This example shows a method in a test class (that inherits from WorkflowTestBase) that creates an event trigger. The code uses the MockTestWorkflowHostUri property and string interpolation to create a value for resourceURI which is a callback URL:

public static StringContent CreateTriggerRequest()
{
    return ContentHelper.CreateJsonStringContent(new
    {
        id = "019aaf01-19ee-424c-94f0-ae5c00a8e677",
        correlationId = "9b6f8da4-13eb-48ac-851c-ae5c00a8e671",
        sourceSystem = "SystemOne",
        timestamp = "2022-03-18T10:14:56.8012126Z",
        type = "CustomerCreated",
        customerId = 12345,
        resourceId = "12345",
        resourceURI = $"{MockTestWorkflowHostUri}/api/v2/customer/12345"
    });
}

The value of the MockTestWorkflowHostUri property depends on the platform that is being used (Windows, Linux or MacOS). For Windows it will be http://<machine name>:7075, for Linux and MacOS it will be http://localhost:7075.

Clone this wiki locally