Overview
This is an open source, .NET Standard 2.0 library that provides helpers for comparing text and flat files (using XMLUnit.NET), optionally in conjunction with one of the ChannelAdam Test Framework Libraries:
Below are the main features of the library. See the linked code for details.
Please contact me if you have any questions.
Getting Started
NuGet Package Installation
To install the ChannelAdam.TestFramework.Xml NuGet package
run the following command in the Package Manager Console:
1PM> Install-Package ChannelAdam.TestFramework.Xml
Usage
1using ChannelAdam.TestFramework.Xml;
2...
3 [TextFixture]
4 public class MyUnitTests : MoqTestFixture
5 {
6 private XmlTester _xmlTester;
7
8 [SetUp]
9 public Setup()
10 {
11 _xmlTester = new XmlTester(base.LogAssert);
12 }
13
14 [Test]
15 public void MyTestMethod()
16 {
17 // ARRANGE
18 _xmlTester.ArrangeExpectedXml(@"<a><b>hi</b></a>");
19
20 // ACT
21 Logger.Log("About to do the ACT part of blah blah... so the test output reads like a story");
22 _xmlTester.ArrangeActualXml(@"<a><c>c</c></a>");
23
24 // ASSERT
25 Logger.Log("Comparing...");
26 LogAssert.IsTrue("XML samples are different", _xmlTester.IsEqual());
27 // OR
28 _xmlTester.AssertActualXmlEqualsExpectedXml();
29 // The test output will have a helpful Diff.
30 }
31 }
See XmlTestingUnitSteps.cs for an example of using the XmlTester
helper.
Reference
XML Tester
The XmlTester class
is used in tests for asserting whether the actual XML is similar enough to the given expected XML.
Under the covers, it uses the XMLUnit.NET open source library to identify differences.
The XmlTester
class has the following properties:
XElement ActualXml
XElement ExpectedXml
Diff Differences
The XmlTester
class has the following Arrange methods:
void ArrangeActualXml(Assembly assembly, string resourceName)
- to arrange the actual XML from an embedded resource in the given assembly
void ArrangeActualXml(XElement xmlElement)
- to arrange the actual XML from a given XElement
void ArrangeActualXml(object valueToSerialise)
- to arrange the actual XML by serialising the given object
void ArrangeActualXml(object valueToSerialise, XmlRootAttribute xmlRootAttribute)
- to arrange the actual XML by serialising the given object and applying the given XmlRootAttribute
during the serialisation
void ArrangeActualXml(object valueToSerialise, XmlAttributeOverrides xmlAttributeOverrides)
- to arrange the actual XML by serialising the given object and applying the given XmlAttributeOverrides
during the serialisation
void ArrangeActualXml(string xmlValue)
- to arrange the actual XML from the given XML string
void ArrangeExpectedXml(Assembly assembly, string resourceName)
- to arrange the expected XML from an embedded resource in the given assembly
void ArrangeExpectedXml(XElement xmlElement)
- to arrange the expected XML from a given XElement
void ArrangeExpectedXml(object valueToSerialise)
- to arrange the expected XML by serialising the given object
void ArrangeExpectedXml(object valueToSerialise, XmlRootAttribute xmlRootAttribute)
- to arrange the expected XML by serialising the given object and applying the given XmlRootAttribute
during the serialisation
void ArrangeExpectedXml(object valueToSerialise, XmlAttributeOverrides xmlAttributeOverrides)
- to arrange the expected XML by serialising the given object and applying the given XmlAttributeOverrides
during the serialisation
void ArrangeExpectedXml(string xmlValue)
- to arrange the expected XML from the given XML string
The XmlTester
class has the following Assert methods:
virtual void AssertActualXmlEqualsExpectedXml()
- to assert the actual XML against the expected XML
virtual void AssertActualXmlEqualsExpectedXml(IXmlFilter xmlFilter)
- to assert the actual XML against the expected XML, ignoring items listed in the given IXmlFilter
(see below)
bool IsEqual()
- determines if the given actual and expected XML is equivalent
bool IsEqual(XNode expected, XNode actual)
- determines if the given actual and expected XNode XML is equivalent
virtual bool IsEqual(XmlNode expected, XmlNode actual)
- determines if the given actual and expected XmlNode XML is equivalent
Ignoring / Filtering XML Elements
The AssertActualXmlEqualsExpectedXml(IXmlFilter xmlFilter)
override allows you to specify an
IXmlFilter interface
which allows you to specify a list of the local names of XML elements to ignore in comparisons, and/or a list of XPath expressions to ignore in comparisons.
Examples
For a quick example, please see the Behaviour Specification for the
XmlTester class
and the
XML tester code
XML Filter
The XmlFilter class
allows you to specify a list of the local names of XML elements to ignore in comparisons, and/or a list of XPath expressions to ignore in comparisons.
Two constructor methods allow you to easily specify these lists.
XmlFilter(IList<string> elementLocalNamesToIgnore)
XmlFilter(IList<string> elementLocalNamesToIgnore, IList<string> xpathsToIgnore)
XML Asserter
The XmlAsserter class
provides utility methods for performing assertions on XML values.
It has the following methods:
void XPathValueEquals(string description, string xpath, XNode rootElement, string expected)
- to assert that the given XPath of the provided XNode has the given expected string value
void XPathValueEquals(string description, string xpath, XNode rootElement, XmlNamespaceManager namespaceManager, string expected)
- uses the provided namespace manager to assert that the given XPath of the provided XNode has the given expected string value
void XPathValuesAreEqual(string description, string xpath, XNode expectedElements, XNode actualElements)
- to assert that the value of the XPath in the given actual XNode equals the corresponding value in the given expected XNode
void XPathValuesAreEqual(string description, string expectedXpath, XNode expectedElements, string actualXpath, XNode actualElements)
- to assert that the value of the actual XPath in the actual XNode is the same as the value of the expected XPath in the expected XNode
void XPathValuesAreEqual(string description, string expectedXpath, XNode expectedElements, XmlNamespaceManager expectedNamespaceManager, string actualXpath, XNode actualElements, XmlNamespaceManager actualNamespaceManager)
- to assert that the value of the actual XPath in the actual XNode is the same as the value of the expected XPath in the expected XNode, using the provided XML namespace managers
void AreEqual(XElement expectedXml, XElement actualXml)
- to assert that the given expected XElement is equivalent to the actual XElement
void AreEqual(XElement expectedXml, XElement actualXml, IXmlFilter xmlFilter)
- to assert that the given expected XElement is equivalent to the actual XElement - using the given IXmlFilter
Mapping Testers
There are 3 map tester classes to be used depending on the type of input and output of your map:
The MappingFromXmlToXmlTester class
has the following method overrides for arranging the input XML.
void ArrangeInputXml(Assembly assembly, string resourceName)
- to arrange the input XML from an embedded resource in the given assembly
void ArrangeInputXml(XElement xmlElement)
- to arrange the input XML from a given XElement
void ArrangeInputXml(object valueToSerialise)
- to arrange the input XML by serialising the given object
void ArrangeInputXml(object valueToSerialise, XmlRootAttribute xmlRootAttribute)
- to arrange the input XML by serialising the given object and applying the given XmlRootAttribute
during the serialisation
void ArrangeInputXml(object valueToSerialise, XmlAttributeOverrides xmlAttributeOverrides)
- to arrange the input XML by serialising the given object and applying the given XmlAttributeOverrides
during the serialisation
void ArrangeInputXml(string xmlValue)
- to arrange the input XML from the given XML string
void ArrangeExpectedOutputXml(Assembly assembly, string resourceName)
- to arrange the expected output XML from an embedded resource in the given assembly
void ArrangeExpectedOutputXml(XElement xmlElement)
- to arrange the expected output XML from a given XElement
void ArrangeExpectedOutputXml(object valueToSerialise)
- to arrange the expected output XML by serialising the given object
void ArrangeExpectedOutputXml(object valueToSerialise, XmlRootAttribute xmlRootAttribute)
- to arrange the expected output XML by serialising the given object and applying the given XmlRootAttribute
during the serialisation
void ArrangeExpectedOutputXml(object valueToSerialise, XmlAttributeOverrides xmlAttributeOverrides)
- to arrange the expected output XML by serialising the given object and applying the given XmlAttributeOverrides
during the serialisation
void ArrangeExpectedOutputXml(string xmlValue)
- to arrange the expected output XML from the given XML string
Input can be arranged from an embedded resource, XElement, object that is XML serialisable (for which you can also override the root XML namespace when serialised) or simply a string.
This same pattern is followed for arranging the expected output of the map, and for arranging the contents of flat files with the other map tester classes.
Assert - Compare the Actual Output Against the Expected Output
The MappingFromXmlToXmlTester class
has the following method overrides for asserting the actual output from the map against the expected output that was arranged.
void AssertActualOutputXmlEqualsExpectedOutputXml()
AssertActualOutputXmlEqualsExpectedOutputXml(IXmlFilter xmlFilter)
(see below)
All the comparison and logging and formatting of any differences is done for you. Easy!
Ignoring / Filtering XML Elements
The AssertActualOutputXmlEqualsExpectedOutputXml(IXmlFilter xmlFilter)
override allows you to specify an
IXmlFilter
which allows you to specify a list of the local names of XML elements to ignore in comparisons, and/or a list of XPath expressions to ignore in comparisons.
Two constructor methods on XmlFilter
allow you to easily specify these lists.
XmlFilter(IList<string> elementLocalNamesToIgnore)
XmlFilter(IList<string> elementLocalNamesToIgnore, IList<string> xpathsToIgnore)