Writing Multiple Tests

In the [[|previous tutorial], we saw how to write a simple test for the returned status code of a real world API. However, testing only the returned status code is not enough. For the previously created request, we need to check for example that the board that we want to create has been actually created with the proper name and specific settings.

  1. Let's test whether the board that we want to create is created with the proper name my API board.
  2. Go to Tests tab. Click on the snippet Response Body: JSON value check. If you don't find it, copy the code below:
    pm.test("Your test name", function () {
        var jsonData = pm.response.json();
        pm.expect(jsonData.value).to.eql(100);
    });
  3. Name the test Board should be created.
  4. The callback function gets the request json response in a variable named jsonData. This json object can be parsed to retrieve the different key/value pairs. Replace jsonData.value with jsonDATA.name to retireve the key name from the response and 100 with my API board to check whether name=my API board. \\
    pm.test("Board shoud be created", function () {
        var jsonData = pm.response.json();
        pm.expect(jsonData.name).to.eql("My API board");
    });
  5. Click Send. Your tests should pass.
  6. Change the name value to My API board xx and check whether the test fails now.
  7. You can add more assertions into your test. For example to check whether this board is opened and not closed, we can test whether the parameter closed is false. Change your test as shown below:
    pm.test("Board shoud be created", function () {
        var jsonData = pm.response.json();
        pm.expect(jsonData.name).to.eql("My API board");
        pm.expect(jsonData.closed).to.eql(false);
    });
  8. Click Send and check the Test results.
  9. Let's try now if this board is a private one. In the Body response, you can see that there's a parameter named permissionLevel inside prefs. You can see that permissionLevel is at another level. It is called a nested object.
  10. Create a new test named Board should be private. Add the following code:
    pm.test("Board shoud be private", function () {
        var jsonData = pm.response.json();
        pm.expect(jsonData.prefs.permissionLevel).to.eql("private");
    });
  11. Click Send and check the test results.
  12. Change jsonData.prefs.permissionLevel to jsonData.permissionLevel. Click Send and inspect the test result.

Exercises

  1. Build another request which creates a new list called TODO inside your already created board.
  2. Write tests to check that the request performed as expected. As a minimum, test the following:
    1. status is 200
    2. the name of the list is TODO
    3. should not be closed
    4. test that the list was created in the desired board
  3. Inside the TODO list, create a new card (which is like a task) with the name Learn Postman.
  4. Write tests to check that the request performed as expected. As a minimum, test the following:
    1. status is 200
    2. the name of the card is Learn Postman
    3. should not be closed
    4. test that the card was created in the desired TODO list
    5. test that the card was created in the desired board
    6. test that the card has no votes or attachments
  5. Create a new list named DONE.
  6. Move the card from the TODO list to the DONE list.
  7. Write tests to check that the request performed as expected. Test the following:
    1. status is 200
    2. the name of the card is still Learn Postman
    3. test that the card was moved to the desired DONE list
    4. Hint: You will be using the PUT method to update this information.
  8. It is time to clean things up. As you do not need the task, the lists and the board, make sure you delete the board, so that your Trello account does not get filled with useless boards. Test the following:
    1. status is 200
    2. figure a way on how you could test that the board does not exist anymore (hint: using a GET request)
    3. Hint: You will be using the DELETE method in order to delete the board.
teaching_assistant/api/postman_test_more.txt · Last modified: 2021/09/16 16:16 by Nour
Back to top
CC Attribution-Share Alike 4.0 International
chimeric.de = chi`s home Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0