How do I use Microsoft.Activities.UnitTesting?

Microsoft.Activities.UnitTesting is a test framework designed for unit testing Workflow Activities. In this walk through you will create a unit test for a simple activity.

Notes:
  • This test code requires Microsoft.Activities and Microsoft.Activities.UnitTesting >= v1.8.5
  • Sample code installed with the NuGet package under Microsoft.Activites.UnitTesting.9.9.9.9\Samples
  • Download the sample code from MSDN Code Gallery

Task 1 - Create and Configure Projects

  • Create a new project using the Workflow / Activity Library template named MathActivityLibrary
  • Delete Activity1.xaml you won't need it
  • Add a new Activity named Sum.xaml
  • Build the solution
  • Right click on the solution and select Add New Project...
  • Select Test / Test Project
  • Name the project MathActivityLibrary.Tests
  • Rename UnitTest1 (class and file) to MathActivityTest
  • Open MatchActivityTest.cs and add the following namespace directives
using Microsoft.Activities.UnitTesting;
  • Right click on MathActivityLibrary.Tests and select Add Reference...
  • Add a project reference to MathActivityLibrary
  • Add a reference to Microsoft.CSharp (to support the dynamic keyword)
  • Use NuGet to Install Microsoft.Activities.UnitTesting to MathActivityLibrary.Tests
packagemanagerConsole.png

Task 2 - Write the Sum activity

The behavior of the activity is as follows
Given
  • An activity named Sum
  • Which accepts two in arguments of type Int32 named Num1 and Num2
  • And returns an Int32 out argument named Result
When
  • The Sum activity is invoked
Then
  • The Result out argument will be the result of Num1 + Num2

Modify Sum.xaml

  • Open Sum.xaml in the workflow designer
  • Add two In arguments of type Int32 named Num1 and Num2
  • Add an Out argument of type Int32 named Result
  • Add an Assign Activity and set the properties with an intentional error of adding Num1 and Num1 together instead of Num1 and Num2
To Result
Value Num1+Num1

Task 3 - Write a Test

There are three categories of activities that you can test with Windows Workflow Foundation.
  1. Activities without bookmarks
  2. Activities that might have bookmarks
  3. Workflow Services

For activities without bookmarks you can use WorkflowInvokerTest to test them.
For activities that might have bookmarks you must use WorkflowApplicationTest. If you aren't sure, just use WorkflowApplicationTest for both.
For Workflow Services use WorkflowServiceTestHost

For this activity, we know it does not have bookmarks and it will probably never have bookmarks so we will use WorkflowInvokerTest because it is the simplest model
Our activity as the name suggests will add two numbers together and return the result in an out argument named sum. Before we write the test
we need to consider the behavior of the activity.
Given
  • An activity named Sum
  • Which accepts two in arguments of type Int32 named Num1 and Num2
  • And returns an Int32 out argument named Result
When
  • The Sum activity is invoked with Num1=1 and Num2 = 2
Then
  • The Result out argument will be 3
Write the test method
[TestMethod]
public void SumAddsTwoNumbers()
{
    // Arrange
    var activity = new Sum();

    var host = WorkflowInvokerTest.Create(activity);

    // InArguments is a dynamic object of type Microsoft.Activities.WorkflowArguments
    host.InArguments.Num1 = 1;
    host.InArguments.Num2 = 2;

    try
    {
        // Act
        host.TestActivity();

        // Assert
        // Note: The host automatically captures the out arguments
        host.AssertOutArgument.AreEqual("Result", 3);
    }
    finally
    {
        // Note: The host automatically captures tracking
        host.Tracking.Trace();
    }
}

Task 4 - Run the test

  • From the menu select Test / Run / All Tests In Solution
  • The test will fail
Test method MathActivityLibrary.Tests.MathActivityTest.TestMethod1 threw exception: 
Microsoft.Activities.UnitTesting.WorkflowAssertFailedException: AssertOutArgument.AreEqual failed. Expected:<3>. Actual:<2>.

Task 5 - Diagnose the Test Failure

  • In the Test Results window, right click on the test and select View Test Results Details
The Debug Trace will contain the tracking information formatted to be human readable. Review the tracking data to determine the problem.
Were the correct values passed as arguments to the workflow?
Tracking Record 2 shows the arguments passed to Sum Num1=1 and Num2=2
Did the expression in the assign activity get the correct result?
Tracking Record 4 shows the assign activity value property (which is the result of the expression) is 2. This value is incorrect so we know where the problem is.
*** Tracking data follows ***
0: WorkflowInstance "Sum" is Started at 09:14:57.9069
1: Activity [null] "null" scheduled child activity [1] "Sum" at 09:14:57.9069
2: Activity [1] "Sum" is Executing at 09:14:57.9069
{
    Arguments
        Num1: 1
        Num2: 2
}
3: Activity [1] "Sum" scheduled child activity [1.1] "Assign" at 09:14:57.9069
4: Activity [1.1] "Assign" is Executing at 09:14:57.9089
{
    Arguments
        Value: 2
}
5: Activity [1.1] "Assign" is Closed at 09:14:57.9089
{
    Arguments
        Value: 2
        To: 2
}
6: Activity [1] "Sum" is Closed at 09:14:57.9089
{
    Arguments
        Num1: 1
        Num2: 2
        Result: 2
}
7: WorkflowInstance "Sum" is Completed at 09:14:57.9089

Task 6 - Correct the Sum activity and test again

  • Open Sum.xaml
  • Select the Assign Activity and set the properties and correct the error
To Result
Value Num1+Num2
  • Run the test again. It will pass

Last edited Jul 28, 2011 at 7:55 PM by ronjacobs, version 15

Comments

No comments yet.