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.
Let's test whether the board that we want to create is created with the proper name
my API board.
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);
});
Name the test Board should be created.
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");
});
Click Send. Your tests should pass.
Change the name value to
My API board xx and check whether the test fails now.
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);
});
Click Send and check the Test results.
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.
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");
});
Click Send and check the test results.
Change jsonData.prefs.permissionLevel to jsonData.permissionLevel. Click Send and inspect the test result.
Exercises
-
Build another request which creates a new list called TODO inside your already created board.
Write tests to check that the request performed as expected. As a minimum, test the following:
status is 200
the name of the list is TODO
should not be closed
test that the list was created in the desired board
Inside the TODO list, create a new card (which is like a task) with the name Learn Postman.
Write tests to check that the request performed as expected. As a minimum, test the following:
status is 200
the name of the card is Learn Postman
should not be closed
test that the card was created in the desired TODO list
test that the card was created in the desired board
test that the card has no votes or attachments
Create a new list named DONE.
Move the card from the TODO list to the DONE list.
Write tests to check that the request performed as expected. Test the following:
status is 200
the name of the card is still Learn Postman
test that the card was moved to the desired DONE list
Hint: You will be using the PUT method to update this information.
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:
status is 200
figure a way on how you could test that the board does not exist anymore (hint: using a GET request)
Hint: You will be using the DELETE method in order to delete the board.
Back to top