🤖 How I Got Tired Of Writing Python Tests. Now AI Writes Them For Me

Tom Smykowski
8 min readMay 30, 2023

--

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:

Official Page

VSCode Extension

JetBrains Extension

If you’re interested in programming and AI don’t forget to subscribe to my articles!

--

--

Tom Smykowski

Software Engineer & Tech Editor. Top 2% on StackOverflow, 3mil views on Quora. Won Shattered Pixel Dungeon.