XML Schemas and Unit Tests

Florian CAILLAUD
Published by Florian CAILLAUD
Category : BizTalk
13/04/2018

During BizTalk development, XML schema creation is almost inevitable. Once the schema is built, it is a good practice to validate it (using the “Validate Schema” and “Validate Instance” features). This check, more than useful, is only carried out punctually. But it seems quite complicated to guarantee the validity and/or non-regression of all the schemas in several BizTalk projects.

In order to control the validity of the different schemas, it is possible to test them within a unit test project. To do this, we will first create a BizTalk project containing an XML schema:
unit test xml schema project

 

You then have to go to the BizTalk project properties to set the “Enable Unit Testing” option to “True”.

unit test xml schema properties

 

This action will have the effect of inheriting the various schemas in the project from the Microsoft.BizTalk.TestTools.Schema.TestableSchemaBase class, which itself inherits from Microsoft.XLANGs.BaseTypes.SchemaBase (so we retain all the initial properties of an XML schema).

It is possible to check the inheritance change by opening the C# file associated with the schema.

unit test xml schema code view

 

 

One of the limitations of this approach is that the Microsoft.BizTalk.TestTools assembly is not natively present on the Production version of BizTalk Server. Then there are 2 solutions:

  • Add this assembly to your production environment ;
  • Define a Debug profile (with the option “Enable Unit Testing” to “True”), and a Release profile (with the option to “false”).

The second solution being, in my point of view, cleaner than the first one. In a second step, and to facilitate the setting up of unit tests, we will create a SchemaValidator class (in another project). This class will be intended to provide the static “Validate” method. The latter will return “True” if the file (whose path is given as a parameter) is valid from the point of view of the schema (also given as a parameter).

unit test xml schema validator

 

using System;
using Microsoft.XLANGs.BaseTypes;
using Microsoft.BizTalk.TestTools.Schema;
using System.Xml;
using System.Xml.Schema;

namespace SchemaValidationHelpers
{

public class SchemaValidator
{

public static bool Validate(string xmlPath, SchemaBase schema, OutputInstanceType schemaType, out string errorMessage)
{

string err = String.Empty;

//Validation settings Initialisation
XmlReaderSettings settings = new XmlReaderSettings();
settings.ValidationType = ValidationType.Schema;
settings.Schemas = schema.SchemaSet;
settings.ValidationFlags |= XmlSchemaValidationFlags.ProcessInlineSchema;
settings.ValidationFlags |= XmlSchemaValidationFlags.ProcessSchemaLocation;
settings.ValidationFlags |= XmlSchemaValidationFlags.ReportValidationWarnings;
settings.ValidationEventHandler += new ValidationEventHandler((s, a) =>
{
if (a.Severity == XmlSeverityType.Warning)
err += “\n\tValidation warning: ” + a.Message;
else
err += “\n\tValidation error: ” + a.Message;
});

// Create the XmlReader object.
XmlReader reader = XmlReader.Create(xmlPath, settings);

// Parse the file.
while (reader.Read()) ;

errorMessage = err;

return String.IsNullOrEmpty(errorMessage);

}

}

}

Finally, we create a unit testing project. This refers to the first 2 projects and contains this class:

namespace UnitTestProject
{

[TestClass]
public class UnitTestExample
{

[TestMethod]
public void Validate_AdressSchema_FirstMethod()
{

Address schema = new Address();
string strSourceXML = @”..\..\Sample\testAdress1.xml”;

bool result = schema.ValidateInstance(strSourceXML, OutputInstanceType.XML);
Assert.IsTrue(result);

}

[TestMethod]
public void Validate_AdressSchema_SecondMethod()
{

Address schema = new Address();
string strSourceXML = @”..\..\Sample\testAddress1.xml”;
string errorMessage = String.Empty;

//Second Method
bool result = SchemaValidator.Validate(strSourceXML, schema, OutputInstanceType.XML, out errorMessage);
Assert.IsTrue(result, errorMessage);

}

}

}

This test class allows you to validate an XML file in 2 different ways. The first one uses the ValidateInstance method of TestableSchemaBase. Unfortunately, if an error occurs, it is not possible to obtain additional information. So, you’ll have to go through the Visual Studio feature “Validate Instance” to learn more. (This way does not use the helper class described above)

The second approach uses the helper class and its “Validate” method. The advantage is that it is possible to recover all errors (and warnings according to the XmlReaderSettings used).

With these elements, it is possible to simply set up unit tests related to XML schemas. Thus, the validity and non-regression of the various schemas can be ensured throughout the developments.