đ¤ How I Got Tired Of Writing Python Tests. Now AI Writes Them For Me
I donât think AI will replace us in writing code, because someone has to understand the client. But AI can be pretty good at writing unit tests. Of course with our support, but I really like AI that works on something hard, rather something Iâd rather do on myself like painting, drawing and such sorts of things. Thatâs why I really loved CodiumAI when I learned about it when writing about awesome AI projects. From this article youâll learn how to install CodiumAI and use it to generate tests for your Python code in VSCode and PyCharm.
Writing code tests plays a vital role in software development, guaranteeing high-quality code, early bug detection, and enhanced maintainability. Yet, this process often becomes a time-consuming and monotonous endeavor.
Developers frequently find themselves allocating considerable time to writing tests, diverting their attention from actual development. Fortunately, the progress in artificial intelligence (AI) has opened doors to automated test generation, presenting developers with an innovative method for creating code tests. One such tool is CodiumAI, which allows you to automatically generate tests for your code.
With CodiumAI, you receive meaningful and straightforward automated unit tests suggestions directly within your integrated development environment (IDE), enabling you to write better code. Letâs see how we can use CodiumAI on VSCode and PyCharm.
Table of contests:
- How To Use CodiumAI To Generate Tests (VSCode)
- Add Test Example
- Add Input Data
- Change General Configuration
- Modify Framework
- Modify Tests
- Request More Tests
- Delete Single Test
- Run The Tests
- Code Analysis
- Code Suggestions
- Save The Test Suite - How To Use CodiumAI To Generate Tests (PyCharm)
- Add New Custom Test
- Modify Tests
- Delete Single Test
- Change Test Suite Configuration
- Add Input Data
- Change General Configuration
- Modify Framework
- Code Analysis
Letâs dive in:
đ¤ How To Use CodiumAI To Generate Tests (VSCode)
To install Codium in VSCode, go to the âExtensionsâ on the sidebar and click on it. Search for âCodiumAIâ and press enter. Click on the Install button to install the extension. Thatâs all you have to do to set up your AI unit test generator. Isnât it so easy?
Now you might be wondering how to use CodiumAI to write tests. Sit tight, we got you. Letâs take a simple Python program and try writing tests for it:
This code is pretty simple, but you can also easily write tests for much more complex code with some clicks using CodiumAI.
There are two ways to generate tests, by clicking on the âGenerate testsâ button or selecting the code snippet and then generating tests for that.
Firstly, letâs just try generating tests by clicking on the âGenerate testsâ button which is right above our functions:
Another way is to select the code snippet, right-click on it, and choose âCodiumAI â Generate Testsâ to automatically generate tests for that code:
Whatever way you choose to generate tests, you will be presented with a test results window once the tests have been generated:
You can also configure the tests according to your requirements.
Add Test Example
This test example will represent what you would like the generated tests to look like in terms of style, setup, etc.:
Add Input Data
Adding example code demonstrates the needed inputs for the component under test:
Change General Configuration
Modify Framework
Modify Tests
Donât like the test code? You can simply type the changes inside the âSuggest changesâ field, and it will automatically modify the tests based on your requirements:
You can also get an alternative test code by clicking on the âReflect & Regenerateâ button:
Request More Tests
Click on âGive me more testsâ at the bottom of the test suite to generate some more tests. You can also write your own custom test by clicking on the âNew custom testâ button:
Protip: If you canât see the âGenerate Testsâ button you might have to set âeditor.codeLensâ: true, in your VSCode configuration file.
Delete Single Test
You can delete a specific test by clicking on the delete button:
Run The Tests
You can run a single test by clicking on the âRunâ button on the top right of that test, or you can run all tests by clicking on the âRun All Testsâ button:
Our code is perfectly fine, so it would pass all tests. Letâs just change the code so that it does the wrong multiplication. I added -1 to the return value so that it returns a wrong result for multiplication every time itâs called:
Letâs regenerate the tests and run all tests:
As expected the tests failed. You can see the log by clicking on the âFailedâ button. But nothing to worry about, CodiumAI got our back. You can easily fix the test by clicking on Reflect & Regenerate button. But in our case, the problem is with the code and not with the test itself, so you can get the possible fixes by clicking on the âPossible Code Bugâ button:
Protip: You can show and hide the CodiumAI panel in the extension settings or from the command palette.
Code Analysis
You can easily get your code analysis done by the TestGPT model by clicking on the âCode Analysisâ tab:
Code Suggestions
Along with the code analysis, the TestGPT model can also suggest some code changes to improve performance, correctness, and more. You can get suggestions by clicking on the âCode Suggestionsâ tab:
Select whatever suggestion you like and click on the âPrepare Code Changesâ button. After that merge the changes:
Save The Test Suite
Once you are satisfied with the generated tests, you can save them by clicking on âCopy testsâ and copying the tests code, or you can open the tests in a separate file by clicking on âOpen as fileâ and then save that file:
đ¤ How to Use CodiumAI to Generate Tests (PyCharm)
Like VSCode, set up on JetBrains Pycharm is very easy. First, you have to open Pycharm and click on âPluginsâ. After that search for âCodiumAIâ and click on the âInstallâ button to install the plugin:
Letâs take our last example code and generate tests for that. To generate the tests for your code, you need to click on the âGenerate testsâ button and the plugin will generate a set of tests for your code:
Add New Custom Test
Would you like to include a personalized test that wasnât generated automatically? Simply click on the âNew custom testâ button, provide the test name and objective, and the TestGPT model will generate the corresponding test code for you, taking into account your preferences:
Modify Tests
Donât like the test code? You can simply type the changes inside the âSuggest changesâ field, and it will automatically modify the tests based on your requirements:
Delete Single Test
You can delete a specific test by clicking on the delete button:
Change Test Suit Configuration:
Enhance and customize your test suite by providing general instructions, sample inputs, and tests, and selecting your preferred testing framework in the Configuration section.
Add Test Example
Add Input Data
Change General Configuration
Modify Framework
Code Analysis
You can easily get your code analysis done by the TestGPT model by clicking on the âCode Analysisâ tab:
I really like how CodiumAI boosts my Python unit test writing abilities. CodiumAI has a great gain / cost ratio. Meaning, it is almost effortless to use, and you save a lot of time using it.
Links
Here are the links to the official page of CodiumAI and extensions for VSCode and JetBrains:
If youâre interested in programming and AI donât forget to subscribe to my articles!