Introduction

Testing is an important aspect of (BizTalk) application life cycle. Before a developer deploys his solution he needs to be confident that it will perform, and do the task(s) it is intended to do. It is a developers responsibility that he creates a robust application. Therefore he needs to unit test his BizTalk application artifacts before he deploys them for further testing.

A BizTalk developer has a couple of options when it comes to unit testing BizTalk artifacts. Testing of each can be done using a framework like BizUnit, or some of the other available tools offered through CodePlex, or Visual Studio. With BizTalk Server 2009 the unit test feature was introduced, which offered built-in developer support for testing schemas, maps and pipelines in Visual Studio. The test capabilities offered by Visual Studio in for BizTalk artifacts are the following:
  •     Validating an XML document instance.
  •     Testing a map.
  •     Unit test a schema, map and/or a pipeline.
This article will explain step-by-step how we can implement unit test within Visual Studio 2010 to test Schemas and Maps.

Step-by-Step Implementation

Adding a Unit Test Project to your BizTalk Project

To implement unit test within Visual Studio 2010 to test Schemas and Map we need to:
  • Open your BizTalk Project in Visual Studio.NET 2010, in this sample: "UnitTestingFeatureWithMaps.sln"
  • In Solution Explorer, right-click in the BizTalk Server project, in this sample "UnitTestingFeatureWithMaps", and then click Properties.
    • In Project Designer, click the Deployment property page tab and set "Enable Unit Testing" option to "True".

    • Close the project properties page saving the changes.

This feature allows you to create unit tests for schemas, maps, and pipelines. The topics in this section provide some example approaches to using the unit testing feature. When this feature is enabled and the project rebuilt, the artifact classes will be derived from the following base classes to support unit testing.

 Artifact type  Base class
Schema
Microsoft.BizTalk.TestTools.Schema.TestableSchemaBase
Map
Microsoft.BizTalk.TestTools.Mapper.TestableMapBase
Pipeline
Microsoft.BizTalk.TestTools.Pipeline.TestablePipelineBase 
  • In main menu, click Build, and then click Rebuild Solution.
  • On the main menu, click Test, and then click "New Test..."

  • In the "Add New Test" dialog box, select "Create a new Visual C# test project..." for Add to Test Project field. Select "Unit Test Wizard" in the Templates list, and then click OK.

  • In the New Test Project dialog box, leave the project name as "TestProjectBizTalk" and click Create.

  • In the "Create Unit Tests" dialog box, expand the types and select the PersonOrigin () constructor under the UnitTestingFeatureWithMaps.Schemas.PersonOrigin node. 

  • Also select HowMapsWorks() constructor under the UnitTestingFeatureWithMaps.Maps.HowMapsWorks node. 

  • After making the selections shown below, press OK.
This will create for us an Unit test project referring all the necessary assemblies:
  • Microsoft.BizTalk.TestTools
  • Microsoft XLANG/s Base Types
  • BizTalk Server project assembly
And the skeleton classes to perform all the test to the selected artifact (schema and map):
  • HowMapsWorksTest.cs
  • PersonOriginTest.cs

Adding Test Code to Test the Schemas and Map

  • In Solution Explorer, open PersonOriginTest.cs
  • Scroll to the bottom of the file and replace the PersonOriginConstructorTest() method with the following code which validates the sample input message:
/// <summary>
///A test for PersonOrigin Constructor
///</summary>
[TestMethod()]
public void PersonOriginConstructorTest()
{
    PersonOrigin target = new PersonOrigin();
 
 
    //=== The SamplePOInput.xml file from <Samples Path>\Orchestrations\HelloWorld ===//
    string strSourcePO_XML = "C:\\Development\\UnitTestingFeatureWithMaps\\Files\\PersonOrigin.xml";
     
    //=== Validate the SamplePOInput message against the schema ===//
    Assert.IsTrue(target.ValidateInstance(strSourcePO_XML, Microsoft.BizTalk.TestTools.Schema.OutputInstanceType.XML));
}
 
// <summary>
///A test for PersonOrigin Constructor
///</summary>
[TestMethod()]
public void PersonOriginWithoutPhoneCallsConstructorTest()
{
    PersonOrigin target = new PersonOrigin();
 
 
    //=== The SamplePOInput.xml file from <Samples Path>\Orchestrations\HelloWorld ===//
    string strSourcePO_XML = "C:\\Development\\UnitTestingFeatureWithMaps\\Files\\PersonOrigin2.xml";
 
    //=== Validate the SamplePOInput message against the schema ===//
    Assert.IsTrue(target.ValidateInstance(strSourcePO_XML, Microsoft.BizTalk.TestTools.Schema.OutputInstanceType.XML));
}
 
// <summary>
///A test for PersonOrigin Constructor
///</summary>
[TestMethod()]
public void PersonOriginWithoutZipConstructorTest()
{
    PersonOrigin target = new PersonOrigin();
 
 
    //=== The SamplePOInput.xml file from <Samples Path>\Orchestrations\HelloWorld ===//
    string strSourcePO_XML = "C:\\Development\\UnitTestingFeatureWithMaps\\Files\\PersonOriginWithoutZip.xml";
 
    //=== Validate the SamplePOInput message against the schema ===//
    Assert.IsTrue(target.ValidateInstance(strSourcePO_XML, Microsoft.BizTalk.TestTools.Schema.OutputInstanceType.XML));
}
  • In Solution Explorer open HowMapsWorksTest.cs and add the following directive to the top of that file:
using System.IO;
  • Scroll to the bottom of the file and replace the HowMapsWorksMapTest() method with the following code which tests the map using the sample input message:
[TestMethod()]
public void HowMapsWorksMapTest()
{
    HowMapsWorks map = new HowMapsWorks();
 
    //=== Use the HelloWorld sample directory path for the message files ===//
 
    string strSourcePO_XML = testContextInstance.TestDir + "..\\..\\..\\Files\\PersonOrigin.xml";
    string strDestInvoice_XML = "F:\\" + testContextInstance.TestDir + "\\OUT\\PersonTarget2.xml";
 
 
    //=== Test the map by using the TestMap method of the TestableMapBase class ===//
 
    map.ValidateOutput = true;
    map.TestMap(strSourcePO_XML,
                   Microsoft.BizTalk.TestTools.Schema.InputInstanceType.Xml,
                   strDestInvoice_XML,
                   Microsoft.BizTalk.TestTools.Schema.OutputInstanceType.XML);
 
 
    //=== Output file should be created as a result of testing the map ===//
 
    Assert.IsTrue(File.Exists(strDestInvoice_XML));
}
 
[TestMethod()]
public void HowMapsWorksWithoutPhoneCallsMapTest()
{
    HowMapsWorks map = new HowMapsWorks();
 
    //=== Use the HelloWorld sample directory path for the message files ===//
 
    string strSourcePO_XML = testContextInstance.TestDir + "..\\..\\..\\Files\\PersonOrigin2.xml";
    string strDestInvoice_XML = testContextInstance.TestDir + "\\OUT\\PersonTargetWithoutTotals.xml";
 
 
    //=== Test the map by using the TestMap method of the TestableMapBase class ===//
 
    map.ValidateOutput = true;
    map.TestMap(strSourcePO_XML,
                   Microsoft.BizTalk.TestTools.Schema.InputInstanceType.Xml,
                   strDestInvoice_XML,
                   Microsoft.BizTalk.TestTools.Schema.OutputInstanceType.XML);
 
 
    //=== Output file should be created as a result of testing the map ===//
 
    Assert.IsTrue(File.Exists(strDestInvoice_XML));
}
 
[TestMethod()]
public void HowMapsWorksWithoutZipMapTest()
{
    HowMapsWorks map = new HowMapsWorks();
 
    //=== Use the HelloWorld sample directory path for the message files ===//
 
    string strSourcePO_XML = testContextInstance.TestDir + "..\\..\\..\\Files\\PersonOrigin.xml";
    string strDestInvoice_XML = testContextInstance.TestDir + "\\OUT\\PersonTargetWithoutZip.xml";
 
 
    //=== Test the map by using the TestMap method of the TestableMapBase class ===//
 
    map.ValidateOutput = true;
    map.TestMap(strSourcePO_XML,
                   Microsoft.BizTalk.TestTools.Schema.InputInstanceType.Xml,
                   strDestInvoice_XML,
                   Microsoft.BizTalk.TestTools.Schema.OutputInstanceType.XML);
 
 
    //=== Output file should be created as a result of testing the map ===//
 
    Assert.IsTrue(File.Exists(strDestInvoice_XML));
}

Building and Running the Unit Test

  • In Solution Explorer, right-click TestProjectBizTalk, and then click Build.
  • On the main menu, click "Tes"t, and then select "Run" and click "All Tests in Solution".

  • A "Test Results" will be display presenting all the status information about the unit test. If any test fails you can double-click the test in the Test Results window to see the assert or exception that caused that test failure.

Source Code

This sample can be found and downloaded in Microsoft Code Gallery:

See Also

The following resources will provide useful information on unit testing BizTalk Server projects: Another important place to find a huge amount of BizTalk related articles is the TechNet Wiki itself. The best entry point is BizTalk Server Resources on the TechNet Wiki.



Back to Top