/test_xml

Small library to test your xml with Test::Unit or RSpec

Primary LanguageRuby

TestXml

DESCRIPTION:

TestXml is a small extension for RSpec and TestUnit for XML/HTML testing. I have found it very useful for API testing with our Cucumber tests.

FEATURES:

  • test XML with: RSpec, Test::Unit, Cucumber

  • test XML structure (partially or fully matched)

  • test the element values within XML structure

EXAMPLES:

Cucumber feature

Scenario:
  Given data
  When I post the data
  The I receive successful response
  And response matches the following xml
  """
    <transaction>
      <status>success</status>
      <id/>
      <order_id/>
    </transaction>
  """

The scenario will check:

  • ‘status’ element and its value.

  • ‘id’ and ‘order_id’ elements are present in XML

USAGE:

RSpec

in spec_helper.rb

require 'test_xml'
require 'test_xml/spec'

in spec file:

it "should match_xml" do
  xml = <<-XML
    <root>
      <one>1</one>
      <two>2</two>
    </root>
  XML

  xml.should match_xml(<<-XML)
    <root>
      <one>1</one>
      <two>2</two>
    </root>
  XML
end

Implemented matchers

  • match_xml

  • exactly_match_xml

  • match_xml_structure

  • exactly_match_xml_structure

With Test::Unit

in test_helper.rb

require 'test_xml'
require 'test_xml/test_unit'

in test file:

def test_that_xml_matches
  xml = <<-XML
    <root>
      <one>1</one>
    </root>
  XML

  assert_match_xml(xml) do
    <<-XML
      <root>
        <one>1</one>
      </root>
    XML
  end
end

Implemented assertions

  • assert_match_xml

  • assert_exactly_match_xml

  • assert_match_xml_structure

  • assert_exactly_match_xml_structure

with assert_not_*

With Cucumber

Add to features/env.rb

require 'test_xml'
require 'test_xml/spec'
World(TestXml::Spec)

And you can add the following step:

Then /^response matches the following xml$/ do |string|
  response.body.should match_xml(string)
end

REQUIREMENTS

test_xml depends on Nokogiri

INSTALL

[sudo] gem install test_xml