What is Codeium and How to Use it for Test Case Writing

In this step-by-step article, we are going to learn about Codeium. This article covers the following topics.

  • What is Codeium?
  • Features
  • How do you install the Codeium extension into Visual Studio?
  • How do you use Codeium to write test cases?
  • Conclusion

Prerequisites

  • Visual Studio
  • Internet Connection
  • Basic understanding of xUnit with C#

What is Codeium?

"The Modern Coding Superpower"

Codeium is an AI tool that helps developers write code faster and more easily. It offers features like smart

code suggestions (autocomplete), a built-in chat for help, and quick edits right in your code.

Think of it as a helpful assistant that makes coding smoother and more efficient.

Features

  • AI-Powered Autocomplete: Streamline your coding with smart, real-time suggestions that adapt to your code context.
  • Code Chat Integration: Get instant feedback or assistance through chat, designed to help you solve problems faster.
  • In-line Edits: Easily make changes with command-based in-line edits, making refactoring and adjustments more efficient.
  • Multi-language Support: Works across various programming languages, so you can rely on it no matter the tech stack.
  • Customizable Workflows: Adjust settings to fit your coding style and speed up your development process.
  • Seamless IDE Integration: Plug Codeium into your favorite IDE for a smooth, hassle-free experience.

How do I install the Codeium extension in Visual Studio?

Following are the steps to install the Codeium extension in Visual Studio.

Step 1. Open Visual Studio go to the Extensions menu, select Manage Extensions, and search Codeium as below.

Manage Extensions

Step 2. Click on the download button. It will download and install the extension. Restart the visual studio to complete the installation process; it will show as below once completed:

Download

Step 3. Open the visual studio, it will show you a notification for sign-in to Codeium click on Sign-in.

Visual studio

Here, it will open the browser and you need to sign in with Google or sign up with your email address as per your preference.

Browser

Once you complete the sign-in with Google or the sign-up process, you will get the welcome screen.

Codeium

Next, go to Visual Studio, and you can see the Codeium an extension added in the Extension menu as below.

Menu

How do you use Codeium to write xUnit test cases for you?

In the previous section, we have seen how to install the Codeium in Visual Studio and we are all set to use it. Let's see how we can use the

Codeium to write test cases for your methods by following the below steps.

Step 1. Create an xUnit test case project in Visual Studio and add the Calculator.cs file and methods in it as below.

namespace CodeiumTestProject
{
    public class Calculator
    {
        public int Add(int a, int b)
        {
            return a + b;
        }
        public int Subtract(int a, int b)
        {
            return a - b;
        }
        public int Multiply(int a, int b)
        {
            return a * b;
        }
        public double Divide(int a, int b)
        {
            if (b == 0)
            {
                throw new DivideByZeroException("Cannot divide by zero");
            }
            return (double)a / b;
        }
    }
}

Step 2. Open the Calculator.cs file, and you will see Codeium annotations above the class name and method names.

 Calculator.cs

It will give you various options once you click on it, that is Refactor, Explain, and Generate Docstring. Select the Explain option, and it will open.

The Codeium Chat toolbox with an explanation of the Add method as below.

Add method

Select the Generate Docstring option, and it will open the Codeium Chat toolbox with the generated Docstring of the Add method with the option you can.

Manually copy and paste or click on Insert as below.

Insert

Step 3. Next, right-click on the Add method and you will see the menu below with Codeium options.

Codeium options

Step 4. Select the option Codeium: Generate Unit Test for Function, and it will analyze your code and open the Codeium Chat toolbox.

Generate test cases based on your logic with various scenarios as below.

Various scenarios

Step 5. Create a test file named CalculatorTest.cs then copy and paste the generated test cases using Codeium in the file as below.

namespace CodeiumTestProject
{
    public class CalculatorTest
    {
        [Fact]
        public void Add_ReturnsSumOfTwoNumbers()
        {
            // Arrange
            Calculator calculator = new Calculator();

            // Act
            int result = calculator.Add(2, 3);

            // Assert
            Assert.Equal(5, result);
        }
        [Fact]
        public void Add_ReturnsZeroForZeroInputs()
        {
            // Arrange
            Calculator calculator = new Calculator();

            // Act
            int result = calculator.Add(0, 0);

            // Assert
            Assert.Equal(0, result);
        }
        [Fact]
        public void Add_ReturnsPositiveNumberForPositiveInputs()
        {
            // Arrange
            Calculator calculator = new Calculator();

            // Act
            int result = calculator.Add(2, 3);

            // Assert
            Assert.True(result > 0);
        }
        [Fact]
        public void Add_ReturnsNegativeNumberForNegativeInputs()
        {
            // Arrange
            Calculator calculator = new Calculator();
            // Act
            int result = calculator.Add(-2, -3);
            // Assert
            Assert.True(result < 0);
        }
    }
}

Step 6. Finally, go to the Test Explorer and run the tests generated by Codeium as below.

Test Explorer

Conclusion

In this article, we learned about Codeium, its features, and installing, and generating unit test cases for your method. If you have any suggestions or queries regarding this article, please contact me.

“Learn It, Share it.”