agentleFS
MathiasPaulenkoMathiasPaulenko/ai-toolkit/skills/karate-api-testing/SKILL.md

karate-api-testing

Skill2 starsChanged 4 months agoMITView on GitHub

API testing with Karate DSL: REST, GraphQL, gRPC, mocking, parallel execution, CI/CD integration, and data-driven tests.

  • Reads credentials

The file

Files
SKILL.md

Karate API Testing

Invoke when user asks about Karate DSL, API testing with Karate, or BDD-style API automation.

Core Principles

  • DSL first: Tests are readable by non-developers.
  • Zero Java required: JSON/YAML-like syntax, no compilation needed.
  • Built-in: assertions, JSON/XML parsing, parallel execution, mocking.

Feature File Structure

Feature: User API

  Background:
    * url 'https://api.example.com'
    * def token = call read('classpath:auth.js')
    * header Authorization = 'Bearer ' + token

  Scenario: Create and retrieve user
    Given path 'users'
    And request { name: 'Alice', email: 'alice@test.com' }
    When method post
    Then status 201
    And match response ==
      """
      {
        id: '#number',
        name: 'Alice',
        email: 'alice@test.com',
        createdAt: '#ignore'
      }
      """
    And def userId = response.id

    Given path 'users', userId
    When method get
    Then status 200
    And match response.name == 'Alice'

Key Syntax Patterns

Match Operators

Then match response.name == 'Alice'          # Exact
Then match response.id == '#number'          # Type check
Then match response.tags == '#[]'            # Array
Then match response.tags == '#[2] #'         # Array of 2+ items
Then match response.status == '#present'     # Non-null
Then match response.meta == '#notpresent'    # Absent

Dynamic Data

# JavaScript function
* def generateEmail =
  """
  function() {
    var uuid = java.util.UUID.randomUUID().toString();
    return 'test-' + uuid + '@example.com';
  }
  """
* def email = generateEmail()

Data-Driven Tests

Scenario Outline: Login with <scenario>
  Given path 'auth/login'
  And request { email: '<email>', password: '<password>' }
  When method post
  Then status <status>

  Examples:
    | scenario | email             | password | status |
    | valid    | alice@test.com    | correct  | 200    |
    | wrong_pw | alice@test.com    | wrong    | 401    |
    | missing  |                   |          | 400    |

Reusable Helpers

# karate-config.js
function fn() {
  var env = karate.env || 'dev';
  var config = {
    baseUrl: 'https:/' + '/'+ env + '.api.example.com',
    timeout: 30000,
  };

  if (env === 'prod') {
    config.token = karate.read('classpath:secrets/prod-token.txt');
  }

  return config;
}
# common.feature
@ignore
Feature: Common Steps

  Scenario: Authenticate
    Given path 'auth/token'
    And request { client_id: '#(clientId)', client_secret: '#(clientSecret)' }
    When method post
    Then status 200
    And def accessToken = response.access_token

GraphQL Testing

Feature: GraphQL Users

  Scenario: Query users
    Given path 'graphql'
    And request
      """
      {
        query: "{ users(limit: 5) { id name } }"
      }
      """
    When method post
    Then status 200
    And match $.data.users == '#[_ > 0]'

Mocking with Karate Netty

# Start mock server
java -jar karate.jar -m mock.feature -p 8080
# mock.feature
Feature: Mock API

  Scenario: pathMatches('/users') && methodIs('get')
    * def response = read('classpath:fixtures/users.json')
    * def status = 200

CI/CD Integration

<!-- pom.xml -->
<plugin>
  <groupId>com.intuit.karate</groupId>
  <artifactId>karate-maven-plugin</artifactId>
  <version>1.5.0</version>
  <executions>
    <execution>
      <goals><goal>test</goal></goals>
    </execution>
  </executions>
</plugin>
# Run all
mvn test -Dtest=KarateTest

# Run tagged
mvn test -Dkarate.options="--tags @smoke"

Reporting

  • Karate HTML report: target/karate-reports/karate-summary.html
  • Cucumber JSON: add cucumber-report dependency
  • Allure: add allure-cucumber adapter

Anti-Patterns

Anti-PatternFix
Inline large payloadsUse read('classpath:json/user.json')
Hardcoded IDsGenerate UUIDs or use fixtures
One mega-featureSplit by domain: users.feature, orders.feature
Ignoring response structureUse #present / #notpresent selectively

Example: End-to-End Flow

Feature: Order Flow

  Background:
    * url baseUrl
    * call read('classpath:auth.feature')

  Scenario: Place order end-to-end
    # Create product
    Given path 'products'
    And request { name: 'Widget', price: 19.99 }
    When method post
    Then status 201
    And def productId = response.id

    # Add to cart
    Given path 'cart'
    And request { productId: '#(productId)', qty: 2 }
    When method post
    Then status 200

    # Checkout
    Given path 'orders'
    And request { items: [{ productId: '#(productId)', qty: 2 }], payment: 'card' }
    When method post
    Then status 201
    And match response.total == 39.98
    And match response.status == 'confirmed'

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.