0

Integration Testing Examples You Can Actually Use

Definitions are fine, but most people learn integration testing by seeing it. Here are some concrete examples from everyday projects, so you can picture what these tests look like.

Example 1: API and database

Imagine a user registration endpoint. A good integration test sends a real POST request to /register, then checks two things: the API returns a 201 response, and a new row actually exists in the users table with a hashed password (not the plain text one).

A unit test might mock the database and say "yes, save() was called." The integration test confirms the record is really there. That's the difference.

Example 2: Service-to-service communication

Say you run an e-commerce app with an order service and an inventory service. When someone places an order, the order service calls inventory to reduce stock.

The integration test places an order and verifies that:

  1. The order is created with the right status.
  2. The inventory count drops by the right amount.
  3. If inventory is out of stock, the order service handles the error properly instead of crashing.

That third check is where most real bugs hide.

Example 3: Third-party payment gateway

You obviously don't want to charge real cards in tests. Most payment providers offer a sandbox. An integration test can send a test payment through the sandbox and confirm your app handles three outcomes: success, declined card, and timeout. Pay special attention to the timeout, since it's the one people forget and the one that causes duplicate charges.

Example 4: Message queues

If your app publishes events to something like Kafka or RabbitMQ, an integration test can publish a message and verify the consumer picks it up and does the right thing, such as sending a confirmation email or updating a record.

Example 5: A quick code sample

Here's a simple Python example using pytest and a test client:

def test_create_user_saves_to_db(client, db):
    response = client.post("/register", json={
        "email": "test@example.com",
        "password": "secret123"
    })

    assert response.status_code == 201

    user = db.query("SELECT * FROM users WHERE email = %s", ["test@example.com"])
    assert user is not None
    assert user["password"] != "secret123"

Nothing fancy, and it tests the real path from request to database.

Skipping the manual work

Writing these by hand for every endpoint adds up quickly. Keploy takes a different route: it captures actual API calls and database interactions from your running app and generates test cases and mocks from them. That means your tests reflect real usage rather than only the scenarios you thought of. You can read more in Keploy's integration testing guide.

Wrapping up

Good integration tests are usually simple: real components, real interactions, clear assertions. Start with the examples above that match your stack, and expand from there.


All rights reserved

Viblo
Hãy đăng ký một tài khoản Viblo để nhận được nhiều bài viết thú vị hơn.
Đăng kí