API Testing with Postman Course Notes

Jul 27, 2024

API Testing with Postman Course Notes

Instructor Introduction

  • Name: Valentine
  • Background: Software developer passionate about teaching APIs.
  • Course Goal: Explain API testing using Postman in an easy-to-understand way.

Course Overview

  • Understand what Postman is and why it is essential for API testing.
  • Learn to interact with APIs and write API tests.
  • Explore automation of the testing process.
  • Hands-on experience with API assignments for practice.

Understanding APIs

  • Definition: An API (Application Programming Interface) is an interface to a server that provides data or functionality.
  • Analogy: Like a power outlet that accepts any device with the right plug specifications, APIs communicate data based on set specifications.
  • Documentation: Always refer to API documentation to understand its functionalities.

Getting Started with Postman

  • Postman is a tool for interacting with web-based APIs through the internet.
  • Installation: Two options to use Postman:
    • Browser application: postman.com
    • Standalone app (requires installation).
  • Account Setup: Create an account, keeping in mind updates in Postman versions may occur.

Using Postman

Interface and First API Request

  • Set a black theme for better visibility (gear icon > themes).
  • Example API: Book ordering API
  • Making the First Request: Status Endpoint
    • URL format: [Base URL]/status
    • HTTP Method: GET
    • Expected Response: 200 (status OK).

Understanding HTTP Requests/Responses

  • Request: Components
    • URL (API address)
    • Request method (GET, POST)
    • Headers & Body (for POST requests)
  • Response: Components
    • Status code (e.g., 200, 400)
    • Response body (data returned from the server)

Saving Requests and Collections

  • Save frequently used requests in a collection for better organization.
  • Variables can be created to avoid hardcoding values, thus making requests more flexible.

API Data Filtering and Query Parameters

  • Using Query Parameters with GET requests for additional filtering.
  • Example: Adding ?type=fiction to filter requests.
  • Monitor the status codes: 200 (OK), 400 (Bad Request), etc.

Ordering Books: Path Parameters

  • Order Book Endpoint: Specify book ID in the URL.
  • Use variable for book ID for greater flexibility.

Interacting with Books API

  • Using Postman to retrieve available books, order books, and check individual book details based on IDs.
  • Handling responses and recording order IDs for future requests.

Authentication and Authorization

  • Some API requests require authentication (e.g., using an access token).
  • Ensure to configure authorization correctly in the headers of requests.

Writing Tests in Postman

  • Writing JS Tests: Automate validation of HTTP responses using JavaScript in Postman's Tests section:
    • Check if the response status is correct (using pm.response.to.have.status(200)).
    • Ensure returned data is as expected (using pm.expect).

Automating Tests with Newman

  • Newman: CLI tool for running Postman collections to automate tests.
  • Install Node.js to set up Newman.
  • Export the Postman collection in JSON format for running tests.
  • Generate reports from Newman executions (CLI and HTML formats).

Monitoring with Postman

  • Set up Monitors to run collections automatically at defined intervals (daily, weekly).
  • Review execution history and results.

Practical Assignments Throughout the Course

  • Create various requests, handle responses, and troubleshoot issues.
  • Explore different endpoints, experiment with query parameters, and test error handling.
  • Document expectations for the expected behavior of the API through written tests.

Further Learning

  • Consider exploring data-driven testing with external files and deeper concepts in Postman usage.
  • Links to additional tutorials in course notes for advanced subjects.

Conclusion

  • Emphasis on real-world applicability and practical understanding of APIs.
  • Encouragement to dive deeper into testing and development with APIs.
  • Express gratitude for participation and invite further exploration of API testing.