Testing RESTful Service in Ruby, Part 2: Run frequently in a Continuous Testing Server

Stabilizing the tests and setting them up in a Continuous Testing server

Stabilize Test:

1. LIST all records

No changes from Part 1.

it "List all records" do
require "httpclient"
require "rexml"
http = HTTPClient.new
resp = http.get("http://www.thomas-bayer.com/sqlrest/CUSTOMER")
xml_doc = REXML::Document.new(resp.body)
expect(xml_doc.root.elements.size).to be > 10
expect(xml_doc.root.elements.first.text).to eq("1")
end

2. READ one record

No changes, maybe. It might be OK to assume that record #1 always exists, so long as there are no attempts to update or delete it. If this test starts to fail, we can always stabilize this by invoking CREATE service to use the freshly created one (see UPDATE or DELETE for more).

it "Read first record" do
http = HTTPClient.new
customer_id = 1
get_rest_url = "http://www.thomas-bayer.com/sqlrest/CUSTOMER/#{customer_id}"
resp = http.get(get_rest_url)
puts resp.body
expect(resp.body).to include("<LASTNAME>King</LASTNAME>")
end

3. CREATE a new record

If the ID is not provided, the service might assign a unique ID for a newly created record. However, this is not the case for this service. If we supply a specific ID, the first test execution passes, but the second one would fail because of the “duplicate record ID error”. In fact, “500 Internal Server Error” was returned for this scenario.

new_id = Faker::Number.between(from: 9001, to: 9999)
it "Create a new record" do
new_id = Faker::Number.between(from: 9001, to: 9999)
http = HTTPClient.new
create_rest_url = "http://www.thomas-bayer.com/sqlrest/CUSTOMER/"
new_record_xml = <<END_OF_MESSAGE
<CUSTOMER>
<ID>#{new_id}</ID>
<FIRSTNAME>P</FIRSTNAME>
<LASTNAME>Sherman</LASTNAME>
<STREET>42 Wallaby Way</STREET>
<CITY>Sydney</CITY>
</CUSTOMER>
END_OF_MESSAGE
resp = http.put(create_rest_url, new_record_xml)
expect(resp.body).not_to include("Internal Server Error")
fail_safe { delete_record(new_id) }
end

4. UPDATE a new record

We cannot just update an existing record in an automated test, because the test won’t be valid after the first run. Check out this article “One Test Automation Scenario Interview Question that Most Candidates Failed”.

  1. Create a brand new record
  2. Update it
  3. Verify the update
  4. Delete it
it "Update a record" do
record_id = create_record
http = HTTPClient.new
update_rest_url = "http://www.thomas-bayer.com/sqlrest/CUSTOMER/#{record_id}/"
update_xml = <<END_OF_MESSAGE
<CUSTOMER>
<FIRSTNAME>Paul</FIRSTNAME>
</CUSTOMER>
END_OF_MESSAGE
resp = http.post(update_rest_url, update_xml)
expect(resp.code).to eq(200) # OK
expect(read_record(record_id)).to include("Paul")delete_record(record_id)
end

5. DELETE a record

Like UPDATE, we cannot delete an existing record. Create a new one first.

it "Delete a record" do
record_id = create_record
http = HTTPClient.new
delete_rest_url = "http://www.thomas-bayer.com/sqlrest/CUSTOMER/#{record_id}"
resp = http.delete(delete_rest_url)
expect(resp.body).to include("<deleted>#{record_id}</deleted>")
end

Run all tests in TestWise a couple of times

Before I put the tests in a CT Server to run as regression, I will try to run them multiple times locally first. The below video shows the tests running twice in TestWise.

Run five tests, twice, in TestWise

Set up build project in BuildWise to run all tests in a CT Server

You can check out this article: “Set Up a Continuous Testing Server to Run Selenium Tests in Minutes” for setting up a new BuildWise CT server.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store