Creating a Custom Test Step for BizUnit

BizUnit is a framework, often it won’t have every test step to support your scenario, because its a Framework this should not present an issue. The overhead for creating new test steps has for the large part always been pretty low, in 4.0 its easier than ever.

To create a new test step your class needs to derive from TestStepBase, which is shown below:

image

As you will see its an abstract class and has two abstract methods which you will need to override, Execute and Validate, by now it should be clear what these are used for. Also note that TestStepBase has a collection SubSteps, these may be used for validation for example should you step support validation. So lets say we wish to implement a new step that reads and validates a file, the code below shows how this new step would start out, note a reference to BizUnit.dll will need to be added to this new library project.

using BizUnit;
using BizUnit.Xaml;

namespace ExampleTestStepLibrary
{
    public class ReadFileStep : TestStepBase
    {
        public override void Execute(Context context) {}
        public override void Validate(Context context) {}
    }
}

The next thing to consider is what configuration this step requires, I probably need to be able to specify the path to the file to read, perhaps I also wish to allow flexibility around whether the file is deleted after its been read, for this I can add another property. To do this I simply need to add two public properties as follows:

        public string FilePath { get; set; }
        public bool DeleteFile { get; set; }

This will allow my step to be configured programmatically or in XAML.

Next, lets implement the Execute method, first I want to validate that the file exists, if it doesn’t I’ll throw a meaning full error. Then I’ll open the file stream. Now, because I want this step to support validation I’ll pass the file stream to each sub step to give it a chance to process the data. Finally, if the step has been configured to delete the file, it’ll do that.

        public override void Execute(Context context)
        {
            var fi = new FileInfo(FilePath);
            if(!fi.Exists)
            {
                throw new ApplicationException(
                    string.Format("The file: {0}, could not be found!", 
                    FilePath));
            }

            Stream data = null;
            using (data = File.Open(
                FilePath, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                foreach (var subStep in SubSteps)
                {
                    data = subStep.Execute(data, context);
                }
            }

            if(DeleteFile)
            {
                fi.Delete();
            }
        }

You’ll notice that I’m using the SubSteps, whilst these are declared on the TestStepBase they are not created since they are an optional feature, so my constructor needs to create the collection:

        public ReadFileStep()
        {
            SubSteps = new Collection<SubStepBase>();
        }

The last thing to do is to implement the Validate method, as you’ll know by now, this is going to be executed by BizUnit after the step has been configured, but before its executed. So, my validation should check that the FilePath property has been set, to do this I’m simply going to use a BizUnit helper class:

        public override void Validate(Context context)
        {
            ArgumentValidation.CheckForEmptyString(FilePath, "FilePath");
        }

So, now my step is ready, and it may be used in both coded or XAML tests. Of course, I’d want to add a unit test project to ensure there’s no bugs in my step.

The whole class looks like this:

using System;
using System.Collections.ObjectModel;
using System.IO;
using BizUnit;
using BizUnit.Common;
using BizUnit.Xaml;

namespace ExampleTestStepLibrary
{
    public class ReadFileStep : TestStepBase
    {
        public string FilePath { get; set; }
        public bool DeleteFile { get; set; }

        public ReadFileStep()
        {
            SubSteps = new Collection<SubStepBase>();
        }

        public override void Execute(Context context)
        {
            var fi = new FileInfo(FilePath);
            if(!fi.Exists)
            {
                throw new ApplicationException(
                    string.Format("The file: {0}, could not be found!", 
                    FilePath));
            }

            Stream data = null;
            using (data = File.Open(
                FilePath, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                foreach (var subStep in SubSteps)
                {
                    data = subStep.Execute(data, context);
                }
            }

            if(DeleteFile)
            {
                fi.Delete();
            }
        }

        public override void Validate(Context context)
        {
            ArgumentValidation.CheckForEmptyString(FilePath, "FilePath");
        }
    }
}
Advertisements

~ by kevinsmi on April 13, 2011.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: