One of the things that makes me happy about API testing is how easy it is to organize tests and environment variables. I love having test suites ready at a moment’s notice; to run at the push of a button when regression testing is needed, or to run automatically as part of continuous integration.
This week I’ll be talking about some organizational patterns you can use for your API tests. I’ll be discussing them in the context of Postman, but the concepts will be similar no matter what API testing platform you are using.
First, let’s discuss environments. As you recall from last week’s post, an environment is a collection of variables in Postman. There are two different ways I like to set up my Postman environments. In order to explain them, I’ll use some scenarios. For both scenarios, let’s assume that I have an application that begins its deployment lifecycle in Development, then moves to QA, then Staging, and then Production.
In my first scenario, I have an API that gets and updates information about all the users on my website. In each product environment (Dev, QA, Staging, Prod), the test users will be different. They’ll have different IDs, and different first and last names. The URLs for the product environments will each be different as well. However, my tests will be exactly the same; in each product environment, I’ll want to GET a user, and PUT a user update.
So, I will create four different Postman environments:
Users- Dev
Users- QA
Users- Staging
Users- Prod
In each of my four environments, I’ll have these variables:
environmentURL
userId
firstName
lastName
Then my test collection will reference those variables. For example, I could have a test request like this:
GET https://{{environmentURL}}/users/{{userId}}
Which environment URL is called and which userId is used will depend on which Postman environment I am using. With this strategy, it’s easy for me to switch from the Dev environment to the QA environment, or any other environment. All I have to do is change the Postman environment setting and run the same test again.
The second scenario I use is for situations like this one: I have a function that delivers an email. The function uses the same URL regardless of the product environment. I like to pass in a timestamp variable, and that stays the same (it shows the current time) regardless of what environment I am using. But I like to change the language of the email depending on what product environment I am in.
In this case, I am creating only one Postman environment:
Email Test
My one Postman environment has this variable:
timestamp
My test collection, however, has one test for each product environment. So I have:
Send Email- Dev
Send Email- QA
Send Email- Staging
Send Email- Production
Each request includes the timestamp variable, but has a variation in what is sent in the body of the email. For the Dev environment, I use a request that contains “Dev” in the message body, for the QA environment, I use a request that contains “QA” in the message body, and so on.
When deciding which of these two environment strategies to use, consider the following:
- what stays the same from product environment to product environment?
- what changes from product environment to product environment?