I have posted a few articles on testing in K2. We have looked at unit testing, testing SmartForms and, just recently, more examples of testing a workflow. After writing that last article I realised something was missing: what about building a workflow testing tool inside a SmartForm? It’s all well and good that we can write unit tests for a workflow, but that relies on the workflow developer being able to write code. The whole idea of K2 is to build low-code, no-code solutions, so there should be a way of doing it without the developer writing code.

There are a number of tools on the market, and they do an absolutely fantastic job of testing workflows without writing code. But I wanted to see if it could be done inside a SmartForm.

So my challenge was to build a SmartForms app that would test a workflow, without the tester having to write any code.

The app had to do the following:

  • Start any workflow and check that it has started
  • Check that tasks have been created, and the correct number of them
  • Action a task
  • Check the task has been completed
  • Check that the workflow has finished
  • Check the workflow has gone down the correct path

It should all be in SmartForms and be easy to use.

So let’s see how I got on. I am going to start with the finished tool and then take you through it.

Testing dashboard

The dashboard shows all the tests that have been performed, along with the overall percentage of passes and failures.

From here you can click on the ‘Create Test’ button to create a new test. Selecting an existing test and clicking ‘Run Test’ will run it, and double-clicking on a test opens its details.

Test details

The test details show the following information:

  • The workflow being tested
  • Whether it has already been tested
  • When it was tested
  • Overall pass or fail
  • What percentage passed and failed
  • What was tested, and whether each test passed or failed
  • Any other tests relating to the workflow being tested

You can also rerun the test from the test details.

Creating a new test

From the dashboard, click on the ‘Create Test’ button.

  • In the new test form, give the test a name.
  • Select the workflow you are going to test. Once it’s selected, you will see all of that workflow’s activities beneath.
  • Select the activities the workflow will go through.
  • The last section is where we build up the actual tests and the results we expect from them.

Building a simple test script

The workflow we have selected is a very simple one, with the following activities: Setup, Task, Approve, Reject and End.

Task has a client event with one slot that goes to the originator.

Now that we have the basic test details and the route we expect the test to take, we can build our test script.

  • Select ‘Start Process’ from the test type drop-down list.
  • You will notice the other text boxes in the row are prepopulated, and some are disabled, depending on what information that test type needs.
  • Now choose the comparison sign to use. It can be =, >, >=, < or <=.
  • We are going to choose >.
  • In the expected result/comparison box, enter 0. If the workflow starts successfully, we expect a value greater than 0.
  • ‘Start Process’ needs a folio to give the workflow. You can enter something, or leave it blank and it will use the name we gave the test earlier.
  • The next parameter is the process name. You can copy the full workflow name and paste it into this box, or leave it and the workflow name will be filled in automatically.
  • The next box is milliseconds. You can leave it at 0 or enter a value. This is useful if your workflow takes a while to perform an event, as you can add a delay to the tests to compensate.
  • Click on ‘Add’ again. Now we want to check the status of the workflow, to make sure it is active once we have started it.
  • Select ‘Get Workflow Status’ from the drop-down list.
  • Choose ‘=’ from Sign.
  • Enter ‘Active’ in the expected result.
  • We don’t need to enter anything else.
  • Click on ‘Add’.
  • Now we want to check whether it has generated any tasks.
  • Select ‘Get Task Count’.
  • Select ‘=’ for Sign.
  • The expected result should be 1, as we know there is only one slot and it goes to the originator.
  • Leave ProcessInstanceId as it is. The testing framework will replace it with the actual process instance ID at run time.
  • For the process name, enter the full name or leave it to be populated automatically.
  • Next we can action the task.
  • Select ‘=’ for Sign.
  • In the expected result box, enter ‘True’.
  • Where it says ‘Action’, enter the action we expect the test to use. Let’s enter ‘Approve’.
  • Leave ‘Serial Number’ as it is. Like the process instance ID, the testing framework will populate it at run time.
  • For Destination User, enter the user the task is supposed to go to.
  • When this test runs, it will try to action the task using the information we gave it (task action and destination user). If it succeeds, the actual result will be true.
  • The last test checks that the workflow has gone down the correct path and the correct activities were used. It compares the path we defined in the ‘Workflow Route’ section with the path actually taken.
  • Select ‘Compare Activities’ from ‘Test Type’.
  • Select ‘=’ from Sign.
  • Enter ‘True’ in the expected result.
  • Leave ProcessInstanceId and Activities as they are.
  • In the Milliseconds box enter ‘4000’. This makes the ‘Compare Activities’ test wait 4,000 milliseconds before starting, giving the workflow time to perform its other activities before completing. If the test fails, increase the time and it will pass.
  • Now click on ‘Save’.
  • You will see a read-only version of the test, with two buttons: ‘Edit Test’ and ‘Run Test’.

Running a test

To run a test, either select it from the test list and click on ‘Run Test’, or click the ‘Run Test’ button in the test details.

Once the test has completed, you will see the results for each individual test and for the test overall.

Each individual test shows whether it passed or failed. A pass shows as true in green, and a fail shows as false in red.

For each test we create, we also get the overall result. The overall test fails if any of the individual tests has failed.

It shows a pie chart of the pass and fail percentages, with a big black cross above it.

For an overall pass we get a tick and, of course, 100%.

These results stay until you click ‘Run Test’ again, which overwrites them with the new results.

How it’s built

Now that we have seen how it works, let’s look at how it’s put together. It uses the main code base from my previous article on testing, with some additional classes to handle the reflection and some wrappers to make some of the methods accessible.

The new code was available to download from the original post. It is a work in progress and in its early stages.

Wrapper class

There is a wrapper class called Endpoint.cs. It wraps the methods in WorkflowFramework.cs and makes them public static methods, so the K2 endpoint assembly broker can pick them up. Every method follows the same pattern, so here is just the first one.

public static int Start(string Folio, string WorkflowName)
 {
 int result = 0;
 WorkflowInstanceFramework framework = new WorkflowInstanceFramework();
 try
 {
 result = framework.StartProcess(Folio, WorkflowName);
 }
 catch (Exception ex)
 {
result = 0;
 }
 finally {
}
return result;
 }

The full listing is in the original post.

The power of reflection

The next class is called Testplan.cs. This is the class that does all the work, from carrying out the tests to using reflection to produce a list of tests based on the methods in WorkflowFramework.cs. A couple of key methods make this possible.

Method: GetMethods

This method reads through WorkflowFramework.cs and makes a list of all the methods and their parameters. It’s what we use in the SmartForm to populate the list of test types.

public static List<MethodList> GetMethods()
 {
 List<MethodList> list = new List<MethodList>();
 WorkflowInstanceFramework wf = new WorkflowInstanceFramework();
 foreach (var meth in wf.GetType().GetMethods())
 {
 string Left = string.Empty;
 string Middle = string.Empty;
 string Right = string.Empty;
switch (meth.GetParameters().Count())
 {
 case 1:
 {
 Left = meth.GetParameters()[0].Name;
 break;
 }
 // ... cases 2 and 3 also fill Middle and Right
}
list.Add(new MethodList
 {
 MethodName = meth.Name,
 NumberOfParameters = meth.GetParameters().Count(),
 LeftParameter = Left,
 RightParameter = Right,
 MiddleParameter = Middle
 });
}
return list;
}

Method: Invoke

This allows methods to be called dynamically, by passing in the method name as a string along with its parameters. It’s used to call the test methods in WorkflowFramework.cs.

private static object Invoke(string typeName, string methodName, List<string> parameter, int Milliseconds, int parameters)
 {
 if (Milliseconds > 0)
 {
 Thread.Sleep(Milliseconds);
 }
object value = null;
try
 {
 Type type = Type.GetType(typeName);
 object instance = Activator.CreateInstance(type);
 System.Reflection.MethodInfo method = type.GetMethod(methodName);
 System.Reflection.ConstructorInfo constructor = type.GetConstructor(Type.EmptyTypes);
 object classobj = constructor.Invoke(new object[] { });
switch (parameters)
 {
 case 1:
 {
 value = method.Invoke(classobj, new object[] { parameter[0] });
 break;
 }
 // ... cases 2 and 3 pass two and three parameters
}
 }
 catch (Exception ex)
 {
value = ex.Message;
 }
 return value;
 }

Method: RunTest

This method actually runs the tests. We pass in the ‘TestId’, which we use to get the list of tests to carry out on a workflow. We then loop through each test, calling the Invoke method with the method name and parameters. Some parameters can’t be known when the test is being built, so the method looks out for keywords such as ‘ProcessInstanceId’ and replaces them with the actual value.

It then compares the return value against the expected value, which gives true if it passes or false if it fails. This result is recorded back into the database against the test.

Once it has finished all the tests, it works out whether the overall test is a pass or a fail by looking at the result of each test and seeing if there are any fails.

public static void RunTest(int TestId)
 {
 object Result = null;
 List<string> parameters = new List<string>();
 try
 {
List<TestPlanTest> tests = BuildTestPlan(TestId);
 if (tests.Count >= 1)
 {
foreach (var test in tests)
 {
 var Actual = string.Empty;
 switch (test.NumberOfParameters)
 {
 case 1:
 {
parameters.Add(AutoBuildTestParameters(test.LeftParameter, TestId));
 break;
 }
 // ... cases 2 and 3 add the middle and right parameters too
}
 Result = Invoke("K2WorkflowMap.WorkflowInstanceFramework", test.TestType.ToString(), parameters, test.Milliseconds, test.NumberOfParameters);
 if (test.TestType == "StartProcess")
 {
 UpdateTestDetails(TestId, (int)Result, parameters[0].ToString(), "bob", DateTime.Today, true);
 }
 RecordResult(test.TestRunId, Result.ToString(), test.ExpectedResult,test.Sign);
parameters.Clear();
}
}
}
 catch (Exception ex)
 { }
finally
 {
 UpdateTestDetailsEnd(TestId);
}
 }

The original post also had the full code for Testplan.cs, including the SmartObject calls that load test plans and record results.

SmartForms

The SmartForms are pretty straightforward and are included in the solution. They use the excellent High Charts custom control for the graphs, created by the master custom control builder Adam Castle. (Check out his other controls, they are awesome.) You will need a licence for High Charts, but it’s really cheap.

What next

This is a work in progress, so there will be some bugs. But I wanted to see whether testing a workflow could be done in a no-code way, and it can. Please feel free to change the code and share it back so everyone gets the benefit, and I will keep improving the solution.

So what else could make it better?

  • Handling IPC events
  • Making it work with workflows that loop back
  • A PDF report