In this blog, I write about Web Development, Blockchain, and DevOps. I am a Docker Captain, Public Speaker, and Developer Advocate at daily.dev. Check my YouTube channel.
Let's test our API using Docker Containers and Postman.
We will test a simple CRUD application created using Node.js, Express, and Postgres.
Clone the repository and modify the docker-compose.yml file
Run and test the application locally
Deploy our application.
Create and export the Postman collection
test the app using containers
Conclusion
0. π¬ Intro
We will use Docker and Postman:
Docker: to run our application.
Postman: to test it.
π§βπ Postman
Postman is a popular collaboration platform and API (Application Programming Interface) client that allows developers to design, test, and document APIs. It provides a user-friendly interface that enables users to request an API and inspect the response and create and manage automated tests for APIs.
Additionally, Postman allows teams to share collections of requests and collaborate on API development and testing.
Postman is very cool, but we will make it even more incredible in this article by using it with Docker.
π³ Docker
Docker is a containerization platform that allows developers to package applications and their dependencies into self-contained units called containers.
Containers are isolated from each other and the underlying host system, making them portable and easy to deploy across different environments. Docker provides a simple and consistent way to build, distribute, and run applications, which helps to streamline the development process and reduce the time and effort required to set up and manage complex application architectures.
In this article, we will use docker both to run our application and to test the application using Postman.
1. 𧬠Clone the repository
We will use a JavaScript project I created in another video. It is a simple CRUD application using Node.js, Express, and Postgres.
This will create a 4-hour session where you can create and test your containers.
Click on + Add new instance
And on the right, you should see a terminal and the instance. This instance has a public address.
Deploy the application
Deploying the application is very easy.
We need to copy the docker-compose.yml file and paste it on the playground.
You can do it using ssh, or you can drag and drop the file on the playground.
Check if the docker-compose file is there by typing
ls
Now let's run the same two commands we ran before locally:
docker compose up -d node_db
docker compose up -d node_app
And let's check it
docker ps -a
Note that now there are 2 ports open: 3000 and 5432
If you click on the 3000 next to OPEN PORT, you should see the application running on a public IP (If you use any a cloud provider, check the public IP)
let's get an example
4. ποΈ Postman Collection
Open Postman, and if you don't have it already create a New public space named whatever you want.
In my case, I created a workspace named public-francesco
Now create a new collection by clicking Create Collection
Rename it as postman-docker
Add a new GET request at the public IP address of the application, and SAVE it (top-right corner)
Duplicate it (right-click on the first request) and name the new one Get All Users
This request will be a GET request to <public-server-ip>:/users
β οΈRemember to SAVE the requests
You can test this Collection by running it directly in Postman:
We are now ready to export this collection and test it using containers
6. π§ͺ Test the app using containers
We will use Newman, a command-line collection runner for Postman. It allows you to run and test a Postman Collection directly from the command line. It is built with extensibility in mind so that you can easily integrate it with your continuous integration servers and build systems.
Export the Postman Collection
Now export this collection as a JSON file. You can do it by clicking on the three dots on the right of the collection name and then clicking on Export
Save it as node-collection (no extension)
Now it's time to run this collection.
run the Postman Collection using Newman
β οΈ For Windows users: use the Command Prompt or Powershell
navigate to the folder where the file node-collection is located
And run this command:
docker run -v <postman-collection-path>:/etc/newman -t postman/newman run node-collection
Replace <postman-collection-path> with the path of the folder where the file node-collection is located.
In my case, it's /c/workspace/postman-collection-test
and you will get a list of the requests.
7. π Conclusion
We made it! We tested the application using the following: