Jolt.Testing : Test Framework Adapters for XML Assertions

Note: This page describes the XML assertion adaptors for the core XML assertions in the Jolt.Testing library. For a description of the core XML assertions, please refer to this page.

Table of Contents

  1. NUnit Usage Examples
    1. Using XmlValidityAssertion with NUnit
    2. Using XmlEqualityAssertion with NUnit
    3. Using XmlEquivalencyAssertion with NUnit
  2. Visual Studio Usage Examples
    1. Using XmlValidityAssertion with Visual Studio
    2. Using XmlEqualityAssertion with Visual Studio
    3. Using XmlEquivalencyAssertion with Visual Studio

NUnit Usage Examples

The XML assertion adaptors for NUnit are available in the Jolt.Testing.Assertions.NUnit.dll assembly and their usage is demonstrated in the following sections.


The XmlValidityConstraint class adapts the XmlValidityAssertion class for use through the NUnit constraint syntax. An NUnit-style syntax helper is also available for use, as demonstrated in the following example.

using System.Xml;
using System.Xml.Schema;
using Jolt.Testing.Assertions.NUnit;
using Jolt.Testing.Assertions.NUnit.SyntaxHelpers;
using NUnit.Framework;
using NUnit.Framework.SyntaxHelpers;
 
[Test]
void ValidateXml()
{
  using (XmlReader xml = LoadXml(),
                   otherXml = LoadXml())
  {
    XmlSchemaSet schemas = LoadXmlSchemas();
 
    // Uses default validation style.
    Assert.That(xml, new XmlValidityConstraint(schemas));
 
    // Uses syntax helper and optional validation style.
    Assert.That(otherXml, IsXml.ValidWith(schemas, XmlSchemaValidationFlags.ProcessInlineSchema));
  }
}


The XmlEqualityConstraint class adapts the XmlEqualityAssertion class for use through the NUnit constraint syntax. An NUnit-style syntax helper is also available for use, as demonstrated in the following example.

using System.Xml;
using Jolt.Testing.Assertions.NUnit;
using Jolt.Testing.Assertions.NUnit.SyntaxHelpers;
using NUnit.Framework;
using NUnit.Framework.SyntaxHelpers;
 
[Test]
void AreEqual()
{
  using (XmlReader expectedXml = LoadExpectedXml(),
                   actualXml = LoadActualXml(),
                   otherExpectedXml = LoadExpectedXml(),
                   otherActualXml = LoadActualXml()))
  {
    Assert.That(actualXml, new XmlEqualityConstraint(expectedXml));
    Assert.That(otherActualXml, IsXml.EqualTo(otherExpectedXml));    // Uses syntax helper.
  }
}


The XmlEquivalencyConstraint class adapts the XmlEquivalencyAssertion class for use through the NUnit constraint syntax. An NUnit-style syntax helper is also available for use, as demonstrated in the following example.

using System.Xml;
using Jolt.Testing.Assertions.NUnit;
using Jolt.Testing.Assertions.NUnit.SyntaxHelpers;
using NUnit.Framework;
using NUnit.Framework.SyntaxHelpers;
 
[Test]
void AreEquivalent()
{
  using (XmlReader expectedXml = LoadExpectedXml(),
                   actualXml = LoadActualXml(),
                   otherExpectedXml = LoadExpectedXml(),
                   otherActualXml = LoadActualXml()))
  {
    // Implied equivalency level is XmlComparisonFlags.Strict.
    Assert.That(actualXml, new XmlEquivalencyConstraint(expectedXml));
 
    // Uses syntax helper with implied equivalency level.
    Assert.That(otherActualXml, IsXml.EquivalentTo(otherExpectedXml));
  }
}

To vary the equivalency strictness, chain the invocation of properties on the XmlEquivalencyConstraint class as follows.

using System.Xml;
using Jolt.Testing.Assertions.NUnit;
using Jolt.Testing.Assertions.NUnit.SyntaxHelpers;
using NUnit.Framework;
using NUnit.Framework.SyntaxHelpers;
 
[Test]
void AreEquivalent()
{
  using (XmlReader expectedXml = LoadExpectedXml(),
                   actualXml = LoadActualXml())
  {
    Assert.That(otherActualXml, IsXml.EquivalentTo(otherExpectedXml).IgnoreAttributes.IgnoreSequenceOrder);
  }
}

Visual Studio Usage Examples

The XML assertion adaptors for Visual Studio are available in the Jolt.Testing.Assertions.VisualStudio.dll assembly and their usage is demonstrated in the following sections.


The XmlAssert.IsValid() method adapts the XmlValidityAssertion class for use through Visual Studio, as demonstrated in the following example.

using System.Xml;
using System.Xml.Schema;
using Jolt.Testing.Assertions.VisualStudio;
using Microsoft.VisualStudio.TestTools.UnitTesting;
 
[TestMethod]
void ValidateXml()
{
  using (XmlReader xml = LoadXml(),
                   otherXml = LoadXml())
  {
    XmlSchemaSet schemas = LoadXmlSchemas();
 
    // Use default validation flags
    XmlAssert.IsValid(schemas, xml);

    // Use explicit validation flags.
    XmlAssert.IsValid(schemas, XmlSchemaValidationFlags.ProcessInlineSchema, otherXml);
  }
}


The XmlAssert.AreEqual() method adapts the XmlEqualityAssertion class for use through Visual Studio, as demonstrated in the following example.

using System.Xml;
using Jolt.Testing.Assertions.VisualStudio;
using Microsoft.VisualStudio.TestTools.UnitTesting;
 
[TestMethod]
void AreEqual()
{
  using (XmlReader expectedXml = LoadExpectedXml(),
                   actualXml = LoadActualXml())
  {
    XmlAssert.AreEqual(expectedXml, actualXml);
  }
}


The XmlAssert.AreEquivalent() method adapts the XmlEquivalencyAssertion class for use through Visual Studio, as demonstrated in the following example.

using System.Xml;
using Jolt.Testing.Assertions;
using Jolt.Testing.Assertions.VisualStudio;
using Microsoft.VisualStudio.TestTools.UnitTesting;
 
[TestMethod]
void AreEquivalent()
{
  using (XmlReader expectedXml = LoadExpectedXml(),
                   actualXml = LoadActualXml())
  {
    XmlAssert.AreEquivalent(XmlComparisonFlags.IngoreElementValues, expectedXml, actualXml);
  }
}

Last edited Jan 1, 2010 at 7:49 PM by SteveGuidi, version 3

Comments

No comments yet.