|
| 1 | +# XMLUnit.NET |
| 2 | + |
| 3 | +XMLUnit provides you with the tools to verify the XML you emit is the |
| 4 | +one you want to create. |
| 5 | + |
| 6 | +It provides helpers to validate against an XML Schema, assert the |
| 7 | +values of XPath queries or compare XML documents against expected |
| 8 | +outcomes. |
| 9 | + |
| 10 | +This package provides the core functionality and can be used |
| 11 | +stand-alone. In addition there are libraries providing NUnit |
| 12 | +constraints and a "placeholders" package that may simplify writing |
| 13 | +comparison tests in certain cases. |
| 14 | + |
| 15 | +* [XMLUnit.NUnit2.Constraints - Constraints for NUnit 2.x ](https://www.nuget.org/packages/XMLUnit.NUnit2.Constraints/) |
| 16 | +* [XMLUnit.NUnit3.Constraints - Constraints for NUnit 3.x ](https://www.nuget.org/packages/XMLUnit.NUnit3.Constraints/) |
| 17 | +* [XMLUnit.NUnit4.Constraints - Constraints for NUnit 4.x ](https://www.nuget.org/packages/XMLUnit.NUnit4.Constraints/) |
| 18 | +* [XMLUnit.Placeholders - simplifies comparisons for special cases ](https://www.nuget.org/packages/XMLUnit.Placeholders/) |
| 19 | + |
| 20 | +[] |
| 21 | + |
| 22 | +## Requirements |
| 23 | + |
| 24 | +XMLUnit requires .NET Standard 2.0 (tested with .NET 8 rigt now) and |
| 25 | +should still support .NET Framework 3.5 and Mono. |
| 26 | + |
| 27 | +The core library hasn't got any dependencies itself. |
| 28 | + |
| 29 | +## Usage |
| 30 | + |
| 31 | +These are some really small examples, more is available as part of the |
| 32 | +[user guide](https://github.com/xmlunit/user-guide/wiki) |
| 33 | + |
| 34 | +### Comparing Two Documents |
| 35 | + |
| 36 | +```csharp |
| 37 | +ISource control = Input.FromFile("test-data/good.xml").Build(); |
| 38 | +ISource test = Input.FromByteArray(CreateTestDocument()).Build(); |
| 39 | +IDifferenceEngine diff = new DOMDifferenceEngine(); |
| 40 | +diff.DifferenceListener += (comparison, outcome) => { |
| 41 | + Assert.Fail("found a difference: {}", comparison); |
| 42 | + }; |
| 43 | +diff.Compare(control, test); |
| 44 | +``` |
| 45 | + |
| 46 | +or using the fluent builder API |
| 47 | + |
| 48 | +```csharp |
| 49 | +Diff d = DiffBuilder.Compare(Input.FromFile("test-data/good.xml")) |
| 50 | + .WithTest(CreateTestDocument()).Build(); |
| 51 | +Assert.IsFalse(d.HasDifferences()); |
| 52 | +``` |
| 53 | + |
| 54 | +### Asserting an XPath Value |
| 55 | + |
| 56 | +```csharp |
| 57 | +ISource source = Input.FromString("<foo>bar</foo>").Build(); |
| 58 | +IXPathEngine xpath = new XPathEngine(); |
| 59 | +IEnumerable<XmlNode> allMatches = xpath.SelectNodes("/foo", source); |
| 60 | +string content = xpath.evaluate("/foo/text()", source); |
| 61 | +``` |
| 62 | + |
| 63 | +### Validating a Document Against an XML Schema |
| 64 | + |
| 65 | + |
| 66 | +```csharp |
| 67 | +Validator v = Validator.ForLanguage(Languages.W3C_XML_SCHEMA_NS_URI); |
| 68 | +v.SchemaSources = new ISource[] { |
| 69 | + Input.FromUri("http://example.com/some.xsd").Build(), |
| 70 | + Input.FromFile("local.xsd").Build() |
| 71 | + }; |
| 72 | +ValidationResult result = v.ValidateInstance(Input.FromDocument(CreateDocument()).Build()); |
| 73 | +bool valid = result.Valid; |
| 74 | +IEnumerable<ValidationProblem> problems = result.Problems; |
| 75 | +``` |
| 76 | + |
| 77 | +## Additional Documentation |
| 78 | + |
| 79 | +XMLUnit.NET is developed at |
| 80 | +[github](https://github.com/xmlunit/xmlunit.net). More documentation, |
| 81 | +releases and an issue tracker can be found there. |
| 82 | + |
| 83 | +## Changelog |
| 84 | + |
| 85 | +See the [Release |
| 86 | +Notes](https://github.com/xmlunit/xmlunit.net/blob/main/RELEASE_NOTES.md) |
| 87 | +at github. |
0 commit comments