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.
-
test XML with: RSpec, Test::Unit, Cucumber
-
test XML structure (partially or fully matched)
-
test the element values within XML structure
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
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
-
match_xml
-
exactly_match_xml
-
match_xml_structure
-
exactly_match_xml_structure
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
-
assert_match_xml
-
assert_exactly_match_xml
-
assert_match_xml_structure
-
assert_exactly_match_xml_structure
with assert_not_*
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
test_xml depends on Nokogiri
[sudo] gem install test_xml