Importing BizUnit Test Cases…

Often test cases are built up progressively testing more and more functionality, or they are built in a manner whereby they have dependencies. The diagram below illustrates this, test case #6 is dependent on test case 3, which is in turn dependant on test case 1 and 2.

image

A real world example of this might be where a GetBookingStatus web service operation needs to be tested, GetBookingStatus takes a bookingId, so before it can be called, CreateBooking needs to be called which returns a bookingId. In order to test GetBookingStatus, a CreateBooking test can be created, which returns a bookingId and sets it in the BizUnit context, next GetBookingStatus is called, but the request message is modified using an Xml data loader setting the bookingId of the request message to the value previously set in the context. This example shows how the various BizUnit features maybe used to achieve a reasonably complex scenario.

To achieve this, BizUnit offers the ability to import a test case at any stage within another test case. The code below illustrates a simple import scenario. The first test case creates a file “File1.xml”. The second test case imports the first test case, then creates a second file “File2.xml”, then uses a validating file read step to check that two files were created that also validate against the defined schema. The code below illustrates this.

Using BizUnit.TestSteps.Common;
using BizUnit.TestSteps.DataLoaders.File;
using BizUnit.TestSteps.File;
using BizUnit.TestSteps.ValidationSteps.Xml;
using BizUnit.Xaml;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace BizUnit.TestSteps.Tests.ImportTestCase
{
    [TestClass]
    public class ImportTestCaseTest
    {
        [TestMethod]
        public void ImportSingleTestCaseTest()
        {
            // Create the first test case i a helper method...
            var testCase1 = BuildFirstTestCase();

            // Create the second test case and import the first test case into it...
            var testCase2 = new TestCase {Name = "Copy First File Test"};

            var createFileStep = new CreateStep {CreationPath = @"File2.xml"};
            var dl = new FileDataLoader
                         {
                             FilePath = 
                       @"..\..\..\Test\BizUnit.TestSteps.Tests\TestData\PurchaseOrder001.xml"                 
                           };
            createFileStep.DataSource = dl;

            testCase2.ExecutionSteps.Add(createFileStep);

            var import = new ImportTestCaseStep {TestCase = testCase1};
            testCase2.ExecutionSteps.Add(import);
           
            // Create a validating read step...
            var validatingFileReadStep = new FileReadMultipleStep
                               {
                                   DirectoryPath = @".",
                                   SearchPattern = "File*.xml",
                                   ExpectedNumberOfFiles = 2
                               };

            var validation = new XmlValidationStep();
            var schemaPurchaseOrder = new SchemaDefinition
            {
                XmlSchemaPath =
                    @"..\..\..\Test\BizUnit.TestSteps.Tests\TestData\PurchaseOrder.xsd",
                XmlSchemaNameSpace =
                    "http://SendMail.PurchaseOrder"
            };
            validation.XmlSchemas.Add(schemaPurchaseOrder);

            var xpathProductId = new XPathDefinition
                                     {
                                         Description = "PONumber",
                                         XPath =
                                             "/*[local-name()='PurchaseOrder' and namespace-uri()='http://SendMail.PurchaseOrder']/*[local-name()='PONumber' and namespace-uri()='']",
                                         Value = "12323"
                                     };
            validation.XPathValidations.Add(xpathProductId);
            validatingFileReadStep.SubSteps.Add(validation);
            testCase2.ExecutionSteps.Add(validatingFileReadStep);

            // Run the second test case...
            var bizUnit = new BizUnit(testCase2);
            bizUnit.RunTest();
        }

        private TestCase BuildFirstTestCase()
        {
            var testCase1 = new TestCase {Name = "Copy First File Test"};

            var step = new CreateStep();
            step.CreationPath = @"File1.xml";
            var dl = new FileDataLoader();
            dl.FilePath = 
                @"..\..\..\Test\BizUnit.TestSteps.Tests\TestData\PurchaseOrder001.xml";
            step.DataSource = dl;
            step.Execute(new Context());

            testCase1.ExecutionSteps.Add(step);
            return testCase1;
        }
    }
}

The same coded test case shown above may also be defined in XAML as shown below:

<TestCase Category="{x:Null}" Description="{x:Null}" ExpectedResults="{x:Null}" Preconditions="{x:Null}" Purpose="{x:Null}" Reference="{x:Null}" BizUnitVersion="4.0.133.0" Name="Copy First File Test" 
  xmlns="clr-namespace:BizUnit.Xaml;assembly=BizUnit" 
  xmlns:btc="clr-namespace:BizUnit.TestSteps.Common;assembly=BizUnit.TestSteps" 
  xmlns:btdf="clr-namespace:BizUnit.TestSteps.DataLoaders.File;assembly=BizUnit.TestSteps" 
  xmlns:btf="clr-namespace:BizUnit.TestSteps.File;assembly=BizUnit.TestSteps" 
  xmlns:btvx="clr-namespace:BizUnit.TestSteps.ValidationSteps.Xml;assembly=BizUnit.TestSteps"   
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" >
  <TestCase.ExecutionSteps>
    <btf:CreateStep SubSteps="{x:Null}" CreationPath="File2.xml" FailOnError="True" 
      RunConcurrently="False">
      <btf:CreateStep.DataSource>
        <btdf:FileDataLoader 
          FilePath="..\..\..\Test\BizUnit.TestSteps.Tests\TestData\PurchaseOrder001.xml" />
      </btf:CreateStep.DataSource>
    </btf:CreateStep>
    <ImportTestCaseStep SubSteps="{x:Null}" TestCasePath="{x:Null}" FailOnError="True" 
      RunConcurrently="False">
      <ImportTestCaseStep.TestCase>
        <TestCase Category="{x:Null}" Description="{x:Null}" ExpectedResults="{x:Null}" 
          Preconditions="{x:Null}" Purpose="{x:Null}" Reference="{x:Null}" 
          BizUnitVersion="4.0.133.0" Name="Copy First File Test">
          <TestCase.ExecutionSteps>
            <btf:CreateStep SubSteps="{x:Null}" CreationPath="File1.xml" FailOnError="True" 
              RunConcurrently="False">
              <btf:CreateStep.DataSource>
                <btdf:FileDataLoader 
                  FilePath=
                    "..\..\..\Test\BizUnit.TestSteps.Tests\TestData\PurchaseOrder001.xml" />
              </btf:CreateStep.DataSource>
            </btf:CreateStep>
          </TestCase.ExecutionSteps>
        </TestCase>
      </ImportTestCaseStep.TestCase>
    </ImportTestCaseStep>
    <btf:FileReadMultipleStep DeleteFiles="False" DirectoryPath="." ExpectedNumberOfFiles="2" 
      FailOnError="True" RunConcurrently="False" SearchPattern="File*.xml" Timeout="0">
      <btf:FileReadMultipleStep.SubSteps>
        <btvx:XmlValidationStep>
          <btvx:XmlValidationStep.XPathValidations>
            <btc:XPathDefinition ContextKey="{x:Null}" Description="PONumber" Value="12323" 
              XPath="/*[local-name()='PurchaseOrder' and namespace-uri()='http://SendMail.PurchaseOrder']/*[local-name()='PONumber' and namespace-uri()='']" />
          </btvx:XmlValidationStep.XPathValidations>
          <btvx:XmlValidationStep.XmlSchemas>
            <btvx:SchemaDefinition XmlSchemaNameSpace="http://SendMail.PurchaseOrder" 
              XmlSchemaPath="..\..\..\Test\BizUnit.TestSteps.Tests\TestData\PurchaseOrder.xsd" />
          </btvx:XmlValidationStep.XmlSchemas>
        </btvx:XmlValidationStep>
      </btf:FileReadMultipleStep.SubSteps>
    </btf:FileReadMultipleStep>
  </TestCase.ExecutionSteps>
</TestCase>
The depth of the import functionality is arbitrary, providing the test cases scenarios are designed in a logical and sensible manner leveraging import can give big productivity gains when developing tests.
Advertisements

~ by kevinsmi on March 23, 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: