Background
Test serpbase with Postman to build an integration test + automation collection in 5 minutes, 3x faster than writing test code by hand.
1. Setup
Create Postman Environment
- Open Postman → Environments → +
- Name:
serpbase - Variables:
-
SERPBASE_KEY:sk_xxx_your_key -
BASE_URL:https://api.serpbase.dev
-
- Set
serpbaseas active environment
Create Collection
- New → Collection → Name:
SerpBase Tests - Authorization → Type:
No Auth(we use header) - Variables:
-
api_key:{{SERPBASE_KEY}} -
base_url:{{BASE_URL}}
-
2. Create Test Requests
Test 1: Search Basic Request
Method: POST
URL: {{base_url}}/google/search
Headers:
X-API-Key: {{SERPBASE_KEY}}
Content-Type: application/json
Body (raw JSON):
{
"q": "best serp api",
"gl": "us",
"hl": "en",
"num": 5
}
Tests:
pm.test("Status is 200", () => {
pm.response.to.have.status(200);
});
pm.test("Has organic results", () => {
const data = pm.response.json();
pm.expect(data.organic.length).to.be.greaterThan(0);
});
pm.test("Has request_id", () => {
pm.expect(pm.response.json().request_id).to.exist;
});
pm.test("Charged 1 credit", () => {
pm.expect(pm.response.json().credits_charged).to.equal(1);
});
Test 2: Multi-Region
URL: {{base_url}}/google/search
Body:
{
"q": "{{query}}",
"gl": "{{gl}}",
"hl": "en",
"num": 5
}
URL Params:
query: best serp api
gl: cn
Test 3: Maps Search
URL: {{base_url}}/google/maps/search
Body:
{
"q": "coffee shop",
"lat": 37.7749,
"lng": -122.4194,
"gl": "us"
}
3. Automation Tests (Pre-request Script)
// Random query for testing
pm.variables.set("random_q", "keyword_" + Math.random().toString(36).substring(7));
4. Integration Test Suite
// Collection-level Pre-request Script
const serpbase = pm.variables.get("base_url");
const apiKey = pm.variables.get("api_key");
pm.test("Suite Setup", () => {
pm.expect(serpbase).to.match(/serpbase\.dev/);
pm.expect(apiKey).to.match(/^sk_/);
});
5. 5 Advanced Tips
Tip 1: Environment Variables
Dev:
SERPBASE_KEY: sk_test_xxx
BASE_URL: https://api.serpbase.dev
Staging:
SERPBASE_KEY: sk_staging_xxx
BASE_URL: https://api.serpbase.dev
Prod:
SERPBASE_KEY: sk_prod_xxx
BASE_URL: https://api.serpbase.dev
Different env auto-switches.
Tip 2: Pre-request Script for Token Refresh
const apiKey = pm.variables.get("SERPBASE_KEY");
if (!apiKey || apiKey.includes("EXPIRED")) {
pm.sendRequest({
url: "https://api.serpbase.dev/auth/refresh",
method: "POST",
}, (err, res) => {
if (!err) {
const newKey = res.json().api_key;
pm.variables.set("SERPBASE_KEY", newKey);
pm.request.headers.upsert({
key: "X-API-Key",
value: newKey,
});
}
});
}
Tip 3: Collection Runner + Data-Driven
// Read CSV file to run 100 queries
const queries = pm.iterationData.toObject().queries;
for (const q of queries) {
pm.sendRequest({
url: pm.variables.get("base_url") + "/google/search",
method: "POST",
body: {
mode: "raw",
raw: JSON.stringify({ q, gl: "us", num: 5 }),
},
}, (err, res) => {
if (!err) {
pm.expect(res.json().organic.length).to.be.greaterThan(0);
}
});
}
Tip 4: Mock Server
// In Postman Mock Server
{
"status": 0,
"request_id": "mock_123",
"organic": [
{"rank": 1, "title": "Mock", "link": "https://mock.com"}
]
}
Tip 5: CI/CD Integration
# GitHub Actions
- name: Run Postman tests
uses: matt-ball/newman-action@v1
with:
collection: ./serpbase.postman_collection.json
environment: ./serpbase.postman_environment.json
6. Real Data (My 1-Month Project)
| Metric | Value |
|---|---|
| Collection requests | 12 |
| Test cases | 45 |
| Pass rate | 100% |
| Debug time | 1.5s per request (vs 5s hand-coded) |
| CI/CD integration | GitHub Actions |
7. vs Hand-Coded Tests
| Dimension | Postman | Hand-coded Python tests |
|---|---|---|
| Writing speed | 5 min | 30 min |
| Debug | GUI realtime | print + log |
| Docs | Auto-generated | Manual |
| CI/CD | Newman one-liner | pytest + config |
| Sharing | Team | Individual |
Summary
serpbase + Postman complete test solution:
- 1 collection, 12 requests, 45 test cases
- 5 min setup, 3x debug speed
- Newman + CI/CD integration, automated testing
Postman fits early-stage testing + debugging + docs. Production tests use pytest + Newman run Postman collection.
Top comments (0)