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.
To create a unit test project
We need two test class, one for testing the Schema and the other to test the map. We can use the UnitTest1.cs that was generated by the project template, but we should give the file and class more descriptive names. We can do that in one step by renaming the file in Solution Explorer.
using
Microsoft.VisualStudio.TestTools.UnitTesting;
System;
System.Collections.Generic;
System.Linq;
System.Text;
System.Threading.Tasks;
UnitTestingFeatureWithMaps.Schemas;
namespace
UnitTestProject1
{
/// <summary>
///This is a test class for PersonOriginTest and is intended
///to contain all PersonOriginTest Unit Tests
///</summary>
[TestClass()]
public
class
PersonOriginTest
private
TestContext testContextInstance;
///Gets or sets the test context which provides
///information about and functionality for the current test run.
TestContext TestContext
get
return
testContextInstance;
}
set
testContextInstance = value;
///A test for PersonOrigin Constructor
[TestMethod()]
void
PersonOriginConstructorTest()
PersonOrigin target =
new
PersonOrigin();
//=== Schema input file for validation ===//
string
strSourcePO_XML = testContextInstance.TestDir +
"..\\..\\..\\Files\\PersonOrigin.xml"
;
//=== Validate the XML Input message against the schema ===//
Assert.IsTrue(target.ValidateInstance(strSourcePO_XML,
Microsoft.BizTalk.TestTools.Schema.OutputInstanceType.XML));
// <summary>
PersonOriginWithoutPhoneCallsConstructorTest()
"..\\..\\..\\Files\\PersonOrigin2.xml"
PersonOriginWithoutZipConstructorTest()
"..\\..\\..\\Files\\PersonOriginWithoutZip.xml"
UnitTestingFeatureWithMaps.Maps;
System.IO;
Microsoft.BizTalk.TestTools;
///This is a test class for HowMapsWorksTest and is intended
///to contain all HowMapsWorksTest Unit Tests
HowMapsWorksTest
HowMapsWorksMapTest()
/*********************************************************************************
* There is a bug with Map Unit Test inside Microsoft.BizTalk.TestTools.dll
* Microsoft had missed on to upgrade TestableMapBase class. They still using the
* BTSXslTransform instead of using XslCompiledTransform which will cause the
* TestMap() function to failed.
*
* The following code was the expected code for BizTalk Map unit testing
*********************************************************************************/
HowMapsWorks map =
HowMapsWorks();
//=== Use the HelloWorld sample directory path for the message files ===//
strDestInvoice_XML = 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));
HowMapsWorksWithoutPhoneCallsMapTest()
* BTSXslTransform instead of using XslCompiledTransform witch will cause the
"\\OUT\\PersonTargetWithoutTotals.xml"
#endregion
HowMapsWorksWithoutZipMapTest()
"\\OUT\\PersonTargetWithoutZip.xml"
Use Test Explorer to run unit tests from Visual Studio unit test projects. You can also debug tests and analyze test performance and code coverage. When you build the test project, the tests appear in Test Explorer and as you run, write, and rerun your tests, Test Explorer displays the results in default groups of Failed Tests, Passed Tests, Skipped Tests and Not Run Tests. You can change the way Test Explorer groups your tests. If Test Explorer windows is not visible:
You can run all the tests in the solution, all the tests in a group, or a set of tests that you select. Do one of the following:
The pass/fail bar at the top of the Test Explorer window is animated as the tests run. At the conclusion of the test run, the pass/fail bar turns green if all tests passed or turns red if any test failed.
See more about Running Unit Tests with Test Explorer here.
Unfortunately there is a bug in BizTalk Server 2013 with Map Unit Test inside Microsoft.BizTalk.TestTools.dll, so each time we try to run the unit test for the map it give us the following error:
Microsoft.BizTalk.TestTools.BizTalkTestAssertFailException: Transform Failure Result StackTrace: at Microsoft.BizTalk.TestTools.Mapper.TestableMapBase.PerformTransform(String inputXmlFile, String outputXmlFile) at Microsoft.BizTalk.TestTools.Mapper.TestableMapBase.TestMap(String inputInstanceFilename, InputInstanceType inputType, String outputInstanceFilename, OutputInstanceType outputType)
Microsoft had missed on to upgrade TestableMapBase class. They still using the BTSXslTransform instead of using XslCompiledTransform witch will cause the TestMap() function to failed. You can find a wrapper (provide by shadabanwer) here: Map Unit test does not work in BizTalk 2013 because TestableMapBase class is not correct. However there is also a problem with schema (input and output) validation options... so I decide to recreated a new custom wrapper based on Microsoft.BizTalk.TestTools.dll and the solution provided by shadabanwer and fixed all the problems because validating the output instance generated by the map is an important step to validate your maps using Unit Testing. You can find this DLL here: BizTalk Server 2013: Wrapper classes to perform Unit Testing in Maps You must use this workaround until Microsoft fix this bug. So to be able to successfully test our maps we need to:
//=== Map input file instance to be mapped ===//
//=== Path for the Map output file instance with the result of the transformation ===//
"\\Out\\PersonTarget2.xml"
//WORKAROUND SOLUTION to test maps
SandroPereira.BizTalk.MapTestTools.TestableMapBase mapper =
SandroPereira.BizTalk.MapTestTools.TestableMapBase();
mapper.Mapper = map;
mapper.Mapper.ValidateOutput =
//=== Test the map by using the TestMap method of a custom TestableMapBase class ===//
//=== that uses the XslCompiledTransform. This class is basically an improved ===//
//=== clone of the class present in the Microsoft.BizTalk.TestTools DLL ===//
mapper.TestMap(strSourcePO_XML,
"\\Out\\PersonTargetWithoutZip.xml"
Unit tests in Visual Studio 2012 can be configured by using a .runsettings file. For example, you can change the .NET Framework on which the tests will be run, the directory where test results are delivered, and the data collected during a test run. .runsettings is new in Visual Studio 2012. If you’re familiar with unit testing in previous versions of Visual Studio, you might know about .testsettings files. You can still use .testsettings in Visual Studio 2012, so any test configurations you wrote for previous editions will still work. But .testsettings can be used only to configure tests written for the MSTest adapter. By contrast, .runsettings can be used with any of the adapters built for the extensible unit test framework in Visual Studio 2012, such as xUnit.net and NUnit. Tests that use .testsettings files might run more slowly than tests that use .runsettings files, or for which there is no configuration file at all. You still need a .testsettings file for some kinds of tests:
So to fixed this problem using the Test Settings file, you could manually create a test settings file for example with notepad, let's call:
And add the following content:
<?
xml
version
=
"1.0"
encoding
"UTF-8"
?>
<
TestSettings
name
"Local"
id
"6bfb00a1-f0ec-4f57-81ab-89ee852f1e49"
xmlns
"http://microsoft.com/schemas/VisualStudio/TeamTest/2010"
>
Description
>These are default test settings for a local test run.</
Deployment
enabled
"false"
/>
Execution
TestTypeSpecific
AgentRule
"Execution Agents"
</
So if we perform the tests again, we will notice that the "Microsoft.BizTalk.TestTools.BizTalkTestAssertFailException: Transform Failure" is solved and the only error that is happening is about a Map output validation failure (intentionally present)