0

Using NUnit and XMLUnit.NET
I have two test which validate generated Xml document against the schema
First one testing only required elements

<Test>
Public Sub GenerateXmlDocument_WithOnlyRequiredElements_SuccesfullyValidated()
    Dim factory As New TestFactory()
    Dim schemaConstraint As SchemaValidConstraint = factory.GreateSchemaConstraint()
    Dim invoice As factory.CreateInvoiceWithOnlyRequiredValues()
    Dim service As New MyService()

    Dim result As XmlDocument = service.GenerateXmlDocument(invoice)
    Dim documentSource As ISource = Input.FromDocument(result).Build()

    Assert.That(documentSource, schemaConstraint)
End Sub

Second testing all elements

<Test>
Public Sub GenerateXmlDocument_WithAllElements_SuccesfullyValidated()
    Dim factory As New TestFactory()
    Dim schemaConstraint As SchemaValidConstraint = factory.GreateSchemaConstraint()
    Dim invoice As factory.CreateInvoiceWithAllValues()
    Dim service As New MyService()

    Dim result As XmlDocument = service.GenerateXmlDocument(invoice)
    Dim documentSource As ISource = Input.FromDocument(result).Build()

    Assert.That(documentSource, schemaConstraint)
End Sub

Both tests cover almost all needed cases as element's type, order and requiring.
Except situation when schema contain a element with minOccurs="0" maxOccurs="1" and that element missing from Xml document generated in the second test(WithAllElements).

Question: Is there a assert/validate method which can handled all elements in schema as required?

I want this for situation when new element need to be added. Then adding new element, event with minOccurs="0" will break a test.

Fabio
  • 31,528
  • 4
  • 33
  • 72

1 Answers1

0

Schema doesn't really help you here, I'm afraid. And there is no built-in way in XMLUnit.

You might test against a manipulated schema where you modify all minOccurs="0" in an automated way - or you could try to automatically create XPath tests for your optional elements.

Stefan Bodewig
  • 3,260
  • 15
  • 22