AI-Powered Test Generation
Relevant source files
The following files were used as context for generating this wiki page:
The AI-Powered Test Generation feature is the core of the TestGen platform. It automates the creation of test cases by analyzing a given code repository using Google's Gemini AI. The system is designed to clone a public GitHub repository, read its source files, generate a comprehensive context, and then use that context to prompt the AI to produce a suite of tests, including unit, integration, edge case, and error handling scenarios.
This process involves a frontend client, a backend service (which has both a Go implementation and a client-side browser implementation), and the external Google Gemini API. The user initiates the process from a web dashboard, providing a repository URL and an API key, and receives structured test cases that can be executed within the browser.
Sources: README.md:3-5, testgen/IMPLEMENTATION.md:3-14
Architecture Overview
The system is composed of three main parts: a React-based frontend, a Go-based backend, and the Google Gemini AI service. The frontend provides the user interface for inputting a repository URL and viewing results. The backend is responsible for cloning the repository and preparing the code for the AI. The Gemini API is the external service that performs the actual test case generation based on the code context provided by the backend.
The diagram below illustrates the high-level architecture and interactions between components.
This diagram shows the flow from the frontend making a request to the backend, which then interacts with GitHub and Gemini to generate and return test cases.
Sources: README.md:16-25, testgen-backend/main.go
System Workflow
The test generation process follows a multi-step workflow, from user input to the final display of generated tests.
1. Repository Analysis
The first step is to acquire and analyze the source code from a GitHub repository. The system supports two methods for this: a server-side git clone operation and a client-side fetch using the GitHub API.
Go Backend: Server-Side Cloning
The Go backend provides an API endpoint (POST /api/clone-repo) that takes a GitHub repository URL. It parses the URL to get the owner and repo name, then clones the repository to a temporary local directory using the git clone command.
After cloning, it walks the repository directory, reading files and filtering out irrelevant ones based on a predefined exclusion list. This list includes directories like node_modules, .git, and files like *.md and LICENSE. It also filters out files larger than 1MB.
Sources: testgen-backend/main.go:174-200, testgen-backend/main.go:203-258, testgen-backend/main.go:82-101
TypeScript Frontend: Client-Side Fetching
As an alternative to the Go backend, the frontend can perform repository analysis directly in the browser. The ClientBackend.cloneRepository function in testgen/src/lib/backend.ts uses the GitHub API to fetch repository contents without needing a server. It parses the URL, fetches the repository tree, and recursively retrieves individual file contents.
This client-side approach also filters files using a similar exclusion list and size constraints (e.g., < 50KB). This method is beneficial for simplified deployment but is subject to GitHub API rate limits and browser CORS policies.
Sources: testgen/src/lib/backend.ts:220-286, testgen/CLIENT_BACKEND.md:5-11, testgen/src/lib/github.ts:100-165
The overall analysis flow is as follows:
This flowchart shows the two paths for acquiring repository code before it's compiled into a context for the AI.
2. Context Generation
Once the relevant source files are collected, they are concatenated into a single string known as the "code context." This context is formatted to be easily understood by the Gemini AI model. Files are grouped by type (e.g., Go source files, configuration files) and are preceded by a comment indicating their path.
The generatePromptContext function, present in both the Go backend and the client-side TypeScript implementation, is responsible for creating this structured text.
Sources: testgen-backend/main.go:260-313, testgen/src/lib/backend.ts:340-391
An example of the context structure:
=== REPOSITORY CODE CONTEXT FOR TEST GENERATION ===
This context contains all source code files from the cloned repository.
...
=== GO SOURCE FILES ===
// File: main.go
package main
...
---
=== CONFIGURATION FILES ===
// File: go.mod
module github.com/strikertushar19/testgenai
...
---
=== END OF CONTEXT ===
Sources: testgen-backend/main.go:263-313
3. AI Test Case Generation
With the code context prepared, the system calls the Google Gemini API to generate the test cases. This is handled by the /api/generate-tests endpoint in the Go backend or the ClientBackend.generateTests function in the frontend.
A detailed prompt is constructed, which includes the code context and instructions for the AI to generate tests in a specific JSON format. The request specifies the model (gemini-1.5-flash-latest) and generation parameters like temperature and max tokens.
The sequence diagram below shows the interaction for generating tests.
This diagram illustrates the call sequence from the application to the Gemini API to get structured test cases.
Sources: testgen-backend/main.go:440-588, testgen/src/lib/backend.ts:468-558, testgen/src/pages/Dashboard.tsx:121-155
4. Test Execution
After the test cases are generated and sent to the frontend, they can be executed. The TestRunner.tsx component provides a sandboxed JavaScript environment in the browser to run the tests. It includes a library of mock functions for common operations like user management (CreateUser, Login) and utilities (validateEmail, fetchData).
The test runner dynamically determines which function to call based on the code field of the test case, executes it with the provided input, and compares the result against the expected output to determine if the test passed or failed.
Sources: testgen/src/components/TestRunner.tsx:32-205, README.md:73-84
API and Data Structures
The system relies on a set of API endpoints and data structures to manage the workflow.
Backend API Endpoints
The Go backend exposes the following endpoints:
| Method | Endpoint | Description |
|---|---|---|
POST | /api/clone-repo | Clones a GitHub repository, reads its files, and returns the generated code context and file list. |
POST | /api/generate-tests | Sends the code context and API key to Gemini to generate test cases. |
GET | /api/context/{owner}/{repo} | Retrieves a previously generated and saved context file for a repository. |
Sources: testgen-backend/main.go:593-595
Key Data Structures
The following interfaces define the primary data objects exchanged between the client, backend, and AI service.
| Struct/Interface | File | Description |
|---|---|---|
RepoRequest | testgen-backend/main.go:11 | Contains the repoUrl to be cloned. |
RepoResponse | testgen-backend/main.go:22 | Response after cloning, includes success status, file count, and a list of FileContent objects. |
GeminiRequest | testgen-backend/main.go:56 | Request to the test generation endpoint, containing the apiKey, codeContext, and an optional prompt. |
GeminiTestCase | testgen-backend/main.go:31 | Defines the structure for a single AI-generated test case, with fields like name, input, expected. |
GeminiResponse | testgen-backend/main.go:41 | The complete JSON response from the /api/generate-tests endpoint, containing a list of test cases. |
Sources: testgen-backend/main.go:11-60, testgen/src/lib/backend.ts:4-44
Conclusion
The AI-Powered Test Generation feature provides a powerful and streamlined solution for automating test creation. By integrating repository analysis with the generative capabilities of Google's Gemini AI, it significantly reduces the manual effort required to develop comprehensive test suites. The architecture is flexible, supporting both a robust Go backend and a lightweight, serverless client-side implementation, making it adaptable to different deployment scenarios.
Sources: README.md:3-5, testgen/IMPLEMENTATION.md:3-5