DCSIMG
Testable Work Flow using Nunit - Doron Goldberg

Testable Work Flow using Nunit

After finishing the wwf hands on lab, I started practicing my first wf project as part of my preparations for starting working with it in two of our upcoming projects (one of them is for distributing email's inside the organization and the other is for managing content&forms inside sps 2003!!!).

 

The first project I created is a very simple wf which is exposed as a web service and uses a simple calculator (add/divide/subtract/multiply) class to perform some calculations.

 

I used nunit in order to test the calculator class and the exposed web service automatically created for the wf itself.

 

Well, that was too easy and wasn't exactly a real automated unit test...

Next thing I wanted to do is test a wf, not a wf interface - set the parameters, launch the wf from within the test fixture and validate the results.

I will pretend I was TDDing in this example so I'll start from the tests of a simple flow which will receive a number and set the IsEven property:

 [TestFixture]
    public class MySecondNotWSWorkFlowTEST
    {
        private int isEvenInt;
        private bool isEven;
        private bool TestCompleted;
        private WorkflowRuntime wr;
        void workflowRuntime_WorkflowCompleted(object sender, WorkflowCompletedEventArgs e)
        {
            
            isEven = (bool)e.OutputParameters["IsEven"];
            if (isEven)
                isEvenInt = 1;
            TestCompleted = true;
        }
        [TestFixtureSetUp]
        public void FixtureSetUp()
        {

            wr = new WorkflowRuntime();
            wr.WorkflowCompleted += workflowRuntime_WorkflowCompleted;
            wr.StartRuntime();
        }
        [SetUp]
        public void TestSetUp()
        {
            TestCompleted = false;
            isEvenInt = 0;
        }
        [Test]
        public void EvenTEST()
        {
            Dictionary<string, object> parameters = new Dictionary<string, object>();
            parameters.Add("Number", 10);
            WorkflowInstance instance = wr.CreateWorkflow(typeof(MySecondNotWSWorkFlow.Workflow1), parameters);
            instance.Start();
            while(!TestCompleted){}
            Assert.AreEqual(1,isEvenInt);
        }
        [Test]
        public void OddTEST()
        {
            Dictionary<string, object> parameters = new Dictionary<string, object>();
            parameters.Add("Number", 11);
            WorkflowInstance instance = wr.CreateWorkflow(typeof(MySecondNotWSWorkFlow.Workflow1), parameters);
            instance.Start();
            while (!TestCompleted) { }
            Assert.AreEqual(0, isEvenInt);
        }
    }

As you can see the test is a bit weird:

1. Why using isEvenInt?

 Because accessing isEven from the test functions resulted an error on the second test:

NUnit.Framework.AssertionException 
at NUnit.Framework.Assert.DoAssert(IAsserter asserter)
at NUnit.Framework.Assert.IsTrue(Boolean condition, String message, Object[] args)
at NUnit.Framework.Assert.IsTrue(Boolean condition)
at CalculatorTEST.MySecondNotWSWorkFlowTEST.OddTES

I'm still not sure why, I hope to resolve it in the future.

2. While (!TestCompleted) { } ?!?!?

I do not know how to test async functions using nunit - maybe some advanced users of the tool can help on that?

And the flow:

 

Flow code:

    public sealed partial class Workflow1: SequentialWorkflowActivity
    {
        public static DependencyProperty IsEvenProperty = System.Workflow.ComponentModel.DependencyProperty.Register("IsEven", typeof(bool), typeof(Workflow1));
        [Description("This is the description which appears in the Property Browser")]
        [Category("This is the category which will be displayed in the Property Browser")]
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public bool IsEven
        {
            get
            {
                return ((bool)(base.GetValue(Workflow1.IsEvenProperty)));
            }
            set
            {
                base.SetValue(Workflow1.IsEvenProperty, value);
            }
        }
        public Workflow1()
        {
            InitializeComponent();
        }
        private  CalculationOperation operation;
        public CalculationOperation Operation
        {
            get { return operation; }
            set { operation = value; }
        }
        private int number;
                                    
        public int Number
        {
            get { return number; }
            set { number = value; }
        }

        private bool isprimary;

        public bool IsPrimary
        {
            get { return isprimary; }
            set { isprimary = value; }
        }

        private void codeActivity1_ExecuteCode(object sender, EventArgs e)
        {
            IsEven = true;
        }

        private void codeActivity2_ExecuteCode(object sender, EventArgs e)
        {
            IsEven = false;
        }
    
    }

What next?

 

I have some ideas in mind:

1. WWFMock

2. Using a flow in order to test a second flow, creating Assert activities maybe?

3. Using a flow in order to test regular non-flow based applications

 

All of the above are "research" ideas and I'm not sure they are of any real use, are they?

Comments

# re: Testable Work Flow using Nunit

Thursday, July 31, 2008 3:19 AM by ganesh

internetexplorer7internetexplorer7internetexplorer7internetexplorer7internetexplorer7internetexplorer7internetexplorer7internetexplorer7internetexplorer7internetexplorer7internetexplorer7internetexplorer7internetexplorer7internetexplorer7internetexplorer7internetexplorer7internetexplorer7internetexplorer7internetexplorer7internetexplorer7internetexplorer7internetexplorer7internetexplorer7internetexplorer7internetexplorer7internetexplorer7internetexplorer7internetexplorer7internetexplorer7internetexplorer7internetexplorer7internetexplorer7internetexplorer7internetexplorer7internetexplorer7internetexplorer7internetexplorer7internetexplorer7internetexplorer7internetexplorer7internetexplorer7internetexplorer7internetexplorer7internetexplorer7internetexplorer7internetexplorer7internetexplorer7internetexplorer7internetexplorer7internetexplorer7internetexplorer7internetexplorer7internetexplorer7internetexplorer7internetexplorer7internetexplorer7internetexplorer7internetexplorer7internetexplorer7internetexplorer7internetexplorer7internetexplorer7internetexplorer7internetexplorer7internetexplorer7internetexplorer7internetexplorer7internetexplorer7internetexplorer7internetexplorer7internetexplorer7internetexplorer7internetexplorer7internetexplorer7internetexplorer7internetexplorer7internetexplorer7internetexplorer7internetexplorer7internetexplorer7internetexplorer7internetexplorer7internetexplorer7internetexplorer7internetexplorer7internetexplorer7internetexplorer7internetexplorer7internetexplorer7internetexplorer7internetexplorer7internetexplorer7internetexplorer7internetexplorer7internetexplorer7

Leave a Comment

(required) 
(required) 
(optional)
(required) 

Enter the numbers above: