Tuesday, March 18, 2008

BizUnit 3.0 Api

Lately I've been working heavily with the BizUnit 3.0 beta for testing a number of different projects. In particular I'm consuming the new object model API in order to programmatically invoke tests using a variety of test sources. The purpose of this post is to provide an introduction to the new API and it's usage. Prior to 3.0, BizUnit needed to be invoked using XML such as the following (an example borrowed from the CodePlex site):

<TestStep assemblyPath="" typeName="Microsoft.Services.BizTalkApplicationFramework.BizUnit.FileValidateStep">
  <Timeout>3000</Timeout>
  <Directory>..\..\..\Rec_03\</Directory>
  <SearchPattern>TransactionId_*.xml</SearchPattern>
  <DeleteFile>true</DeleteFile>
 
  <ValidationStep assemblyPath="" typeName="Microsoft.Services.BizTalkApplicationFramework.BizUnit.XmlValidationStep">
      <XmlSchemaPath>..\..\..\TestData\PurchaseOrder.xsd</XmlSchemaPath>
      <XmlSchemaNameSpace>http://SendMail.PurchaseOrder</XmlSchemaNameSpace>
      <XPathList>
          <XPathValidation query=
              "/*[local-name()='PurchaseOrder' and namespace-uri()='http://SendMail.PurchaseOrder']
              /*[local-name()='PONumber' and namespace-uri()='']">PONumber_0</XPathValidation>
      </XPathList>
  </ValidationStep>  
</TestStep>

BizUnit could then be invoked with something like the following (where BizUnitAPI is the BizUnit class in the BizUnit namespace):

string myConfigFile = "c:\path\to\test.xml"; BizUnitAPI bizUnit = new BizUnitAPI(myConfigFile); bizUnit.RunTest();

This is great because we can easily generate XML to represent a set of Test Cases and Test Steps to run in series. However, this doesn't give you the control you might need. Enter the BizUnit.BizUnitOM namespace and it's new classes:

  • BizUnitTestCase : Encapsulates a set of test steps.
  • TestStepBuilder : Builds a test step instance.
  • ITestStepOM : Test step classes must implement this to work with the new API.

Our usage of the API can be written as follows:

  1. Create a BizUnitTestCase.
  2. For each step:
    1. Create a TestStepBuilder.
    2. Set each of the properties of the test step to ensure the step behaves as we want.
    3. Add the TestStepBuilder to our test case.
  3. Create a BizUnit instance using the BizUnitTestCase.
  4. Run the test.

The following is a trivial example of creating a test case to delete all the TXT files in a specified path.

BizUnitTestCase testCase = new BizUnitTestCase("My First Test Case");

TestStepBuilder testStepBuilder = new TestStepBuilder("BizUnit.FileDeleteStep");

object[] args = new object[1] {@"c:\my\path\*.txt"};

testStepBuilder.SetProperty("FilesToDeletePath", args);

testCase.AddTestStep(testStepBuilder, TestStage.Execution);

BizUnitAPI bizUnit = new BizUnitAPI(testCase);

bizUnit.RunTest();

If the test step were to require multiple arguments to be set, then the SetProperty method can be called as many times is required. Also, a TestStepBuilder needs to be generated for each test step in our test case.

If the required implementation of ITestStepOM is not included with BizUnit, then the TestStepBuilder can be invoked to load an external assembly:

TestStepBuilder testStepBuilder = new TestStepBuilder("MyAssembly.MyTestStep", "c:\path\to\MyAssembly.dll");

Note that, out of the box, only certain property types are supported as test step properties. These are:

  • double
  • bool
  • short
  • int
  • long
  • string
  • IList<string>
  • IList<Pair>

Other types can be supported but they require a custom implementation of ITestStepParameterFormatter. The implementation of which I'll save for another post.

Any questions or comments? I recommend the CodePlex Discussions Page.

No comments: