DEV Community

Cover image for 🧪 API Testing with Swagger: Functional, Security, and Performance in One Powerful Tool
Sergio Alberto Colque Ponce
Sergio Alberto Colque Ponce

Posted on

🧪 API Testing with Swagger: Functional, Security, and Performance in One Powerful Tool

🔍 Introduction

API testing is crucial in today’s software development lifecycle. Among many available tools, Swagger stands out for its integration with the OpenAPI Specification and its powerful capabilities for functional, performance, and security testing.

In this article, I’ll show how to use Swagger Inspector for testing APIs with real-world examples. We’ll also explore how Swagger integrates into CI/CD pipelines and supports teams in large-scale API development.

🚀 What is Swagger?

Swagger is a suite of open-source and commercial tools for API development and testing, based on the OpenAPI Specification.

Key components:

  • Swagger Inspector: Test endpoints and validate responses interactively.
  • SwaggerHub: Collaborative platform for designing and documenting APIs.
  • Swagger UI: Visualize and interact with APIs.
  • Swagger Codegen: Generate client libraries from API definitions.

✅ Why Choose Swagger for API Testing?

  • Supports REST, SOAP, and GraphQL
  • Auto-generates assertions based on OpenAPI
  • Schema validation out of the box
  • Performance/load testing with synthetic data
  • Free and open source

💡 Real-World Example: Testing a Public API

We'll test the JSONPlaceholder API () using Swagger Inspector.

https://jsonplaceholder.typicode.com/posts

Step 1: Open Swagger Inspector
Step 2: Send a GET request

GET https://jsonplaceholder.typicode.com/posts/1
Enter fullscreen mode Exit fullscreen mode

Step 3: Analyze the response

  • Status Code: 200
  • Response Time: ~50ms
  • Content-Type: application/json
  • Body:
{
  "userId": 1,
  "id": 1,
  "title": "sunt aut facere...",
  "body": "quia et suscipit..."
}
Enter fullscreen mode Exit fullscreen mode

Step 4: Add Assertions
Swagger automatically suggests:

  • Status is 200 OK
  • Response body contains id
  • Header contains Content-Type: application/json

You can customize and add more validations manually.

⚙️ Schema Validation Example

When importing an OpenAPI definition, Swagger Inspector will:

  • Validate endpoint responses against the schema
  • Highlight mismatches and missing fields
  • Generate test assertions

yaml

paths:
  /posts/{id}:
    get:
      summary: Get post by ID
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Post'
components:
  schemas:
    Post:
      type: object
      properties:
        id:
          type: integer
        userId:
          type: integer
        title:
          type: string
        body:
          type: string
Enter fullscreen mode Exit fullscreen mode

📦 Integrating Swagger Tests into CI/CD

Swagger Inspector allows exporting tests into ReadyAPI or Postman-compatible collections.

  • Export test suite
  • Integrate in Jenkins/GitHub Actions
  • Run assertions automatically after deployments

yaml

name: Run Swagger API Tests
on: [push]
jobs:
  test:
    runs-on: ubuntu-latest
    steps:
    - name: Download tests
      run: curl -O swagger-tests.json
    - name: Run tests
      run: newman run swagger-tests.json
Enter fullscreen mode Exit fullscreen mode

✍️ Final Thoughts

Swagger is more than just a documentation tool — it’s a powerful API testing framework that helps ensure quality, reliability, and performance from the first line of the API spec.

🔗 References

  1. Swagger Official Website: https://swagger.io/
  2. JSONPlaceholder API: https://jsonplaceholder.typicode.com/
  3. Top 10 API Testing Tools: https://alicealdaine.medium.com/top-10-api-testing-tools-rest-soap-services-5395cb03cfa9

Top comments (0)