Create consistent tests with AI Test Templates

Introducing Test Templates

We’re excited to announce Test Templates, a new capability designed to help teams generate contract tests that match their existing style, frameworks, and SDK versions.

Background

When generating Pact tests, engineers often find that the output does not always align with their project's existing style, idioms, frameworks, and SDK versions. This leads to extra refactoring work, reducing the efficiency gains of automated test generation.

To address this challenge, we are introducing Test Templates—allowing users to define their own templates as code, ensuring generated tests conform to their preferred patterns from the start.

Introducing Test Templates

Test Templates enable teams to generate contract tests using predefined templates that reflect their existing project conventions. This ensures a seamless integration with their current codebase, reducing the need for manual refactoring and improving efficiency.

Key capabilities include:

  • Specify Code-Based Test Templates – Provide a local code-based template when generating tests.
  • Guided Templates – Optionally include additional documentation or instructions on best practices for using a template.
  • RBAC Integration – Restrict template usage with role-based access controls.

Getting Started

To get started, download the latest CLI release. Two new parameters - --template and --instructions - to specify the path to your code template and provide additional prompts, will be available to you when executing the pactflow-ai generate command. The template may be an existing test file, but for best results we recommend creating a clean, well-documented test file.

Example template:

import { SpecificationVersion, PactV4, MatchersV3 } from "@pact-foundation/pact";
import { ThingAPI } from './thing'

// Extract matchers here to improve readability when used in the test
const { like } = MatchersV3;

// Create a 3 level test hierarchy
//
// 1. Top level describe block containing the name of the API being tested
// 2. Describe block for the specific API endpoint
// 3. Test block for the specific test case
// 4. Execute the test case
// 5. Call the API under test
// 6. Assert the response
// 8. Use Pact matchers to constrain and test the Provider response
// 7. Use Jest matchers to assert the API client behaviour

// Top level - name of the API
describe("Thing API", () => {
  // Use the PactV4 class, and serialise the Pact as V4 Pact Specification
  const pact = new PactV4({
    consumer: "ThingConsumer",
    provider: "ThingProvider",
    spec: SpecificationVersion.SPECIFICATION_VERSION_V4,
  });

  // Level 2 - Describe block for the specific API endpoint
  describe("GET /thing/:id", () => {

    // Level 3 - Test block for the specific test case
    test("given a valid thing, returns 200", async () => {
      await pact
        .addInteraction()
        .given("a thing with id 1 exists")
        .uponReceiving("a request for a valid thing")
        // Avoid matchers on the request unless necessary
        .withRequest("GET", "/thing/1", (builder) => {
          builder.headers({ Accept: "application/json" });
        })
        .willRespondWith(200, (builder) => {
          // Use loose matchers where possible, to avoid unnecessary constraints on the provider
          builder.jsonBody(
            like({
              id: 1,
              name: "Thing 1",
              price: 100,
            })
          );
        })
        .executeTest(async (mockserver) => {
          // Instantiate the ThingAPI client
          const ThingAPI = new ThingAPI(mockserver.url);

          // Call the API under test
          const Thing = await ThingAPI.getThingById(1);

          // Use Jest matchers to assert the response
          expect(Thing).toEqual({
            id: 1,
            name: "Some 1",
            price: 100,
          });
        });
    });
  });
});

Example command:

pactflow-ai generate \
  --output ./src/api.pact.spec.ts \
  --language typescript \
  --code ./src/product.js \
  --code ./src/api.js \
  --template ./src/pact.test.template \
  --instructions "Write test cases for the positive (HTTP 200) scenario and negative scenarios, specifically the case of 400, 401 and 404"

Refer to our documentation for further information.

Watch a video

Available Now

Test Templates are available to all cloud customers on all plans.