The Unit Test Case Generator is a Generative AI-based tool that automates the creation of unit test cases from provided code snippets or entire code files. It uses OpenAI's GPT-3.5-turbo model via the ChatCompleteAsync API, the system generates both the test case scenarios and the corresponding unit test code. The project is implemented with a .NET Core Web API backend and a React-based frontend.
Generate unit tests from a file
The system architecture consists of three primary components:
Frontend: A React application responsible for user interaction, code input, and displaying generated unit test cases.
Backend: A .NET Core Web API that handles requests from the frontend, interacts with the OpenAI API, and processes the results.
OpenAI API: The generative AI engine that creates the unit test cases, and their code based on the provided input.
- NavBar.jsx: For handling navigation and routing to components.
- GenerateUnitTestFromFile.jsx: Allows users to input code snippets, communicates with the backend, and displays generated unit tests.
- GenerateUnitTestFromSnippet.jsx: Handles code file uploads, sends data to the backend, and displays generated unit tests.
- RenderOutput.jsx: Allows the output to be rendered as a react code block.
API Endpoints:
- '/api/GenerateUnitTestCase/GenerateUnitTestFromSnippet'
- It receives a code snippet and returns the generated test cases.
- '/api/GenerateUnitTestCase/GenerateUnitTestFromFile'
- It receives a code file and returns the generated test cases.
Services:
- OpenAIService.cs:
- Constructs the prompt using the provided code input.
- Sends the prompt to the OpenAI ChatCompleteAsync API.
- Receives and processes the response to extract relevant unit test cases and code.
OpenAI API:
GPT-3.5-turbo is chosen for its balance between performance and cost-efficiency.
The prompt includes a system message that defines the behaviour of the response expected and a user message that contains the code snippet or file content.
System Message: You are a helpful assistant, who generates unit tests cases and unit test code from a code snippet provided to you in the same language. Start with generating a comprehensive list of unit test cases for the supplied code followed by the unit test code for each of the identified test cases. In the event user has not provided a code example then respond politely asking to enter a valid code example.
The ChatCompleteAsync API is used to interact with the GPT-3.5-turbo model.
This API is provided by the OpenAI NuGet package .NuGet Gallery | OpenAI 2.0.0-beta.2
- The user inputs a code snippet or uploads a code file.
- The input is sent to the backend API for processing.
- The generated unit test cases are displayed on the frontend.
- The controller receives input from the frontend.
- The input is sent to OpenAI via the OpenAIService.
- The unit test cases are returned to the frontend.
This was a good learning experience that helped me develop my understanding of working with Chat GPT and React. I have learned how a generative ai model can be used to solve a problem technically.