test_xml 0.1 released – Testing your XML has never been easier!

A couple of days ago Pavel Gabriel and I released a refreshed version of the “test_xml”:https://github.com/alovak/test_xml gem.

test_xml helps you *asserting that XML is the way your specification wants it.* It provides some simple assertions and runs with Test::Unit, MiniTest, RSpec2 and Cucumber. It’s perfect for testing REST services emitting XML representations. Internally, it uses Nokogiri for node matching.

h3. Overview

Say your *_expecting_ the following XML* document from your service (or whatever).


Now it is pretty tedious testing this against a string with regexps while skipping newlines or whitespaces and so on. *Use test_xml for that!*

assert_xml_equal expected, "1

As you’re using assert_xml_equal this will fail – the subject is missing the @flavor@ attribute and thus is not _equal_!

We can *do that in RSpec* as well!

  expected.should equal_xml expected

This will work. Obviously.

h3. Testing structure, ignoring content

Sometimes you might want to *test the tag structure* without respecting the content or attributes.

assert_xml_structure_equal expected, "
  One million

Turns out to be true.

h3. Asserting parts, only

Sometimes you might want to check that your XML contains some markup block, which doesn’t need to be the very image of your specification.

assert_xml_contain expected, "

Works! This is especially helpful if you’re interested in certain blocks of your markup and not the entire document.

h3. Assertions?

What we got is

* @assert_xml_equal@
* @assert_xml_contain@
* @assert_xml_structure_equal@
* @assert_xml_structure_contain@

Pretty obvious what they’re doing, isn’t it? Use ’em in RSpec if you feel like it.

h3. Give it a try!

Now go and check out “the test_xml gem”:https://github.com/alovak/test_xml and feel safer when rendering XML. Testing FTW!



One thought on “test_xml 0.1 released – Testing your XML has never been easier!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s