- this course is ideal if you already know how to use Postman for creating requests
- in this course you will learn:
- write API tests in Postman
- automate test execution with various Postman tools (Collection Runner, Postman CLI)
- running tests with the help of the CI/CD tool Github Actions
- Postman offers an official badge for completing this course and doing all assignments
- Say THANK YOU for this course by subscribing on YouTube
- Check out Postman's YouTube channel
- Join the Postman newsletter
- before we can automate the testing of an API, we first need to do a manual test
- create a new public workspace that will store all collections used during the course
- to run or make changes to a collection from a workspace that isn't yours, you need to make a copy of it (create a fork)
- if you can't get a status 200 OK reply from the server when trying the Status endpoint, check the following:
- ensure you are on postman.com
- if the error persists, submit an issue and add as many details as possible, including screenshots of any errors that appear.
- API testing involves verifying the functionality, reliability, performance, and security of an API
- the goal of API testing is to identify issues and defects in the API before it is released
- this course focuses on functional testing
- to automate the API tests, we first need to know how to perform them manually
- the goal of this lecture is to go through all the endpoints of this API
- regardless if the collection is public or private, it is a best practice to store secrets (like API keys, passwords, tokens) in Postman variables
- Postman auth helpers can configure different types of authentication
- keep in mind that authentication can be configured on one level (collection, folder) and inherited
- automated testing is a way to check if the API works correctly by letting Postman run tests
- we write scripts to automate the testing of the API
- API tests are written using scripts
- Postman uses JavaScript for writing tests
- don't confuse JavaScript with Java, as they are two different programming languages
- console.log can be used to write a message to the Postman console:
console.log("Hello from the Tests!");
- the Postman console can be used to view any information about the request and the response (URL, headers, body)
- any messages logged using console.log can also be seen there
- to clear the Postman console of entries you can use the "Clear" button or use the following script:
console.clear()
- the following script can be added to the "Tests" to check if the response status code is 200 OK.
pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
});
- to write tests in Postman, you need to know some JavaScript basics
- these are the most important concepts you need to be familiar with:
- variables and their scope
- data types including objects and arrays
- functions
- variables are like containers that store data
- you can define a variable named "name" with the value "jamie" and log the value to the console like this:
let name = "Jamie";
console.log(name);
- variables defined within a block statement are available only to code within that block
- variables defined outside of a block statement are in the global scope can be accessed from anywhere
- JavaScript has various data types to represent information
- the most likely data types you need to know about are: string, number, boolean, object, and array
- if you are unsure of a data type you can use the
typeof
operator. Example:console.log(typeof "John");
- example of an object containing various data types:
let person = {
name: "Jake", // string
age: 29, //number
isAdult: true, // boolean
'e-mail': 'jake@example.com', // string
hobbies: ['reading', 'traveling', 'gardening', 'cooking'] // array of strings
};
typeof - JavaScript - MDN Web Docs
- functions are fundamental concepts in programming:
- they are blocks of code designed to perform specific tasks
- they allow us to organize and reuse code effectively
- A simple function definition in JavaScript:
function greet() {
console.log("Hello from Postman!");
}
- a function needs to be invoked to work, using the following example syntax: greet()
- functions can take inputs in the form of arguments:
function greet(name) {
console.log("Hello from Postman, " + name + "!");
}
greet('Valentin');
- using console.log in a function is primarily used for debugging and doesn't mean the function returns a value
- the
return
statement specifies the value that a function should return after executing. - using
return
is necessary when you want to use the result of a function in another part of your code:
function add(a, b) {
let sum = a + b;
console.log(sum);
return sum;
}
console.log("The sum is: " + add(1,2));
- you can also define a function inside an object:
let person = {
firstName: "Jake",
age: 29,
isAdult: true,
'e-mail': 'jake@example.com',
hobbies: ['reading', 'traveling', 'gardening', 'cooking'],
greet: function(name) {
console.log('Hello from Postman ' + name + '. My name is ' + this.firstName);
}
};
person.greet('Jake');
- when a function is defined inside an object, we call it a method
- callback functions are an essential concept in JavaScript programming and allow for more efficient and flexible code.
- functions can be stored in variables:
const sayHello = function() {
console.log('Hello');
}
- functions without a name are called anonymous functions and can only be called by referencing the variable they're stored in, e.g., sayHello.
- functions can be passed as arguments to another function:
function doSomething(someFunction) {
someFunction(); // function is invoked here
}
doSomething(sayHello); // function passed as an argument
- functions can be defined directly inside another function:
function doSomething(someFunction) {
someFunction();
}
doSomething(function() {
sayHello();
});
- this syntax is used in Postman for writing tests
- most APIs use the JSON format
- JSON and JavaScript objects are not the same
JSON - JavaScript - MDN Web Docs
- to access any information from a JSON response body, you first need to parse the response:
const response = pm.response.json();
console.log(response);
- using Postman variables is a good way to reduce duplication and to be able to control multiple requests
- using Postman variables reduces or eliminates the need to copy/paste data or to manually reconfigure requests
- JavaScript variables are not the same as Postman variables
- JavaScript variables are scoped only to the script where they are defined and any variable set from there is not persisted
- Postman variables useful for:
- storing settings and persisting data in the long term, such as the baseUrl, API key, or other details
- passing data between requests
- it is possible to create or update a Postman collection variable from a script:
pm.collectionVariables.set('firstName', 'Jamie');
- fork the Assignment #1 collection and follow the instructions from the documentation
- we have a Postman collection that we can run request by request from the beginning to the end.
- in this unit, we will focus on writing tests for the API
- the basic structure of a test in Postman is the following:
pm.test('Name of the test', function () {
// assertions
});
- assertions can be written with
pm.expect
:
pm.expect(1).to.eql(1); - test passes
pm.expect(1).to.eql(2); - test fails
- an alternative way of writing a status code test using
pm.expect
:
pm.test('Status is 200', function () {
pm.expect(pm.response.status).to.eql(200);
});
- fork the Assignment #2 collection and follow the instructions from the documentation
- an API may return a response that is not JSON
- before trying to parse the response, it is best to check if the response body is JSON.
pm.test("Response body is JSON", () => {
pm.response.to.be.json;
});
- it is important to assert different aspects of the response body
- example checking the product name which is a string:
pm.test("Product is Espresso", () => {
const response = pm.response.json();
pm.expect(response.name).to.eql('Espresso');
});
- example checking a boolean value:
pm.test("Product is in stock", () => {
const response = pm.response.json();
pm.expect(response.isAvailable).to.eql(true);
pm.expect(response.isAvailable).to.be.true;
});
- example where the expected value is a Postman variable:
pm.expect(response.id).to.eql(pm.collectionVariables.get('productId'));
- instead of checking for a specific value, it is possible to check if a property exists
- example checking if the response as a property called
name
:
expect(response).to.have.property('name');
- example checking if a property of the response is a number
expect(response.price).to.be.a('number');
- it is possible to use regular expressions in assertions:
pm.expect(response.id).to.match(/^[A-Z0-9]{9}$/);
- fork the Assignment #3 collection and follow the instructions from the documentation
pm.test('Schema is valid', function () {
const schema = {
"type": "object",
"properties": {
"id": {
"type": "string"
},
"clientId": {
"type": "string"
},
"created": {
"type": "string",
},
"customerName": {
"type": "string"
},
"products": {
"type": "array"
}
}
};
pm.response.to.have.jsonSchema(schema);
});
- using Postman mock servers is an effective way to test if the tests written will fail
{
"type": "object",
"properties": {
"id": {
"type": "string",
"pattern": "^[A-Z0-9]{9}$"
},
"clientId": {
"type": "string",
"pattern": "^[a-zA-Z0-9]{9}$"
},
"created": {
"type": "string",
"format": "date-time"
},
"customerName": {
"type": "string"
},
"products": {
"type": "array",
"items": {
"type": "object",
"properties": {
"id": {
"type": "integer"
},
"quantity": {
"type": "integer",
"minimum": 1
}
},
"required": ["id", "quantity"]
}
}
},
"required": ["id", "clientId", "created", "customerName", "products"]
}
- fork the Assignment #4 collection and follow the instructions from the documentation
pm.expect(pm.response.headers.get('X-Powered-By')).to.eql('Express');
- the Postman collection runner is the first step toward testing if an automated run is possible
- the Postman collection runner is a useful tool for debugging
- runs are executed on the Postman Cloud
- no need to keep Postman open or your computer on during the run
- an excellent tool for API monitoring after deployment, ensuring it continues to function as expected.
- variable issues: if a variable isn't being resolved during the run, ensure that the variable has been correctly defined in the selected environment.
- best suited for APIs that are publicly accessible.
- limited use for pre-production environments which are typically not publicly accessible.
- Important: don't put secrets in the initial value of a Postman variable if the workspace is public
- CLI tools are necessary for running Postman collections without human intervention and on CI/CD servers like Jenkins, Github Actions, or GitLab
- Postman CLI is a command line tool for running Postman collections
- when selecting "Run collection" > "Automate runs via CLI", Postman will provide the necessary commands needed to run the collection
- the first step is authentication with the command
postman login
(this requires a Postman API key) - the second step is running the collection with the command
postman collection run
- For advanced Postman CLI configurations, you can specify additional command options
- this is the final unit that focuses on integrating Postman tests in Continuous Integration/Continuous Deployment (CI/CD) pipelines
- the objective is to automate API testing, ensuring APIs are continuously validated
- CI/CD stands for Continuous Integration and Continuous Deployment which is a practice in software development
- CI/CD aims to make software development process faster and more reliable
- Continuous Integration involves automatic testing of code changes and helps in detecting issues early
- After CI, the Continuous Deployment (CD) pipeline begins
- CD typically first deploys software to a test environment for testing purposes
- if tests pass, changes are automatically deployed to production
- Postman tests can be run at two points: a. after deploying API to pre-production environment; b. after deploying to the production environment
- Postman CLI can automate running collections and tests in the CD pipeline
- there isnβt a single βbestβ tool for CI/CD
- popular solutions for CI/CD include Jenkins, GitLab, Circle CI, GitHub Actions, β¦
- once you understand how to use Postman CLI, integration with any other CI/CD server is relatively easy
- to use GitHub Action you need to sign-up for a free GitHub account
- Create a new repository and make it public
- create a new workflow and grab the pipeline configuration from Postman
- store API key privately & reference it in the pipeline configuration
- fork the Assignment #5 collection and follow the instructions from the documentation
- Postman can generate pipeline configurations for the following tools: Jenkins, Bitbucket Pipelines, CircleCI, GitLab, Azure Pipelines, Travis CI
- test executions in the CI/CD are ingested into the workspace reports
- Postman is all about collaboration. Most of the time, you would want to create a team in Postman and use team workspaces
- Important: once you create a team workspace, your "Public" workspace will transform in a "Personal" workspace; make sure to change it back to "Public"
- ensure you have completed all assignments
- ensure that your workspace is "Public" and not "Personal"
- to claim your Postman badge, follow the instructions from the collection named "Claim your badge"
- Congrats! You have completed the API test automation course with Postman.
- π 142 hours of work (and counting)
- βοΈ 56 coffees
- β amount of fun!