How To Generate Tests Using CodiumAI
Tags: ai, testing
Published: 2024-01-22 20:37:58

CodiumAI is an IDE extension that allows you to generate test suites.

CodiumAI analyzes the entirety of your code and then suggests tests as you code.

CodiumAI's main vision is to help developers create better code faster.

To use CodiumAI, you will need:

1. VS Code or JetBrains IDE installed on your laptop.

2. Go to [CodiumAI](https://www.codium.ai/) and download CodiumAI on your laptop.

This tutorial uses Visual Studio Code with Java.

Once you have Visual Studio Code installed on your laptop, on CodiumAI, select the option to download the free version of your Visual Studio Code.

## How to use CodiumAI on Visual Studio Code

Create a Java project on your Visual Studio Code called `CodiumAI_Project`.

Once you have created your project, open it on your Visual Studio Code.

This is what you should see:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1705345917843/31668357-1284-44ae-bb32-4e93c7d5a2b1.png)

Select the CodiumAI icon at the bottom of the left sidebar to use CodiumAI on Visual Studio Code:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1705346157836/6db8143c-96a1-4792-9c44-4fc8061e5b84.jpeg)

At the bottom, select **No Context**. Selecting **No Context** allows you to ask a question without context. For example, you can ask CodiumAI to generate a class called `Vehicle` with `brand`, `colour` and `purpose_of_use` as private fields.

The code generated will initially be written in Python like this:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1705347311902/de9ba073-6c42-4a39-893f-218c4370eff3.png)

```
class Vehicle: def __init__ (self, brand, colour, purpose_of_use): self. __brand = brand self.__ colour = colour self.__purpose_of_use = purpose_of_use # getter methods def get_brand(self): return self.__brand def get_colour(self): return self.__colour def get_purpose_of_use(self): return self.__purpose_of_use # setter methods def set_brand(self, brand): self.__brand = brand def set_colour(self, colour): self.__colour = colour def set_purpose_of_use(self, purpose_of_use): self.__purpose_of_use = purpose_of_use
```

CodiumAI allows you to translate code from one programming language to another. Since this tutorial uses Java, you can translate the Python-generated code into Java.

Select **Continue this chat** , and ask CodiumAI to change the code to Java.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1705347617327/6945b809-daa0-492f-9d69-c19011c55881.png)

```
public class Vehicle { private String brand; private String colour; private String purposeOfUse; // constructor public Vehicle(String brand, String colour, String purposeOfUse) { this.brand = brand; this.colour = colour; this.purposeOfUse = purposeOfUse; } // getter methods public String getBrand() { return this.brand; } public String getColour() { return this.colour; } public String getPurposeOfUse() { return this.purposeOfUse; } // setter methods public void setBrand(String brand) { this.brand = brand; } public void setColour(String colour) { this.colour = colour; } public void setPurposeOfUse(String purposeOfUse) { this.purposeOfUse = purposeOfUse; }}
```

With CodiumAI, you can generate a test suite for your desired code. Select **Continue this chat** , and ask CodiumAI to generate tests for the above class `Vehicle`.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1705347944003/1a0a5e81-1e97-4685-ae72-ac1fbcf15d18.png)

```
import org.junit.jupiter.api.Test;import static org.junit.jupiter.api.Assertions.assertEquals;public class VehicleTest { @Test public void testVehicle() { // create an instance of Vehicle Vehicle vehicle = new Vehicle("Toyota", "Red", "Personal"); // test getters assertEquals("Toyota", vehicle.getBrand()); assertEquals("Red", vehicle.getColour()); assertEquals("Personal", vehicle.getPurposeOfUse()); // test setters vehicle.setBrand("Honda"); vehicle.setColour("Blue"); vehicle.setPurposeOfUse("Commercial"); assertEquals("Honda", vehicle.getBrand()); assertEquals("Blue", vehicle.getColour()); assertEquals("Commercial", vehicle.getPurposeOfUse()); }}
```

## CodiumAI features

CodiumAI:

- [AlphaCodium](https://www.codium.ai/blog/alphacodium-state-of-the-art-code-generation-for-code-contests/): a test-based, multi-stage, code-oriented iterative flow that improves the performance of LLMs on code problems.

- Supports any language, even though advanced features are available in Python, JavaScript, TypeScript, and Java.

- Allows you to spend more time writing core features instead of tests.

- Helps you create bug-free software by thoroughly analyzing your code for edge cases.

To start using CodiumAI, refer to the [CodiumAI website](https://www.codium.ai/).

Further reading:

- [AlphaCodium](https://www.codium.ai/blog/alphacodium-state-of-the-art-code-generation-for-code-contests/): State-of-the-art generation code.

- [IntegrityAgent](https://www.codium.ai/products/ide-plugin/): Build Fast with Confidence using CodiumAI.

- CodiumAI [Developer's Hub](https://www.codium.ai/developers-hub/).