Secret path to Paw assertions

Yesterday a user tweeted us to know if Paw could support testing against responses.

As we hate to disappoint users, we've decided to make a special gift for him and for all the Paw community. It has taken us a tremendous amount of work to build this. We gathered a team of two talented engineers and kindly asked them to implement this in an afternoon. Challenge taken! A few hours and 50 200 lines of code later, it was done, passed our QA process and ready for production.

While this might sound like an April Fool – we would be only 14 days late for this – this is perfectly functional. The good news is that you don’t need to update Paw! Everything will be working with the current version.

We are going to reveal the way to the assertions of server responses in Paw. Ready? Let’s explore it together!

Introducing assertions in Paw

First thing you will need to do is to open your project in Paw. Add testing to your project by clicking the two links below:

To add testing to more projects, just open them and hit the second link again

Follow the on-screen instructions to install the importer. And here we go! All ready to start!

To help you run your first test we will guide you through the simple steps:

  1. Open the request you want to test. In the description field type ## Tests followed by a new line then the JavaScript that you want to run. It can follow your other comments for the given request.

    Start writing assertion tests in Paw

  2. Select the new group that was added called Testing and send it. If all tests pass, you will receive a green 200 OK in the title bar. If anything fails, you will receive a red 500 Error.

  3. To see more details open the Extension Console by pressing ⌥⌘3 (or menu Window > Extension Console)

Hint: You can re-run all your requests by pressing ⌥⌘↵

We also made some exemples for your reference.

## Tests

/* We can get the current testing context with `testContext`
 * for example may want to get another request and assert
 * some relationship between their responses
 */

var otherRequest = testContext.getRequestByName('Other Request Name')
var otherExchange = otherRequest.getLastExchange()
var otherBody = JSON.parse(otherExchange.responseBody)
var body = JSON.parse(exchange.responseBody)

assertEqual(otherBody.origin, body.origin)

Here's another example with a little description before the tests.

This enpoint will wait 2 seconds before responding.

## Tests

assertEqual(exchange.responseStatusCode, 200, 'Status code')
assert(exchange.responseTime < 2800, 'Too long loading time')
assert(exchange.responseTime > 2000, 'Too short loading time')

Make use of the following functions and objects that are available in this closure:

  • assert(cond: Boolean, message: String) to assert the condition is true or throw an error with the message
  • assertEqual(value, expectedValue [,message: String]) to assert the value is equal to the expected value
  • exchange to access the latest HTTPExchange
  • testContext to access the Paw Context which provides you an API to the rest of your project (useful for retrieving data from other requests)

Of course we are going to put more work into this and make testing of the response a real feature later. In the meantime, we hope that you will make use of this hidden opportunity and save your time checking what you get back from the server.

You don’t have to keep this secret to yourself though. Feel free to tweet about this and let them benefit from this hidden treasure.

Comments

comments powered by Disqus