Mastering API Testing with Postman
Mastering API Testing with Postman: A Deep Dive into Contract and Regression Tests
As APIs become increasingly crucial in modern software development, ensuring their reliability and consistency is paramount. In this post, we’ll explore how to leverage Postman for creating comprehensive API tests, focusing on contract and regression testing techniques. We’ll use Megaport’s Location endpoint as our example, but the principles can be applied to any API.
What We’ll Cover
- Setting up Postman for API testing
- Understanding contract vs. regression testing
- Creating robust test scripts
- Practical examples using Megaport’s Location API
Setting Up Postman
Before we dive into the tests, let’s ensure our Postman environment is properly configured:
- Install Postman if you haven’t already.
- Set up your API client credentials (for this example, we’re using Megaport’s API).
- Create a new collection for our tests.
Pre-request Script for Authentication
To streamline our testing process, we’ll use a pre-request script to handle authentication:
// Set your credentials
var clientId = 'YOUR_CLIENT_ID'
var clientSecret = 'YOUR_CLIENT_SECRET'
// Set the URL and request parameters
var url = 'https://api.megaport.com/oauth2/token'
var headers = {
'Content-Type': 'application/x-www-form-urlencoded',
Authorization: 'Basic ' + btoa(clientId + ':' + clientSecret)
}
// Set the request body
var body = {
mode: 'urlencoded',
urlencoded: [{ key: 'grant_type', value: 'client_credentials' }]
}
// Send the POST request
pm.sendRequest(
{
url: url,
method: 'POST',
header: headers,
body: body
},
function (err, res) {
if (err) {
console.error('Error:', err)
return
}
var jsonResponse = res.json()
if (jsonResponse && jsonResponse.access_token) {
pm.environment.set('accessToken', jsonResponse.access_token)
console.log('Access Token saved:', pm.environment.get('accessToken'))
}
}
)
This script automatically fetches and stores an access token, which we’ll use in our subsequent requests.
Understanding Contract vs. Regression Testing
Before we start writing tests, let’s clarify the difference between contract and regression testing:
| Aspect | Contract Testing | Regression Testing |
|---|---|---|
| Focus | API structure and data format | Overall functionality and existing features |
| Purpose | Ensure API adheres to its specified contract | Prevent new changes from breaking existing functionality |
| When to use | During development, especially in microservices | After code changes, as part of the release cycle |
Creating Robust Test Scripts
Now, let’s look at some examples of both contract and regression tests.
Regression Tests
These tests ensure that basic functionality remains intact:
pm.test('Verify Response status code is 200', function () {
pm.expect(pm.response.code).to.equal(200)
})
pm.test('Verify Response Content-Type is application/json', function () {
pm.expect(pm.response.headers.get('Content-Type')).to.include('application/json')
})
pm.test('Verify response time is less than 5 seconds', function () {
pm.expect(pm.response.responseTime).to.be.below(5000)
})
pm.test('Verify Data array is present and contains at least one element', function () {
const responseData = pm.response.json()
pm.expect(responseData).to.have.property('data').that.is.an('array').and.not.empty
})
Contract Tests
These tests focus on the structure and content of the API response:
pm.test('Verify Metro fields are non-empty strings', function () {
const responseData = pm.response.json().data
responseData.forEach(function (location) {
pm.expect(location.metro)
.to.be.a('string')
.and.to.have.lengthOf.at.least(1, 'Metro should not be empty')
})
})
pm.test('Verify each location includes latitude and longitude details', function () {
pm.response.json().data.forEach(function (location) {
pm.expect(location).to.have.property('latitude').that.is.a('number')
pm.expect(location).to.have.property('longitude').that.is.a('number')
})
})
Practical Examples Using Megaport’s Location API
Let’s look at some specific tests for Megaport’s Location endpoint:
Filtering by Metro
pm.test('Verify Metro fields are non-empty strings', function () {
const responseData = pm.response.json().data
responseData.forEach(function (location) {
pm.expect(location.metro)
.to.be.a('string')
.and.to.have.lengthOf.at.least(1, 'Metro should not be empty')
})
})
Checking Location Status
pm.test('Verify Status is Active for each location', function () {
const responseData = pm.response.json().data
const statusOptions = ['Active']
statusOptions.forEach((option) => {
pm.expect(responseData.every((location) => location.status.includes(option))).to.be.true
})
})
Verifying MVE Vendor Details
pm.test('Verify MVE Vendor property to equal VMware for each location', function () {
pm.response.json().data.forEach(function (location) {
pm.expect(location.products.mve[0].vendor).to.equal('VMware')
})
})
Conclusion
By combining contract and regression tests in Postman, we can create a robust testing suite that ensures our API remains reliable and consistent. This approach helps catch issues early in the development process and provides confidence when making changes to the API.
Remember, the examples provided here are just a starting point. As you become more comfortable with Postman and API testing, you can create more complex and comprehensive test suites tailored to your specific needs.