Prompt
How do I reuse the same API tests across dev and staging?
Latest observation
Oct 6, 2026
OpenAI APIWeb search: off
The best way is to keep the tests environment-agnostic and move all environment-specific values into config.
Practical approach
1) Put base URLs and secrets in per-environment config
Use separate config for dev and staging, for example:
dev→https://dev.api.example.comstaging→https://staging.api.example.com
Keep things like:
baseUrl- auth tokens
- tenant IDs
- test data seeds
- feature flags
out of the test code itself.
2) Make tests use variables, not hardcoded values
Instead of:
fetch("https://dev.api.example.com/users")
write:
fetch(`${baseUrl}/users`)
3) Load config based on environment
Example in JavaScript:
const env = process.env.TEST_ENV || "dev";
const config = {
dev: {
baseUrl: "https://dev.api.example.com",
token: process.env.DEV_TOKEN,
},
staging: {
baseUrl: "https://staging.api.example.com",
token: process.env.STAGING_TOKEN,
},
};
module.exports = config[env];
Then run:
TEST_ENV=dev npm test
TEST_ENV=staging npm test
4) Keep assertions focused on behavior
Avoid asserting environment-specific details unless required. For example:
- Good:
status code is 200 - Good:
response includes user id - Avoid:
exact timestamp valueunless necessary
5) Use the same test data setup/teardown
If dev and staging have different state, add:
- seed scripts
- cleanup hooks
- isolated test accounts/resources
6) Parameterize anything that differs
Examples:
- user credentials
- IDs
- hostnames
- timeouts
- rate limits
If you use a test framework
Most frameworks support this directly:
- Postman/Newman: environments
- pytest: fixtures + config files
- JUnit/TestNG: profiles/properties
- Playwright / Cypress: config per project/environment
- pytest + requests: fixture for
base_url
Recommended structure
tests/
api/
users.test.js
orders.test.js
config/
dev.json
staging.json
Rule of thumb
If the test can be the same, keep it the same.
If only the endpoint/data differs, externalize that difference into config.
If you want, I can show you a concrete example in Postman, pytest, JUnit, or JavaScript.