> ## Documentation Index
> Fetch the complete documentation index at: https://docs.coderabbit.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Generate unit tests

> Generate comprehensive unit tests automatically for your pull requests, including edge cases and error conditions.

export const OpenBetaBadge = ({tip = "This feature is currently in open beta. We are actively improving it based on your feedback. If you encounter any issues or have suggestions, please share them on our Discord community or visit the support page.", title = "Open Beta", cta = "Contact support", href = "/support", disabled = false}) => {
  return <Tooltip tip={tip} cta={cta} href={href}>
        <Badge icon="badge-alert" disabled={disabled || undefined}>
            {title}
        </Badge>
    </Tooltip>;
};

export const ProPlusPlanBadge = ({tip = "This feature is available as part of the Pro+ plan and Enterprise plan. Please refer to our pricing page for more information about our plans and features.", title = "Pro+ Plan", cta = "Read more", href = "https://coderabbit.ai/pricing", disabled = false}) => {
  return <Tooltip tip={tip} cta={cta} href={href}>
        <Badge icon="shield-plus" disabled={disabled || undefined}>
            {title}
        </Badge>
    </Tooltip>;
};

<OpenBetaBadge />

<ProPlusPlanBadge />

## Platform support

<CardGroup cols={1}>
  <Card title="GitHub" icon="github" horizontal>
    Version control and CI/CD pipelines (GitHub Actions)
  </Card>
</CardGroup>

<img src="https://mintcdn.com/coderabbit/D_rqUjBaYiE185JH/images/finishing-touches/assets/images/utg.gif?s=fc4df621b719a224133ebd133c3a4384" alt="Unit Test Generation" width="1920" height="1080" data-path="images/finishing-touches/assets/images/utg.gif" />

AI coding tools let you write code 10x faster, but comprehensive testing still happens manually. CodeRabbit bridges this gap by analyzing your code changes and generating sophisticated unit tests that understand your project's patterns, testing frameworks, and edge cases.

<CardGroup cols={2}>
  <Card title="PR comment command" icon="message-square-code">
    Type `@coderabbitai generate unit tests` in any PR comment to generate tests
    automatically
  </Card>

  <Card title="UI checkbox" icon="square-check-big">
    Use the **Generate unit tests** checkbox in the CodeRabbit Walkthrough for
    one-click generation
  </Card>
</CardGroup>

## How it works

<Steps>
  <Step title="Request test generation">
    Comment `@coderabbitai generate unit tests` in your PR or check the box in
    CodeRabbit Walkthrough
  </Step>

  <Step title="Context-aware analysis">
    CodeRabbit examines your code changes with full context - understanding your
    testing frameworks, existing patterns, and team guidelines from previous
    reviews to identify what needs coverage
  </Step>

  <Step title="Intelligent test generation">
    Tests are generated based on your project's specific context, including
    proper mocking strategies, framework conventions, and edge cases that matter
    for your codebase
  </Step>

  <Step title="Choose delivery method">
    Receive tests in a separate PR or as a new commit on your current PR
    branch
  </Step>

  <Step title="Automatic CI integration">
    For separate PRs, CodeRabbit analyzes your GitHub Actions workflows and
    fixes build failures, import errors, and dependency issues automatically
  </Step>
</Steps>

CodeRabbit's test generation goes beyond basic templates. It understands your project's testing patterns and generates tests that actually integrate with your existing test suite.

## Output delivery options

<Tabs>
  <Tab title="Separate PR">
    **Recommended for most teams** CodeRabbit creates a new pull request
    containing all generated tests. This approach keeps your feature PR focused
    while allowing independent review of test code. **Key advantage**:
    CodeRabbit automatically fixes CI/CD integration issues. If tests fail due
    to missing dependencies, import errors, or configuration problems,
    CodeRabbit analyzes your GitHub Actions logs and pushes fixes. This means
    you get working tests, not just test code that looks right.
  </Tab>

  <Tab title="Same PR commit">
    Tests are added as a new commit to your existing pull request. This keeps
    feature code and tests together but expands your PR scope. Good for smaller
    changes where you want everything reviewed together.
  </Tab>
</Tabs>

## Path-specific customization

Configure test generation for different parts of your codebase using `.coderabbit.yaml`:

```yaml theme={null}
code_generation:
  unit_tests:
    path_instructions:
      - path: "**/*.ts"
        instructions: |
          Use vitest for testing framework.
          Generate comprehensive test cases including edge cases and error conditions.
          Include proper TypeScript types in test expectations.
      - path: "**/api/**"
        instructions: |
          Focus on request/response validation and error handling.
          Mock external API calls using MSW.
          Test authentication middleware and rate limiting.
      - path: "**/components/**"
        instructions: |
          Use React Testing Library for component tests.
          Test user interactions, accessibility, and error boundaries.
          Mock complex props and verify state changes.
```

These instructions become part of CodeRabbit's context for future test generation, continuously improving test quality and consistency across your codebase.

## What's next

<CardGroup cols={1}>
  <Card title="Finishing Touches overview" href="/finishing-touches" icon="sparkles" horizontal>
    See all available finishing touches and how to trigger them from any PR
  </Card>

  <Card title="Autofix" href="/finishing-touches/autofix" icon="wrench" horizontal>
    Automatically implement fixes for unresolved CodeRabbit review findings
  </Card>

  <Card title="Resolve merge conflicts" href="/finishing-touches/resolve-merge-conflict" icon="git-merge" horizontal>
    Let CodeRabbit analyze the intent behind conflicting changes and commit a clean resolution to your branch
  </Card>
</CardGroup>
