다음을 통해 공유


BizTalk Server 2013: Test BizTalk artifacts with Visual Studio 2012

Introduction

Testing is an important aspect of (BizTalk) application life cycle. Before a developer deploys his solution into UAT 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 to UAT environment 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 is a 101 on testing BizTalk artifacts within Visual Studio and a reference to available resources found on MSDN and the internet.

Test an instance of a document schema

Testing an instance of document schema in Visual Studio is basic task you can perform. You can navigate to the schema within your BizTalk project. You right click on the schema in the Solution Explorer and choose properties. Before you test a schema through Validate Instance you specify the Output Type, Input Instance Name, Output Instance Name, and Validate Instance Input Type. As input instance you can choose a document on your file system you wish to test if it adheres to the schema. After selecting the document and specifying other details in the properties you can right click the schema and choose Validate Instance. A window will pop up with information of the validation. In case the the Output window did not open and display information about whether the schema validation succeeded or failed, you can open it manually.

See Also MSDN Instance Message Generation and Validation and Testing Schemas.

Testing a map

Testing of a map in Visual Studio is another basic task you can perform like validating a schema. You can navigate to the map within your BizTalk project. You right click on the map in the Solution Explorer and choose properties. Before you test a map through Test Map you specify the TestMap Input, TestMap Input Instance, TestMap Output, TestMap Output Instance, Validate TestMap Input, and Validate TestMap Output. After selecting the document and specifying other details in the properties you can right click the map and choose Test Map. A window will pop up with information of the test. In case the the Output window did not open and display information about whether the test succeeded or failed, you can open it manually.

See Also MSDN How to Test Maps and How to Configure Map Validation and Test Parameters.

Unit testing of BizTalk artifacts

A developer can leverage the capabilities of unit testing in Visual Studio for a schema, map and/or pipeline. Visual Studio offers the Unit Test functionality by Unit Testing Framework, which allows you to create unit tests.

Unit testing of a schema

To unit test a schema you can leveraging the unit test framework in Visual Studio. You can follow the steps below, which is one of the approaches you can take:

  • You right click the BizTalk project in the solution explorer containing the schema(s).
  • You go the Deployment Tab and in the Unit Test section choose Enable Unit Testing. By enabling unit testing the schema(s) will inherit from TestableSchemaBase.
  • Add a new test project to your solution.
  • Set reference to Microsoft.BizTalk.TestTools.dll (<install drive>\Program Files (x86)\Microsoft BizTalk Server 2013\Developer Tools) , Microsoft.XLANGs.BaseTypes.dll (<install drive>\Program Files (x86)\Microsoft BizTalk Server 2013\) and the assembly containing the schema(s).
  • In the Unit Test Class you can test methods like below for validating XML document instance:
[TestMethod]
public void  TNWikiArticleInstanceTest()
 {
  TNWikiArticle tnw = new  Schemas.TNWikiArticle();
  bool success = false;
  success = tnw.ValidateInstance("..//..//TNWikiArticleInstance.xml", Microsoft.BizTalk.TestTools.Schema.OutputInstanceType.XML);
  Assert.IsTrue(success);
 }
  • You can test the method by clicking Run all Tests in Solution.

Unit testing a schema means that the ValidateInstance method is used, which is similar as to choosing Validate Instance on a schema in a BizTalk project (See Test an instance of a document schema). ValidateInstance method is a member of TestabaleSchemaBase class belonging to the namespace Microsoft.XLANGs.BaseTypes. When calling this method you have to provide the path to document, and the output instance type. The method will return a Boolean value indicating if the validation was successful (true) or not (false).

See Also MSDN:

Unit testing of a map

To unit test a map you can leveraging the unit test framework in Visual Studio. You can follow the steps below, which is one of the approaches you can take:

  • You right click the BizTalk project in the solution explorer containing the map(s).
  • You go the Deployment Tab and in the Unit Test section choose Enable Unit Testing. By enabling unit testing the schema(s) will inherit from TestableMapBase.
  • Add a new test project to your solution.
  • Set reference to Microsoft.BizTalk.TestTools.dll (<install drive>\Program Files (x86)\Microsoft BizTalk Server 2013\Developer Tools) , Microsoft.XLANGs.BaseTypes.dll (<install drive>\Program Files (x86)\Microsoft BizTalk Server 2013\) and the assembly containing the schema(s).
  • In the Unit Test Class you can test method like below for testing a mapping:
[TestMethod()]
public void  MapOutputTNWikiArticleToUpdatedTNWikiArticleTest()
 {
  TNWikiArticleToUpdatedTNWikiArticle tnwiki = new  Maps.TNWikiArticleToUpdatedTNWikiArticle();
  string input = "..\\..\\TNWikiArticleInstance.xml";
  string output = @"..\\..\\UpdatedTNWikiArticle_Output.XML";
  tnwiki.TestMap(input, Microsoft.BizTalk.TestTools.Schema.InputInstanceType.Xml, output, Microsoft.BizTalk.TestTools.Schema.OutputInstanceType.XML);
  Assert.IsTrue(File.Exists(output));
 }
  • You can test the method by clicking Run all Tests in Solution.

Unit testing a map means that the TestMap method is used, which is similar as to choosing Test Map on a map in a BizTalk project (See Testing a Map). TestMap method is a member of TestabaleMapBase class belonging also to the namespace Microsoft.XLANGs.BaseTypes. When calling this method you have to provide the path to a source document (instance), type of instance, path to location for output document (instance), and type of output instance. This method will not return anything (i.e. void method). The way to check if the mapping has executed successfully is to validate if  the output has been created.

See Also MSDN:

Unit testing of a pipeline

To unit test a custom pipeline you can leveraging the unit test framework in Visual Studio. You can follow the steps below, which is one of the approaches you can take:

  • You right click the BizTalk project in the solution explorer containing the pipeline(s).
  • You go the Deployment Tab and in the Unit Test section choose Enable Unit Testing. By enabling unit testing the schema(s) will inherit from either TestableReceivePipeline and/or TestableSendPipeline.
  • Add a new test project to your solution.
  • Set reference to Microsoft.BizTalk.TestTools.dll (<install drive>\Program Files (x86)\Microsoft BizTalk Server 2013\Developer Tools) , Microsoft.XLANGs.BaseTypes.dll (<install drive>\Program Files (x86)\Microsoft BizTalk Server 2013\), Microsoft.BizTalk.Pipeline (<install drive>\Program Files (x86)\Microsoft BizTalk Server 2013\) and the assembly containing the schema(s).
  • In the Unit Test Class you can test method like below for testing a custom receive pipeline:
[TestMethod()]
public void PipelineTNWikiAticleTagsUnitTest()
 {
  TNWikiAticleTags target = new TNWikiAticleTags();
 
  //Collection of messages to test the pipeline
  StringCollection documents = new StringCollection();
  string strTNWikiArticle_XML = "..\\..\\TNWikiArticleInstance.xml";
  Assert.IsTrue(File.Exists(strTNWikiArticle_XML));
  documents.Add(strTNWikiArticle_XML);
 
  // Only a body part for this test message so an empty 
  // collection will be passed.  
  StringCollection parts = new StringCollection();
 
  // Dictionary mapping the schema to the namespace and type
  // as displayed in the properties window for the *.xsd     
  Dictionary<string, string> schemas = new Dictionary<string, string>();
  string SchemaFile = "..//..//TNWikiArticle.xsd";
  Assert.IsTrue(File.Exists(SchemaFile));
  schemas.Add("BTS.TNWIKI.Schemas.TNWikiArticle", SchemaFile);
 
  // Test the execution of the pipeline using the inputs
  target.TestPipeline(documents, parts, schemas);
 
  // Validate that the pipeline test produced the message
  // which conforms to the schema.                       
  string[] strMessages = Directory.GetFiles(testContextInstance.TestDir + "\\out", "Message*.out");
  Assert.IsTrue(strMessages.Length > 0);
 
  Schemas.TNWikiArticle tnWikiArticles= new Schemas.TNWikiArticle tnWikiArticles();
  foreach (string outFile in strMessages)
   {
     Assert.IsTrue(tnWikiArticles.TNWikiArticleInstance(outFile, Microsoft.BizTalk.TestTools.Schema.OutputInstanceType.XML));
   }  
 }
  • You can test the method by clicking Run all Tests in Solution.

The pipeline class in your project is derived from the Microsoft.BizTalk.TestTools.Pipeline.TestableReceivePipeline class, which models some of the same functionality exposed by the Pipeline.exe tool (See Pipeline Tools).

See Also MSDN:

Wrap up

You can perform easy tests through Visual Studio on a schema using Validate Instance. Mapping can be tested in Visual Studio using Test Map. With Visual Studio you can also unit test the BizTalk artifacts schemas, maps and pipelines. Unfortunately you cannot unit test orchestrations. To unit orchestration you can use for instance using Event Tracing for Windows (ETW). This framework can also be used to test other BizTalk artifacts. In case you want to test pipeline components you can use available frameworks online (See blog post BizTalk Testing Series - Testing Pipeline Components).

See Also

Other suggested article to read:

Michael Stephenson has written an extensive series of blog post on testing in BizTalk Server (targeted on BizTalk 2009):

Another important place to find an extensive amount of BizTalk related articles is the TechNet Wiki itself. The best entry point is BizTalk Server Resources on the TechNet Wiki.