2

I am trying to Approvals.Verify xml but problem is attributes' order. In few elements they are in different order in my computerand Jenkins. From other question I found to change order of properties in class. That kinda worked: it changed attributes' order but it now Jenkins' order is what it used to be in my computer and vice versa. So no help from changed properties' order in class.

Is there any way to use ApprovalTests to ignore xml attributes' order? Or how can I say exact order for attributes in xml serialization? Or other hints?

Community
  • 1
  • 1
Lassi Autio
  • 1,147
  • 1
  • 13
  • 35
  • Sounds like you need to have a quiet but very intense discussion with Jenkins about not scrambling his attributes. In fact, quietly disposing of Jenkins himself may be your best option. But if you could provide enough code to reproduce the issue, that could be another way to address your problem. – 15ee8f99-57ff-4f92-890c-b56153 Oct 06 '16 at 20:25
  • According to the [XML Standard Section 3.1](https://www.w3.org/TR/REC-xml/#sec-starttags), **the order of attribute specifications in a start-tag or empty-element tag is not significant.** So if ApprovalTests is not ignoring attribute order, then that's a bug in ApprovalTests. For comparison [`XNode.DeepEquals()`](https://msdn.microsoft.com/en-us/library/system.xml.linq.xnode.deepequals(v=vs.110).aspx) ignores attribute order, so maybe you could use that. – dbc Oct 06 '16 at 20:51
  • That being said, from the [reference source](https://github.com/approvals/ApprovalTests.Net), it appears [`XmlApprovals.VerifyOrderedXml()`](https://github.com/approvals/ApprovalTests.Net/blob/master/ApprovalTests/Xml/XmlApprovals.cs) tries to normalize the order of attributes. Is this the method you are using? If not, can you provide a [mcve]? – dbc Oct 06 '16 at 21:05
  • @dbc XNode.DeepEquals() does NOT ignore attribute order.. I just run into that issue. So I found out that the best way to compare two XMLs for differencies is using XmlDiff, you can check it here: https://learn.microsoft.com/en-us/previous-versions/dotnet/articles/aa302294(v=msdn.10)?redirectedfrom=MSDN – Marcin Jun 03 '22 at 08:19

1 Answers1

3

According to the XML Standard Section 3.1, the order of attribute specifications in a start-tag or empty-element tag is not significant. So a change in XML attribute ought not trigger a unit test failure.

And, from the reference source, it appears that the method XmlApprovals.VerifyOrderedXml() normalizes the order of attributes before validation by internally calling XmlUtils.FormatXmlWithOrderedAttributes() which recursively sorts all attributes. Thus this method should meet your needs.

janv8000
  • 1,569
  • 2
  • 19
  • 33
dbc
  • 104,963
  • 20
  • 228
  • 340