Tom Clarkson is a SharePoint consultant and entrepreneur based in Sydney, Australia.

Contact Details

Links



Recent Searches



Archives




Past Posts







RSS Feed

Unit Testing a State Machine Workflow with Delay Activities

SharePoint 2007, Workflow
Thursday May 22 2008

The general approach I am using for workflow testing involves a test fixture class for each state in the workflow. TestFixtureSetup creates a workflow runtime and there is a method CreateWorkflowAndMoveToPaidState() which creates a new workflow instance and triggers the various events needed to move it to the state being tested.

   

This approach allows effective testing of everything in the workflow except the activities including delays. Testing the delayed activities required some small changes to the workflow.

   

1. Add a new event ForceTimerExpiry that can be triggered by the test.

   

2. Add a handler for that event to any state with a timer

   

   

The event handler code sets the timeout to zero:

   

        private void handleForceTimerExpiryPaid_Invoked(object sender, ExternalDataEventArgs e)

        {

            delayComputronUpdate.TimeoutDuration = new TimeSpan(0);

        }

   

Then a SetState activity resets the current state, which will recreate the timer with the new zero timeout.

   

3. Make sure reinitialisation of the timer will not add a new delay

   

        private void delayComputronUpdate_InitializeTimeoutDuration(object sender, EventArgs e)

        {

            // For testing purposes. Since all delays have non-zero defaults, zero 0 means

            // that the timeout has been reset by the ForceTimerExpiry event.

            if (((DelayActivity)sender).TimeoutDuration.Ticks == 0) return;

            if (DateTime.Now.TimeOfDay < Constants.ComputronUpdateTime)

            {

                ((DelayActivity)sender).TimeoutDuration = DateTime.Today + Constants.ComputronUpdateTime - DateTime.Now;

            }

            else

            {

                ((DelayActivity)sender).TimeoutDuration = DateTime.Today.AddDays(1) + Constants.ComputronUpdateTime - DateTime.Now;

            }

        }

   

4. Trigger the ForceTimerExpiry event from a test

   

        [Test]       

        public void ExpireComputronUpdateTimer_Test()

        {

            Guid instanceId = CreateWorkflowAndMoveToPaidState();

            WorkflowManager.ForceTimerExpiry(runtime, instanceId, 0);

             

            TestEntityHelper.CheckState(runtime, instanceId, null, null, APWorkflowStates.Presented);

        }

   

Comments

On 31 May 2008 06:26, Peter Seale said:
I've been interested in the community's various attempts at trying out automated testing in SharePoint (and posted about what I've found)--so would you say this was worth it? How difficult was it to implement? What is the biggest benefit you received from testing your VS workflow? Anyway I'm fascinated by all these sorts of things.
On 02 Jun 2008 02:25, Tom Clarkson said:
In this particular case the workflow interacts with a sql database more than with SharePoint (architectural decisions made long before I joined the project), but since the workflow doesn't need UI as long as you pass it the right parameters, adding SPSite to the test setup wouldn't be too hard.

The difficult part of the implementation was the generic setup - setting up the runtime and getting events to the workflow, especially timer events. Once that's in place, implementation of new tests is relatively easy.

The testing has definitely been worth it. Being able to run through a couple of hundred tests at once exposed several bugs that likely wouldn't have appeared until production - even stuff like timers firing after 2 days instead of 2 business days.

On 06 Oct 2008 07:01, said:

Leave a comment