DEV Community

Cover image for Streamline Test Writing with QodoAI
Karlgusta Esimit
Karlgusta Esimit

Posted on

Streamline Test Writing with QodoAI

Today we are going to write a test with Qodo AI.

Qodo AI(formerly CodiumAI) is an AI code generation tool that empowers developers to build high-quality software with confidence, precision, and ease.

It is a great alternative to GitHub Copilot.

It allows you to generate a comprehensive test suite for various types of code, including classes, functions, and code snippets. It assists you by automating the test creation process, saving time and effort.

Whether you need to test a specific class, a function, or a small section of code, Qodo offers the functionality to generate meaningful test cases that can ensure the quality and reliability of your code.

Here is the code that we will write tests for:

function factorial(n) {
  if (!Number.isInteger(n)) {
    throw new TypeError("Input must be an integer");
  }
  if (n < 0) {
    throw new RangeError("Input must be non-negative");
  }
  if (n === 0 || n === 1) {
    return 1;
  }
  return n * factorial(n - 1);
}
Enter fullscreen mode Exit fullscreen mode

It is a JavaScript code that calculates factorials.

For those that have forgotten what factorials is, don't worry. I'll give you a refresher.

A factorial is a number multiplied by every number between itself and 1. For example that factorial of 4 is 24. Meaning, 4! = 4 * 3 * 2 * 1 = 24.

Let's test this code.

Testing the Code

Let's start with creating a simple project called Factorials.

Image description

Open it with VS Code.

Create a JavaScript file called factorial.js and add the code above.

Image description

Now, to test the code using Qodo, assuming you have installed the extension.

If not, you can go to VS Code extensions tab and search for Qodo AI. It has more than 450k+ installations.

Image description

After installing, let's go back to the code and test it.

Image description

Click on the button that says test this function.

Image description

Qodo opens on the left and generates the tests that we would like to run on this function.

Image description

Let's test one and see if it works correctly.

The test returns 1 for input 0 is OK since in our code we have that.

Here is the test.

    // returns 1 for input 0
    it('should return 1 when input is 0', () => {
      expect(factorial(0)).toBe(1);
    });
Enter fullscreen mode Exit fullscreen mode

Image description

Let's see if the test will pass.

Image description

Click the run button to run it.

Image description

It passed.

Image description

Well done!

With Qodo, testing is so easy.

See you in the next one!

Karl

Top comments (0)