🔥 0
0

Introduction to API Testing

Every time you add a product to your cart, check out, or track an order - APIs make it happen behind the scenes. As a QA engineer, testing these APIs is crucial.

What is an API?

API (Application Programming Interface) is how different software systems talk to each other.

Think of an e-commerce app:

┌─────────────┐         ┌─────────────┐         ┌─────────────┐
│   Mobile    │         │             │         │             │
│    App      │ ──────► │   API       │ ──────► │  Database   │
│             │ ◄────── │   Server    │ ◄────── │             │
└─────────────┘         └─────────────┘         └─────────────┘
                              ▲
┌─────────────┐               │
│   Website   │ ──────────────┘
└─────────────┘

The mobile app and website don't talk to the database directly - they go through the API.

Why Test APIs?

1. Catch Bugs Earlier

UI Testing:  Click button → Wait for page → Check result → 30 seconds
API Testing: Send request → Get response → 0.5 seconds

API tests are 60x faster than UI tests.

2. Test What Users Can't See

The checkout button might look fine, but:

  • Does the API validate the credit card correctly?
  • Does it handle out-of-stock items?
  • What happens with invalid coupon codes?

3. Test Edge Cases Easily

With UI, testing "what if the product name has 10,000 characters?" is tedious.

With API, just send a request with a long string.

---

API vs UI Testing

AspectUI TestingAPI Testing
SpeedSlow (seconds)Fast (milliseconds)
StabilityFlaky (UI changes)Stable (contracts)
CoverageLimitedComprehensive
SetupBrowser, driversJust HTTP client
When to useUser flowsBusiness logic
Best practice: Test the API first, then test critical user journeys via UI.

---

E-commerce API Examples

Here are typical APIs you'll test in an e-commerce application:

Product APIs

GET  /api/products           → List all products
GET  /api/products/123       → Get product details
GET  /api/products?category=electronics → Filter products

Cart APIs

GET    /api/cart             → View cart
POST   /api/cart             → Add item to cart
PUT    /api/cart/items/456   → Update quantity
DELETE /api/cart/items/456   → Remove item

Order APIs

POST /api/orders             → Place order
GET  /api/orders/789         → Get order details
GET  /api/orders?status=shipped → Filter orders

User APIs

POST /api/auth/login         → Login
POST /api/auth/register      → Register
GET  /api/users/me           → Get profile

---

What Makes a Good API Test?

A good API test verifies:

  • Status Code - Did we get 200 OK or an error?
  • Response Body - Is the data correct?
  • Response Time - Is it fast enough?
  • Headers - Are security headers present?
  • Error Handling - Do invalid inputs return proper errors?

Example: Testing "Add to Cart"

Request:
POST /api/cart
{
  "productId": "PROD-123",
  "quantity": 2
}

Expected Response (200 OK):
{
  "cartId": "CART-456",
  "items": [
    {
      "productId": "PROD-123",
      "name": "Wireless Headphones",
      "quantity": 2,
      "price": 79.99
    }
  ],
  "total": 159.98
}
Test checks:
  • ✅ Status code is 200
  • ✅ Response contains cartId
  • ✅ Item quantity matches request
  • ✅ Total is calculated correctly
  • ✅ Response time < 500ms

---

REST APIs

Most modern APIs follow REST (Representational State Transfer) principles:

  • Resources - Everything is a resource (products, orders, users)
  • HTTP Methods - Use GET, POST, PUT, DELETE
  • Stateless - Each request is independent
  • JSON - Data is usually in JSON format

We'll focus on REST APIs in this course, which is what Postman excels at testing.

---

What You'll Learn

By the end of this course, you'll be able to:

  • ✅ Send API requests using Postman
  • ✅ Test all HTTP methods (GET, POST, PUT, DELETE)
  • ✅ Write automated test assertions
  • ✅ Use environment variables for different environments
  • ✅ Create and run test collections
  • ✅ Debug API issues effectively

---

Key Takeaways

  • APIs are the backbone of modern applications
  • API testing is faster and more reliable than UI testing
  • Good API tests check status codes, response data, and performance
  • REST APIs use HTTP methods and JSON - perfect for Postman

Next up: Setting Up Postman - let's get your testing environment ready!