-
Notifications
You must be signed in to change notification settings - Fork 16
Using the Testing Framework
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.
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.
The next step is to run the workflow. The TestRunner.TriggerWorkflow()
method creates a HTTP request for the workflow trigger and sends it to the workflow. This example uses HTTP POST but other HTTP methods can be used:
HttpResponseMessage workflowResponse = testRunner.TriggerWorkflow(FunctionToGetTriggerContent(), HttpMethod.Post);
The TriggerWorkflow()
method will complete when the workflow execution has completed.
There are a few overloads of the TriggerWorkflow()
method that allow you to set the following:
- HTTP request headers.
- The relative path, if the HTTP trigger is configured to use a relative path. The relative path must be URL-encoded by the test case, this is not done by the test runner.
The trigger URL for the workflow is logged to the test execution log. This example is for a workflow that uses a relative path of /thisIsMyContainer/thisIsMyBlob
:
Workflow trigger:
Name: manual
URL: POST http://localhost:7071/api/stateless-test-workflow/triggers/manual/invoke/thisIsMyContainer/thisIsMyBlob?api-version=2022-05-01&sp=%2Ftriggers%2Fmanual%2Frun&sv=1.0&sig=123ao4DL03l1c1C-KRqfsl9hr0G_ipOjg_h77STbAWQ
The example shows a trigger with the name of manual
, but the testing framework makes no assumptions about the name of the trigger, it can be set to any valid value.
- Home
- Using the Testing Framework
- Test Configuration
- Azurite
- Local Settings File
- Test Execution Logs
- Stateless Workflows
- Handling Workflow Dependencies
- Fluent API
- Automated testing using a DevOps pipeline
- Summary of Test Configuration Options
-
Example Mock Requests and Responses
- Call a Local Function action
- Invoke Workflow action
- Built-In Connectors:
- Service Bus
- SMTP
- Storage Account
- SQL Server