How to Generate Tests
Early AI makes generating unit tests effortless with two intuitive methods. This guide will walk you through both approaches, ensuring you can generate high-quality tests for your code with ease.
Watch It in Action 🎥​
Here’s a quick demo showing how to generate tests using Early AI:
1. Using Code Lens​
One of the simplest ways to generate tests is through Code Lens. Code Lens appears directly in your code editor above each testable function.
Steps to Generate Tests with Code Lens:​
-
Open your code file in Visual Studio Code.
-
Locate the Code Lens link above a function that is accessible outside the file.
- The link is represented by a "Early AI: Generate Tests" label.
-
Click the Generate Tests link.
-
The extension will process the function and generate a test file containing comprehensive unit tests for it.
2. Using the Extension’s Sidebar View​
The Early AI extension provides a tree view in the sidebar, displaying all testable code in your project. The project structure is retained, and you can navigate to specific functions for test generation.
Key Features of the Sidebar View:​
- Testable Code Only: Files that do not contain testable code are not displayed in the tree view.
- Function Accessibility: Functions that are not accessible from outside their file are displayed but are greyed out, indicating that test generation is not available for them.
Steps to Generate Tests from the Sidebar:​
-
Open the Early AI extension view in Visual Studio Code.
-
Browse through the project tree to locate the file or function you want to test.
- Testable functions are highlighted, while greyed-out functions indicate restricted accessibility.
-
Next to each testable function, you’ll see a magic wand icon.
- This icon represents the "Generate Tests" action.
-
Click the magic wand icon for the desired function.
-
The extension will generate a test file with tests tailored for the selected function.
Notes​
- Customization: Generated tests are tailored based on the testing framework configured in the extension settings (e.g., Jest, Mocha, Pytest, Vitest).
- Test Coverage: Functions displayed in the tree view are determined by the extension's analysis of your code structure and accessibility.
- Automation: Both methods streamline the test generation process, allowing you to focus on enhancing code quality without manually writing test cases.
With these two methods, you can efficiently generate unit tests for your project, leveraging Early AI's powerful capabilities. Choose the method that best fits your workflow and start building better, more reliable software today!