EarlyAI Hello World
Maintaining high-quality code is a constant challenge for developers. As projects grow in complexity, ensuring that every component functions correctly becomes increasingly difficult. This is where unit testing comes into play. Unit tests are essential for verifying that individual pieces of code work as intended, catching bugs early, and ensuring that changes do not introduce new issues. However, writing unit tests can be a tedious and time-consuming task, especially when done manually.
In this blog post I present EarlyA by Early, a genAI based dev tool for developers that automatically generates high quality, working unit tests based on your code in minutes. We explain how EarlyAI works and walk you through three major use cases where Early can be helpful: writing a new feature, resolving a bug and handling private methods.
As the co-founder and CTO of Early, with over 20 years of experience in writing code and managing several R&D teams, I have seen firsthand the struggles developers face in maintaining high-quality code. This extension is a culmination of my extensive experience and understanding of the developer’s needs.
The Challenges of Writing Unit Tests
Traditional unit test creation often involves setting up test environments, mocking dependencies, and writing extensive test cases to cover all possible scenarios. This process is not only labor-intensive but also prone to human error.
Developers might overlook edge cases or make mistakes in the test setup, leading to incomplete or inaccurate tests. Moreover, the time required to write these tests can be a significant burden, detracting from the time available to develop new features or fix existing bugs.
The Solution: EarlyAI by Early
In response to these challenges, Early has developed a genAI-based VSCode extension designed to simplify and automate the unit test creation process. This extension generates unit tests with just a click, providing a seamless and efficient way to ensure code quality. By automating the tedious aspects of writing unit tests, the extension allows developers to focus on what truly matters: writing efficient and reliable code.
Use Case #1: Writing a New Feature
When adding a new feature, my approach usually begins by defining a new method in a class, focusing on its signature and return type. I also provide some comments describing what the method should accomplish. At this stage, I turn to the extension to automatically generate unit tests. The extension lays the groundwork for these tests by:
• Mocking the class constructor parameters and method parameters.
• Instantiating necessary concrete objects.
• Creating tests for method invocation and edge cases I might overlook while coding.
Writing a function with signature, return type, and no implementation. Click the code lens and let the extension do its work
Example1: Test suit is generated by Early AI with best practices in mind. See the nested describes with the required Mock objects.
Check the above createTodoSignitureOnly() method on our sample todo-app. and the generated tests here.
Use Case #2: Fixing a Bug
When it comes to fixing bugs, the TDD approach becomes a major time saver. Once I’ve pinpointed the bug, my first step is to generate tests using EarlyAI’s extension for the affected method. Again, describing the method’s functionality and the nature of the bug in comments leads to better test generation.
This approach often produces a red test that captures the bug before I even start fixing it. Achieving a failing test before development ensures the bug is accurately addressed, embodying the true essence of automated TDD.
Example2: This is the same test suite as above. The second nested Describe is for “edge cases”. BTW, two of the tests failed because of bugs in my TodoSample app.
Use Case #3: Handling Private Functions
Occasionally, a bug or feature requires modifications to private functions. Since the extension only generates tests for public functions, I temporarily make these private functions public to generate the necessary tests. This strategy is a huge time saver, allowing me to debug smaller pieces of code without navigating through the entire codebase to locate the issue. Once the tests are generated and validated, I revert the functions back to private, ensuring the integrity of the code structure is maintained
Next Steps for Developers
Incorporating Early AI’s VSCode extension into my workflow has revolutionized my development process. Whether I’m writing new features or fixing bugs, the extension saves time and enhances code quality. By automating the tedious parts of creating unit tests, it allows me to focus on what truly matters: writing efficient and reliable code. If you’re looking to streamline your development process and improve your testing efficiency, give this extension a try. You might find it as indispensable as I do.
This is my first blog for Early AI, and I am excited to share the tools and techniques that have made a significant impact on my development journey. With over two decades of experience, I can confidently say that innovations like this extension are game-changers in the world of software development.
You can see a full example at GitHub. Feel free to generate these tests yourself by looking up Early AI’s extension in the VS Code Marketplace here.
Lior Froimovich, Co-founder and CTO