DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Building REST API Backend Easily With Ballerina Language
  • Breaking Up a Monolithic Database with Kong
  • RESTful Web Services: How To Create a Context Path for Spring Boot Application or Web Service
  • How To Rewrite a Huge Codebase

Trending

  • DZone's Article Submission Guidelines
  • Designing Memory Architecture for Stateful AI Agents: Short-Term Context and Long-Term Persistence
  • Part 4: Multi-Agent Orchestration — Pairing Goose With Quarkus Flow State Machines
  • The SDLC After AI

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Culture and Methodologies
  3. Career Development
  4. Testing REST Services With Pyresttest

Testing REST Services With Pyresttest

A few lines of YAML is all that it takes in order to test a REST service. It's pretty simple. Alan Hohn has all of the details.

By 
Alan Hohn
·
Sep. 30, 16 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
9.8K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

Early in my career, someone explained to me why "ping" is such a natural first test when something goes wrong with an application or service. It's not just because it's a basic test with a quick yes or no answer; it's also because it bisects the standard network stack. If "ping" works, the issue is usually above the IP layer. If not, the issue is usually below (firewalls being the exception).

Similarly, when I recently spent some time creating a "smoke test" for an application deployment, I wanted something that would check if the application was up and running, without checking any complex behavior. The idea is to separate issues with the application itself from issues with the deployment or the build process.

It only took a little bit of looking to come across pyresttest. It's a small Python library with an impressive amount of functionality that goes beyond just checking if a REST service is up to checking that it is behaving correctly. In this article, I want to describe a little basic functionality.

The web site has installation instructions, but for the most part, it's as simple as pip install pyresttest. At that point, we need to provide tests in the form of YAML files. The syntax is then:

pyresttest url yaml-file

The URL parameter is used as the base; the tests themselves, of course, provide a more specific path. Here are some examples, using httpbin to make it easy to try these out.

First, we'll start with just a basic test, the equivalent of a ping for HTTP. The idea is to not get bogged down in error messages from a more complex test if the issue is basic.

---
- test:
    - name: "Connectivity"
    - url: "/get"

If we run this, i.e., pyresttest http://httpbin.org httpbin.yaml, we get the following output:

Test Group Default SUCCEEDED: : 1/1 Tests Passed!

If the server isn't available, i.e., pyresttest http://bad httpbin.yaml, we get:

ERROR:Test Failed: Connectivity URL=http://bad/get Group=Default HTTP Status Code: None
ERROR:Test Failure, failure type: Curl Exception, Reason: Curl Exception: (6, 'Could not resolve host: bad')
ERROR:Validator/Error details:Traceback (most recent call last):
  File "/usr/local/lib/python2.7/site-packages/pyresttest/resttest.py", line 351, in run_test
    curl.perform()  # Run the actual call
error: (6, 'Could not resolve host: bad')

Test Group Default FAILED: : 0/1 Tests Passed!

As you can see, pyresttest is using Curl under the covers. The docs describe how to customize the call to Curl.

A more complex test might check for a header in the response:

- test:
    - name: "Expected header"
    - url: "/get?abc=def"
    - validators:
        - compare: {header: "content-type", expected: "application/json"}

There can be multiple validators for a test and all must pass for the test to be a success.

Of course, we can check for a specific value in the response body as well.

- test:
    - name: "Expected JSON content"
    - url: "/get?abc=def"
    - validators:
        - compare: {jsonpath_mini: "args.abc", comparator: "eq", expected: "def"}

Note the ability to use dot notation to inspect inside deep data structures. Square brackets also work for pulling data out of arrays and strings.

However, when writing a simple smoke test and dealing with an application that returns dynamic data, we might want to check that it's present without getting hung up on what the value is. That way our tests are less fragile. There are a couple of ways to do that:

- test:
    - name: "Expect a field to be present"
    - url: "/get?abc=def"
    - validators:
        - compare: {jsonpath_mini: "args", comparator: "contains", expected: "abc"}

- test:
    - name: "Expected return type"
    - url: "/post"
    - method: "POST"
    - headers: {Content-Type: application/json}
    - body: '{"abc": "def"}'
    - validators:
        - compare: {jsonpath_mini: "args", comparator: "type", expected: "map"}

In the first example, we're just verifying that the 'args' field contains a field called 'abc'; we don't care what the value is. In the second example, the check is even simpler; we're just verifying that "args" is present and is a JSON object. (The list of types to match is in the advanced docs.)

I found this last approach very useful for writing a basic test with Kubernetes, where the goal was to make sure that at least one pod was running.

Overall, we've found pyresttest to be quick to use and easy to learn, making it a great choice for testing REST services without having to create any custom code.

REST Web Protocols Web Service Testing application IT Data (computing) Bracket (tournament) Kubernetes career

Opinions expressed by DZone contributors are their own.

Related

  • Building REST API Backend Easily With Ballerina Language
  • Breaking Up a Monolithic Database with Kong
  • RESTful Web Services: How To Create a Context Path for Spring Boot Application or Web Service
  • How To Rewrite a Huge Codebase

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook