Skip to content
Learn with RV – Tech Blog
Learn with RV – Tech Blog

#testautomation #qa #programming #linux #devops

  • Home
  • Who am I?
  • 1-on-1 Mentoring
  • 24 Testimonials
  • YouTube
  • LinkedIn
  • Contact
Learn with RV – Tech Blog

#testautomation #qa #programming #linux #devops

May 4, 2023October 2, 2024

PactumJS: The Key to Efficient API Automation Testing

API automation testing is an essential part of software development. It ensures that the APIs are working as expected and delivering the intended results. However, the process of API testing can be time-consuming and error-prone. PactumJS is an open-source library that can help streamline the API testing process by enabling developers to write test cases that mimic real-world API calls. In this article, we’ll take a closer look at how PactumJS can be used for API automation testing.

What is PactumJS?

PactumJS is a lightweight, free and open-source REST API automation testing tool, that provides all the necessary ingredients for the most common things to write better API automation tests in an easy, fast & fun way.

PactumJS provides great documentation for API Testing (including GraphQL), Component Testing, Contract Testing and also a Mock server feature.

Note: PactumJS is not a test runner. It needs to be used alongside with a test runner like mocha, jest, jasmine, cucumber or build your own if your heart desires, pactum should work out of the box.

Prerequisites

NodeJS v10 and above

Getting Started with PactumJS

To get started with PactumJS, you first need to install it. You can install it using the following command

npm install -D pactum mocha npx

Demo Apps

For demo purpose, we will be using both free to use QA-Practice REST application and also QA-Practice GraphQL app

If you have Docker installed on your machine, feel free to start the REST API application on local machine with this simple CLI command

docker run -d --rm --name qa-practice-api -p8887:8081 rvancea/qa-practice-api:latest

The REST API app will be up and running on THIS URL

The same goes for QA-Practice GraphQL App: using Docker , feel free to start it on local machine with this simple CLI command

docker run -d --rm --name qa-practice-graphql -p 5000:5000 rvancea/qa-practice-graphql

The GraphQL APP will be up and running on THIS URL

Writing a basic test scenario

In this example, we are making a GET request to the QA-Practice REST app and assert that it returns a 200 response status code. PactumJS provides a fluent syntax that makes it easy to write test cases and it is packed with a rich set of features for making HTTP requests & validating the server responses. Head over to Requests and Assertions for more details.

Writing more complex tests - JWT Authorization

pactumjs-qa-practice-rest-auth

In this example, we are using the before hook to generate an authorization token (JWT) and store its value in ‘authToken’ variable.
Then we are using the stored token to make a GET Employees request (that requires authorization) and check that it returns a 200 status code (OK).

Finally, we add a negative test as well, trying to call the same GET Employees endpoint, but without passing the token as Authorization header, therefore the expected response status code is 401 (unauthorized).

Writing a GraphQL test

graphql-test

In this example, we are sending a request and assert that the response status code is 200 and the response contains a song title called ‘Nothing else matters’.

Running tests

The tests get executed with a simple CLI command, as it follows:

npx mocha example.test.js
results-pactumjs

Reporters

By default, pactumjs uses spec reporter, meaning that the test execution will be displayed in the CLI. However, you have the flexibility to install other types of reporters as dependencies (e.g. Mochawesome for HTML reports)

Asserts

PactumJS’s documentation provides lots of asserts that you can find HERE

In the next picture, we will see some of them that we can use in our tests

pactumjs-asserts

Conclusion

By using PactumJS, you can write more comprehensive and effective API tests, ultimately leading to more reliable and high-quality software.

Enjoyed this article? Make sure to subscribe to my YouTube Channel for more Test Automation tutorials, and follow me on LinkedIn and Twitter for regular insights.
Looking to improve your test automation skills?
I’ve created a personalized 1-on-1 Mentoring program refined to boost YOUR skills. Reach out at iamrv@razvanvancea.ro for more details and together will create a learning path adapted to your current skills and goals that you are aiming for, in a timely-efficient manner🚀

Post Views: 493

Related

Share this article:
QA

Post navigation

Previous post
Next post

Recent Posts

  • ‘selectors’ vs. ‘locators’ – what is the difference?
  • Internationalization (i18n) – in test automation
  • Appium v3 is here: essential changes
  • Zen Browser review: The calmer side of internet
  • Understanding the Difference Between “module” and “commonjs” in package.json

Recent Comments

  1. Paul on Web Accessibility: A step-by-step guide to Testing with pa11y
  2. Automated Tests for website Accessibility with Axe and TestCafe - Learn with RV - Tech Blog on How to generate E2E TestCafe Framework in seconds
  3. RV on Exploring Faker.js: A Powerful Tool for Generating Realistic Random Test Data
  4. Adrian Maciuc on Exploring Faker.js: A Powerful Tool for Generating Realistic Random Test Data
  5. Nick on Cypress vs Playwright vs Testcafe – which framework is faster?

Archives

  • November 2025
  • October 2025
  • September 2025
  • August 2025
  • July 2025
  • June 2025
  • March 2025
  • February 2025
  • January 2025
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • July 2024
  • May 2024
  • April 2024
  • March 2024
  • February 2024
  • January 2024
  • December 2023
  • October 2023
  • September 2023
  • August 2023
  • July 2023
  • June 2023
  • May 2023
  • April 2023

Categories

  • Linux
  • Programming
  • QA
  • Tools
  • Uncategorized
©2025 Learn with RV – Tech Blog | WordPress Theme by SuperbThemes