Skip to main content
Version: 2.0.0

Keploy Unit Test Generator

Keploy's unit test generator(ut-gen) implementation of Meta LLM research paper is a first which understands code semantics and generates meaningful unit tests, aiming to:

  • Automate unit test generation (UTG): Quickly generate comprehensive unit tests and reduce the redundant manual effort.

  • Improve edge cases: Extend and improve the scope of tests to cover more complex scenarios that are often missed manually.

  • Boost test coverage: As codebase grows, ensuring exhaustive coverage should become feasible.

Usage

keploy gen [flag]

Prerequisites

API KEY of the AI model is needed, this can be from either of one these:

Now, let's setup the API_KEY as environment variable : -

export API_KEY=xxxx

Running with Javascript/TypeScript applications

For the we need to make sure that the coverage report is in coberuta format, so to make sure let's modify our package.json by adding :

"jest": {
"coverageReporters": ["text", "cobertura"],
}

or if jest.config.js is present then, we need to add :

module.exports = {
coverageReporters: ["text", "cobertura"],
};

Generating Unit Tests

You can test a smaller section of application or to control costs, we can consider generating tests for a single source and its corresponding test file : -

keploy gen --source-file-path="<path to source file>" \
--test-file-path="<path to test file for above source file>" \
--test-command="npm test" \
--coverage-report-path="<path to coverage.xml>"

For Entire Application we can generate tests by using --test-dir instead of --test-file-path.

⚠️ Warning: Executing command with --test-dir will generate unit tests for all files in the application. Depending on the size of the codebase, this process may take between 20 minutes to an hour and will incur costs related to LLM usage.

Example

Let us consider the express-mongoose sample-application, where we have a jest testcases under test folder with name routes.test.js.

We have modified our package.json by adding below : -

  "jest": {
"collectCoverage": true,
"coverageReporters": ["text", "cobertura"],
"coverageDirectory": "./coverage"
}

Now let's run Keploy UTG command : -

keploy gen \
--source-file-path="./src/routes/routes.js" \
--test-file-path="./test/routes.test.js" \
--test-command="npm test" \
--coverage-report-path="./coverage/cobertura-coverage.xml"

We will get following output : -

Keploy test coverage with ai generated unit tests for express-mongoose

Voila!! The Generated Testcases have provided with 58% coverage🌟

Running with Golang applications

To ensure Cobertura formatted coverage reports, we need to install the following : -

go install github.com/axw/gocov/gocov@v1.1.0
go install github.com/AlekSi/gocov-xml@v1.1.0

Generating Unit Tests

With the above dependecies installed, we can now generate tests for our application by the following dommand : -

keploy gen --source-file-path="<path to source file>o" \
--test-file-path="<path to test file for above source file>" \
--test-command="go test -v ./... -coverprofile=coverage.out && gocov convert coverage.out | gocov-xml > coverage.xml" \
--coverage-report-path="<path to coverage.xml>"

For Entire Application we can generate tests by using --test-dir instead of --test-file-path.

⚠️ Warning: Executing command with --test-dir will generate unit tests for all files in the application. Depending on the size of the codebase, this process may take between 20 minutes to an hour and will incur costs related to LLM usage.

Example

Let us consider the mux-sql sample-application, where we already have our app_test.go test file for app.go source file : -

keploy gen --source-file-path="app.go" \
--test-file-path="app_test.go" \
--test-command="go test -v ./... -coverprofile=coverage.out && gocov convert coverage.out | gocov-xml > coverage.xml" \
--coverage-report-path="./coverage.xml"

We will get following output : -

Keploy test coverage with ai generated unit test for mux-sql

Voila!! The Generated Testcases have provided with 71% coverage in just 2 iterations 🌟