API testing is the process of confirming an API is working as expected.API testing is a crucial aspect of ensuring the quality and reliability of your applications. Postman is a popular tool for creating and running API tests, while Newman is a command-line tool that allows you to run Postman collections and generate detailed reports.
Before you begin, make sure you have the following prerequisites:
Postman is installed on your system.
A Postman account to sync your collections and environments (optional).
Nodejs should be installed Go to Command Prompt and verify the installation is done properly by typing the following command.
> node -v
4. In Command Prompt, verify the NPM installation is done properly by typing the following command.
5. Newman is installed globally on your system. You can install it using npm (Node.js package manager) with the following command:
>npm install -g newman
we will get like this
Step 1: Create a Postman Collection If you haven't already, create a collection in Postman containing the API requests you want to test. Organize your requests into folders within the collection to group related tests. Step 2: Export the Collection Export the collection from Postman in JSON format. To do this:
Select the collection you want to export.
Click the "Export" button.
Choose "Collection v2" as the export format and save the JSON file to your local system.
Export Environment variables and global variables and save JSON file to the local system
Step 3: Run the Collection with Newman Now, you can use Newman to run the collection and generate a report. Open your command-line interface and use the following command:
>newman run <"collection_filename"> -e <environment_filename> -g <global_filename > -r htmlextra
Replace postman_collection.json with the path to the JSON file you exported earlier. The -r htmlextra flag specifies the format of the report (HTML Extra is a popular choice).
Newman will start running the collection, making API requests, and validating responses. Once it's done, you'll see a summary of the run in your terminal.
Step 4: View the Generated Report The HTML Extra report will be saved in the current working directory by default with a filename like newman-run-report-<timestamp>.html. You can open this HTML report in a web browser to view detailed information about the test run. In the report, you'll find:
sample Newman report
Summary Information: Including the number of requests, assertions, and test duration. In the sample, you can see 12 requests are there
Request and Response Details: For each request in your collection.
Assertions: Whether each assertion passed or failed.
Environment Variables: If you used environment variables in your collection, their values will be displayed.
Console Output: Any console log messages generated during the test run.
Generating API test reports using Newman is a straightforward process that provides valuable insights into your test runs. With the ability to customize reports and integrate them into your CI/CD pipelines, you can ensure that your APIs are functioning correctly and reliably. Happy Learning ..!!!