Search code examples
.netxmlmstest

XML unit testing frameworks for MSTest


We need to validate the content of an xml document we generate. Are there any frameworks out there that will help make it easy to extract and validate the content of the document?

things we need to know

  • contains element x
  • value of element x
  • element x contains attribute y with value z
  • unexpected elements

It doesn't matter to much if there are no assertions, this can be done via MSTest


Solution

  • Take a look at Fluent Assertions library documentation here: https://fluentassertions.com/introduction

    The section entitled "XML classes" sounds like what you're after. The code uses LINQ-to-XML underneath.

    xDocument.Should().HaveRoot("configuration");
    xDocument.Should().HaveElement("settings");
    
    xElement.Should().HaveAttribute("age", "36");
    xElement.Should().HaveElement("address");
    
    xAttribute.Should().HaveValue("Amsterdam");
    

    You can install FluentAssertions from nuget here: http://nuget.org/packages/FluentAssertions