Postman (newman) test scripts for HWs
We will post
-
sample testsbefore HW deadline so you can have a sense of what the tests looks like and write your own tests. -
actual grading testsafter HW deadline so you can double check.
Postman (newman) is a javascript tool to test RESTful services. You can change the method (GET/POST/DELETE/PUT, etc.), authentication (Bearer Token/OAuth, etc.), Headers, Params(usually for GET), Body (usually for POST) for a certain request.
There are two scripts part you could write: Pre-request Script and Tests
Pre-request Scriptruns before the request is made, you may generate some random value and set to global variables.Testsruns after the request is done, you may make unit tests to assert on the response you get.
-
Download and install latest
[email protected] -
On the rop left corner, click
import -
Select the test json to be imported. For example, the
BasicService.postman_collection.json -
On the top right corner, next to
Environment, click theManage Environmentbutton -
Add an environment with two keys:
host=localhost,port= your service port. You may also use other environment variables. -
Then run the collection of your requests. Under
Datasection, select the data.json files assoicated with each test.
-
Download
[email protected]withnpm install -g newman. Getnpmif you do not have it. -
Run with the following command:
newman run <test_json> --global-var 'host=<your_service_host>' --global-var 'port=<your_service_port>' -d <data_file>For example:
newman run BasicService.postman_collection.json --global-var 'host=localhost' --global-var 'port=8080' -d ./hw1-test-data.json
-
Add new requests in postman
-
Define your request API with
http://{{host}}:{{port}}+<API_END_POINT> -
Select HTTP method
-
Define Param (for GET) or Body (for POST)
-
If using Json, select
application/json -
Define your
Pre-request ScriptandTests