-
Notifications
You must be signed in to change notification settings - Fork 2
API Testing with Hurl
You can use Hurl to test your API endpoints effectively and integrate testing into your development workflow.
Hurl is a command-line tool that runs HTTP requests defined in a simple plain text format.
It can perform requests, capture values, and evaluate queries on headers and body responses. Hurl is very versatile: it can be used for both fetching data and testing HTTP sessions.
The easiest way to run your API tests is using the built-in Taskfile command:
# Run all API tests
task testThis command automatically:
- Uses the correct variables file (
tests/hurl/.vars) - Runs all tests in the
tests/hurl/directory - Provides clear output about test results
Tests are located in the tests/hurl/ directory:
tests/
├── hurl/
│ ├── .vars # Variables file
│ ├── hello.hurl # Basic endpoint test
│ └── users.hurl # User management tests
Contains environment-specific variables for your tests:
URL=http://localhost:8529/_db/foxx-sandbox/api-dev
GET {{URL}}/
HTTP 200
[Asserts]
jsonpath "$.info.name" == "foxx-service"# Run all tests
task test
# First ensure your service is deployed
task deploy-dockerIf you have hurl installed locally (Installation instructions):
# Run all tests
hurl --test --variables-file tests/hurl/.vars tests/hurl/*.hurl
# Run specific test
hurl --test --variables-file tests/hurl/.vars tests/hurl/hello.hurl
# Run with JUnit output for CI
hurl --test --report-junit test-results/results.xml --variables-file tests/hurl/.vars tests/hurl/*.hurlTesting with hurl running in a Docker container:
# Run all tests
docker run --network host --rm -it -v "$(pwd)/tests/hurl":/app "orangeopensource/hurl:latest" --test --variables-file /app/.vars /app/*.hurl
# Run specific test
docker run --network host --rm -it -v "$(pwd)/tests/hurl":/app "orangeopensource/hurl:latest" --test --variables-file /app/.vars /app/hello.hurl# Request
GET {{URL}}/endpoint
# Expected Response
HTTP 200
# Optional: Response body assertions
[Asserts]
jsonpath "$.status" == "success"
header "Content-Type" contains "application/json"# Create a new user
POST {{URL}}/users
Content-Type: application/json
{
"username": "testuser",
"email": "test@example.com"
}
HTTP 201
[Asserts]
jsonpath "$.user.username" == "testuser"
jsonpath "$.user.email" == "test@example.com"
# Capture user ID for subsequent tests
[Captures]
userId: jsonpath "$.user._key"# Get the created user
GET {{URL}}/users/{{userId}}
HTTP 200
[Asserts]
jsonpath "$.user.username" == "testuser"The project automatically runs Hurl tests in GitHub Actions:
- On every push: Tests run against a fresh ArangoDB instance
- Pull requests: Tests must pass before merging
- Test reports: Results are uploaded as artifacts
The GitHub Actions workflow:
- Sets up ArangoDB in Docker
- Deploys the Foxx service
- Runs Hurl tests with proper variables
- Uploads test results
Ensure your environment is set up correctly:
# 1. Start ArangoDB and deploy service
task docker-db-setup
task deploy-docker
# 2. Verify service is running
curl http://localhost:8529/_db/foxx-sandbox/api-dev/
# 3. Run tests
task testYou can override variables for different environments:
# Development
URL=http://localhost:8529/_db/foxx-sandbox/api-dev
# Staging
URL=https://staging-api.example.com/api
# Production (use with caution)
URL=https://api.example.com/api- Keep tests focused and atomic
- Use descriptive file names (
user-management.hurl,authentication.hurl) - Group related tests in the same file
- Use variables for all environment-specific values
- Keep sensitive data out of test files
- Use meaningful variable names
- Test both success and error cases
- Verify response structure and data types
- Check important headers (Content-Type, etc.)
- Use predictable test data
- Clean up after tests when possible
- Avoid dependencies between test files
-
Tests fail with connection errors:
# Verify service is running task list-services curl http://localhost:8529/_db/foxx-sandbox/api-dev/ -
Variable not found errors:
# Check variables file cat tests/hurl/.vars # Ensure correct path in command
-
Authentication errors:
- Verify database and service are set up correctly
- Check if endpoints require authentication
- Update variables file with correct credentials
-
HTTP version errors:
- Use
HTTP 200instead ofHTTP/* 200 - Hurl is strict about HTTP version syntax
- Use
Run Hurl with verbose output for debugging:
hurl --test --verbose --variables-file tests/hurl/.vars tests/hurl/hello.hurlNote: Test files have been moved from
.api-test/totests/hurl/for better project organization. All commands and documentation have been updated to reflect this change.
Copyright © 2016-2025, Skitsanos™