API Testing With Runscope
If you're using APIs, you need to test well. Here's a guide to testing APIs with Runscope, with a breakdown of creating a bucket and making a test.
Join the DZone community and get the full member experience.
Join For Freei work with a lot of apis and i really appreciate good tools. lately i've been really enjoying using runscope for testing and monitoring my apis so i thought i'd share how i work with this tool, which has a great one-person free tier *and* the ability to import/export tests so if you need to work with a team with just the free tier (the examples here are from an open source project which is an obvious use case where it's hard to fund tools), it's clunky but doable. the idea here is just to show you around how to create your own api tests with runscope (and also to write down what i did so i can point both my future self and others at this!)
create a bucket
first up, create a "bucket"...i'd call this a "test suite" in any other setting. it's just a place to keep things really but it has some really handy features:
-
you can set up variables (such as a
base_url
) in the bucket which are then used by all your tests. this makes it trivial to make changes and run the tests again, for example against another platform - you can also get your tests to use the bucket settings for which locations to run from and again, this makes it easy to change
- once you've written some tests, you can trigger all the tests in a bucket by using a specific url
create a test
i'm just creating a simple test, which points at https://test.api.joind.in and makes sure that there's a "hot events" url listed, and that calling it returns sensible data.
this just shows that i have the bucket-wide settings selected, and a test with two steps - first to get the main api page, and then to get the hot events page.
the
{{base_url}}
is set in the bucket-wide settings, which are selected in the top-right hand side of the test (this took me ages to find the first time), and i use it to define what to call:
as well as setting some "assertions" that your response must fulfil (more on those in a moment), you can grab "variables" from one response to use in the next request. in this example, i want to get the
hot-events
property so i can then call it (this is hypermedia; the url might change).
with that url captured, my next test simply calls it. both steps have some assertions that the responses must be compliant with - here are the ones i have added to check the response of the second step:
if any of these assertions fail, the test will be marked as failed. when working on either capturing variables or writing assertions, look out for the "last response data" accordion piece, it's super-helpful to be able to see what came back last time!
running the tests
i can run tests individually (look for the "save and run" buttons at the top of the edit test screen) and the history of results are displayed in the sidebar (as you can see in my screenshots, i was still working on getting this test passing until shortly before i took the screenshot!), you can also run tests all together and with different settings - that's a blog post for another day perhaps.
runscope can run your tests on a schedule, and be set up with alerts for how to let you know if something fails. in addition to the pass/fail data, it also stores information about how long each test takes, which can be really helpful if you're suddenly seeing timeouts to be able to realise that your api got slower two deploys ago or something.
Published at DZone with permission of Lorna Mitchell, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments