Prompt
How do I integrate API tests into GitLab CI?
Latest observation
Oct 6, 2026
OpenAI APIWeb search: off
Here’s a practical way to integrate API tests into GitLab CI.
1) Put your API tests in the repo
Common options:
- Postman/Newman
- pytest + requests
- JUnit/TestNG + REST Assured
- pytest + Schemathesis
- k6 / Karate / Playwright API tests
GitLab CI just needs a command that runs them.
2) Add a .gitlab-ci.yml
A basic pipeline usually has:
buildorprepareapi_test- optional
deploy
Example using pytest:
stages:
- test
api_tests:
stage: test
image: python:3.12
variables:
API_BASE_URL: "https://staging.example.com"
before_script:
- pip install -r requirements.txt
script:
- pytest tests/api --junitxml रिपोर्ट.xml
artifacts:
when: always
reports:
junit: report.xml
paths:
- report.xml
expire_in: 1 week
3) Use environment variables for endpoints/secrets
Store things like:
API_BASE_URLAPI_TOKENCLIENT_IDCLIENT_SECRET
In GitLab:
- Go to Settings → CI/CD → Variables
- Mark sensitive values as masked and protected
Then reference them in your tests:
script:
- pytest tests/api
And in code:
import os
base_url = os.getenv("API_BASE_URL")
token = os.getenv("API_TOKEN")
4) If your API needs a service dependency, run it as a container
If you need a DB, mock server, or the app itself:
stages:
- test
api_tests:
stage: test
image: python:3.12
services:
- name: postgres:16
alias: db
variables:
POSTGRES_DB: testdb
POSTGRES_USER: testuser
POSTGRES_PASSWORD: testpass
before_script:
- pip install -r requirements.txt
script:
- pytest tests/api
If the API is built in the same pipeline, you may deploy it to a review/staging environment first, then run API tests against that URL.
5) Example with Newman (Postman collections)
If your API tests are in Postman:
stages:
- test
newman_tests:
stage: test
image: node:20
script:
- npm install -g newman
- newman run collection.json -e environment.json
You can also publish JUnit reports:
script:
- npm install -g newman newman-reporter-junitfull
- newman run collection.json -r junit --reporter-junit-export results.xml
artifacts:
reports:
junit: results.xml
6) Common best practices
- Run API tests after deployment to a test/staging environment
- Keep tests idempotent when possible
- Use test data setup/teardown
- Add timeouts and retries for flaky network-dependent tests
- Separate:
- smoke tests for quick validation
- full regression tests for slower, broader coverage
You can tag jobs:
api_smoke:
stage: test
script:
- pytest tests/api/smoke
7) Trigger tests only on certain branches
Example:
api_tests:
stage: test
script:
- pytest tests/api
only:
- main
- merge_requests
Or with rules:
api_tests:
stage: test
script:
- pytest tests/api
rules:
- if: $CI_PIPELINE_SOURCE == "merge_request_event"
- if: $CI_COMMIT_BRANCH == "main"
8) A fuller example
stages:
- build
- test
build_app:
stage: build
image: docker:27
script:
- echo "Build step here"
api_tests:
stage: test
image: python:3.12
variables:
API_BASE_URL: "https://staging.example.com"
before_script:
- pip install -r requirements.txt
script:
- pytest tests/api --junitxml=report.xml
artifacts:
when: always
reports:
junit: report.xml
paths:
- report.xml
If you want, I can give you a ready-to-use GitLab CI file for your specific stack, like:
- Postman/Newman
- pytest
- Java + Rest Assured
- Node.js + Supertest
- Kotlin/Karate