In a previous article I went through how to unit test a K2 workflow. Now I have some more examples of testing a workflow from start to finish.
These unit tests just use the out-of-the-box K2 API and Visual Studio’s testing framework. They use an XML file to store test results and data such as the task SN and process instance ID.
The code was available to download from the original post.
Starting the workflow
To start a workflow we can use the following API:
public int StartProcess(string Folio, string ProcessName)
{
int ProcessInstanceId = 0;
SourceCode.Workflow.Client.Connection K2Conn = new SourceCode.Workflow.Client.Connection();
K2Conn.Open(servername);
SourceCode.Workflow.Client.ProcessInstance K2Proc = K2Conn.CreateProcessInstance(ProcessName);
K2Proc.Folio = Folio;
K2Conn.ImpersonateUser(ServiceAccount);
K2Conn.StartProcessInstance(K2Proc);
ProcessInstanceId = K2Proc.ID;
// ... try/catch/finally (closing the connection) trimmed
return ProcessInstanceId;
}This method returns the process instance ID. We can use it in a unit test and check that the value returned is greater than 0.
Test: start the workflow and check it has started
[TestMethod]
public void StartWorkflow_Success()
{
int actual = workflow.StartProcess(this.folio, this.ProcessName);
this.proceessInstance = actual;
Results.SaveResult(TestType.ProcessInstance,this.proceessInstance.ToString());
Assert.AreNotEqual(0, actual);
ProcInstId = this.proceessInstance;
}Check the workflow is running
Now that the workflow has started, we can run a method to check its current status.
public string GetWorkflowStatus(int processinstanceId)
{
string Result = string.Empty;
SourceCode.Workflow.Client.Connection K2Conn = new SourceCode.Workflow.Client.Connection();
K2Conn.Open(servername);
try
{
SourceCode.Workflow.Client.ProcessInstance K2Proc = K2Conn.OpenProcessInstance(processinstanceId);
switch (K2Proc.Status1)
{
case SourceCode.Workflow.Client.ProcessInstance.Status.Active:
{
Result = SourceCode.Workflow.Client.ProcessInstance.Status.Active.ToString();
break;
}
// ... Completed, Deleted, Error, New, Running and Stopped are handled the same way
}
}
// ... catch and finally trimmed
return Result;
}This method returns the current status, which tells us whether the workflow is running or has completed.
Test: check the workflow has started
[TestMethod]
public void StartWorkflow_Running_Success()
{
int.TryParse(Results.GetResult(TestType.ProcessInstance), out this.proceessInstance);
string actual = workflow.GetWorkflowStatus(this.proceessInstance);
Results.SaveResult(TestType.StartStatus, actual);
StringAssert.Equals(SourceCode.Workflow.Client.ProcessInstance.Status.Active.ToString(), actual);
}We could also take this same unit test and change SourceCode.Workflow.Client.ProcessInstance.Status.Active to ‘Error’ or ‘Completed’, so we can create tests to see whether the workflow has completed or gone into an error state.
Checking a task has been created
This method checks whether a task has been created for the particular process and instance. It goes through the worklist items and looks for one with a matching process instance ID.
worklistItems = wrkmgt.GetWorklistItems("", "", "", "", "", "", "");
foreach (SourceCode.Workflow.Management.WorklistItem worklistItem in worklistItems)
{
if (worklistItem.ProcInstID == ProcessInstanceId)
{
Result = true;
}
}Test: task found on the worklist
Returns true if a task has been found.
[TestMethod]
public void WorkList_TasksFound_Success()
{
Thread.Sleep(milliseconds);
int.TryParse(Results.GetResult(TestType.ProcessInstance), out this.proceessInstance);
Boolean actual = false;
actual = workflow.IsTaskFound(this.proceessInstance, this.ProcessName);
Assert.AreEqual(true, actual);
}Checking the correct number of tasks has been created
The GetTaskCount method uses the same loop, but counts the matching worklist items, so we can check the correct number of tasks was generated.
Test: correct number of tasks created
Returns the number of tasks, which we can compare against the number expected.
[TestMethod]
public void WorkList_CorrectNumberOfTasksCreated_Success()
{
int.TryParse(Results.GetResult(TestType.ProcessInstance), out this.proceessInstance);
int actual = 0;
actual = workflow.GetTaskCount(this.proceessInstance, this.ProcessName);
Results.SaveResult(TestType.TaskCount, actual.ToString());
Assert.AreEqual(this.TaskCount, actual);
}Getting the task details
This method takes the task and gets its details. Inside the same worklist loop, it builds up a list of tasks for the instance:
list.Add(new tasklist
{
Status = worklistItem.Status.ToString(),
Destination = worklistItem.Destination,
EventName = worklistItem.EventName,
ActInstDestID = worklistItem.ActInstDestID.ToString(),
ActivityName = worklistItem.ActivityName,
SerialNumber = (ProcessInstanceId + "_" + worklistItem.ActInstDestID)
});Test: check the correct task has been created
Returns the name of the activity, which we compare against the expected activity. It also saves the serial number and destination user for the next test.
[TestMethod]
public void WorkList_RetrieveTaskList_Success()
{
string Actual = string.Empty;
int.TryParse(Results.GetResult(TestType.ProcessInstance), out this.proceessInstance);
var task = workflow.GetTask(this.proceessInstance, this.ProcessName);
this.SerialNumber = task[0].SerialNumber;
Results.SaveResult(TestType.SerialNumber, this.SerialNumber);
this.DestinationUser = task[0].Destination;
Results.SaveResult(TestType.Destination, this.DestinationUser);
Actual = task[0].ActivityName;
Results.SaveResult(TestType.TaskActivity, Actual);
Assert.AreEqual(this.TaskActivity, Actual);
}Actioning a task
This actions the task and moves the workflow along.
public Boolean ActionTask(string action, string serialnumber, string destinationuser)
{
Boolean result = false;
SourceCode.Workflow.Client.Connection K2Conn = new SourceCode.Workflow.Client.Connection();
K2Conn.Open(servername);
K2Conn.ImpersonateUser(destinationuser);
SourceCode.Workflow.Client.WorklistItem K2WListItem = K2Conn.OpenWorklistItem(serialnumber);
K2WListItem.Actions[action].Execute();
result = true;
// ... try/catch/finally (closing the connection) trimmed
return result;
}Test: action the task
Returns true if the task was actioned successfully, which we compare against the expected result.
[TestMethod]
public void Task_GetActions_Sucess()
{
Boolean Actual = false;
this.SerialNumber = Results.GetResult(TestType.SerialNumber);
this.DestinationUser = Results.GetResult(TestType.Destination);
K2WorkflowMap.TaskDetails task = workflow.OpenTask(this.SerialNumber, this.DestinationUser)[0];
Actual = workflow.ActionTask(this.TaskAction, this.SerialNumber, this.DestinationUser);
Assert.AreEqual(true, Actual);
}This test only accounts for one slot. It could be modified to loop through the task list for the instance.
Test: check the task is complete
Using the same GetTaskCount method, this test checks that the number of tasks for the instance is now 0.
[TestMethod]
public void Task_CheckTaskComplete_Success()
{
int.TryParse(Results.GetResult(TestType.ProcessInstance), out this.proceessInstance);
int actual = 0;
actual = workflow.GetTaskCount(this.proceessInstance, this.ProcessName);
Results.SaveResult(TestType.TaskCount, actual.ToString());
Assert.AreEqual(0, actual);
}Once the main lot of testing is complete, we can check that the correct number of activities were executed and that the correct activities ran. Using the status method, we can also check whether the workflow has completed.
Getting the activities that ran
The GetActivities method gets all the activities that ran during the process instance, by calling the Activity_Instance SmartObject:
SourceCode.SmartObjects.Client.SmartObject smartObject = serverName.GetSmartObject("Activity_Instance");
smartObject.MethodToExecute = "List";
smartObject.Properties["ProcessInstanceID"].Value = processinstanceId;
SourceCode.SmartObjects.Client.SmartObjectList smoList = serverName.ExecuteList(smartObject);Test: compare the number of activities
This test compares the number of activities that actually ran for the instance against the number expected.
[TestMethod]
public void Activities_CompareNumberOfActivities_Success()
{
Thread.Sleep(milliseconds);
int.TryParse(Results.GetResult(TestType.ProcessInstance), out this.proceessInstance);
int NumberOfActivitiesRan = workflow.GetActivities(this.proceessInstance.ToString()).Count;
String[] activities = this.Activities.Split(';');
int count = 0;
foreach (var activity in activities)
{
count++;
}
Assert.AreEqual(count, NumberOfActivitiesRan);
}Activities that ran
This method checks the activities that ran against an array of expected activities.
String[] activities = Activities.Split(';');
int count = 0;
int match = 0;
foreach (var activity in activities)
{
var instanceactivity = instAct.Where(p => p.ActivityName == activity);
foreach (var act in instanceactivity)
{
match++;
}
count++;
}
if (count == match)
{ result = true; }Test: correct activities ran
This test returns true if the activities that ran match the activities in the string array.
[TestMethod]
public void Activities_CorrectActivitiesRan_Success()
{
Thread.Sleep(milliseconds);
int.TryParse(Results.GetResult(TestType.ProcessInstance), out this.proceessInstance);
Boolean actual = workflow.CompareActivities(this.proceessInstance, this.Activities);
Assert.IsTrue(actual);
}The original post finished with a long code appendix: the framework interface, the model classes, the XML results store and the full unit test class. The full listing is in the original post.