• Stars
    star
    495
  • Rank 88,974 (Top 2 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created about 1 year ago
  • Updated 2 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Test generation using large language models

TestPilot

TestPilot is a tool for automatically generating unit tests for npm packages written in JavaScript/TypeScript using a large language model (LLM).

Note that TestPilot represents an early exploration in the use of LLMs for test generation, and has been made available in open source as a basis for research and exploration. For day-to-day use the test generation features in Copilot Chat are likely to yield better results.

Background

TestPilot generates tests for a given function f by prompting the LLM with a skeleton of a test for f, including information about f embedded in code comments, such as its signature, the body of f, and examples usages of f automatically mined from project documentation. The model's response is then parsed and translated into a runnable unit test. Optionally, the test is run and if it fails the model is prompted again with additional information about the failed test, giving it a chance to refine the test.

Unlike other systems for LLM-based test generation, TestPilot does not require any additional training or reinforcement learning, and no examples of functions and their associated tests are needed.

A research paper describing TestPilot in detail is available on arXiv and IEEExplore.

Requirements

In general, to be able to run TestPilot you need access to a Codex-style LLM with completion API. Set the TESTPILOT_LLM_API_ENDPOINT environment variable to the URL of the LLM API endpoint you want to use, and TESTPILOT_LLM_AUTH_HEADERS to a JSON object containing the headers you need to authenticate with the API.

Typical values for these variables might be:

  • TESTPILOT_LLM_API_ENDPOINT='https://api.openai.com/v1/engines/code-cushman-001/completions'
  • TESTPILOT_LLM_AUTH_HEADERS='{"Authorization": "Bearer <your API key>", "OpenAI-Organization": "<your organization ID>"}'

Note, however, that you can run TestPilot in reproduction mode without access to the LLM API where model responses are taken from the output of a previous run; see below for details.

Installation

You can install TestPilot from a pre-built package or from source.

Installing from a pre-built package

TestPilot is a available as a pre-built npm package, though it is not currently published to the npm registry. You can download a tarball from the repository and install it in the usual way. Note that this distribution only contains the core part of TestPilot, not the benchmarking harness.

Installing from source

The src/ directory contains the source code for TestPilot, which is written in TypeScript and gets compiled into the dist/ directory. Tests are in test/; the benchmark/ directory contains a benchmarking harness for running TestPilot on multiple npm packages; and ql/ contains the CodeQL queries used to analyze the results.

In the root directory of a checkout of this repository, run npm build to install dependencies and build the package.

You can also use npm run build:watch to automatically build anytime you make changes to the code. Note, however, that this will not automatically install dependencies, and also will not build the benchmarking harness.

Use npm run test to run the tests. For convenience, this will also install dependencies and run a build.

Benchmarking

If you install TestPilot from source, you can use the benchmarking harness to run TestPilot on multiple packages and analyze the results. This is not currently available if you install TestPilot from a pre-built package.

Running locally

Basic usage is as follows:

node benchmark/run.js --outputDir <report_dir> --package <package_dir>

This generates tests for all functions exported by the package in <package_dir>, validates them, and writes the results to <report_dir>.

Note that this assumes that package dependencies are installed and any build steps have been run (e.g., using npm i and npm run build). TestPilot also relies on mocha, so if the package under test does not already depend on it, you must install it separately, for example using the command npm i --no-save mocha.

Running on Actions

The run-experiment.yml workflow runs an experiment on GitHub Actions, producing the final report as an artifact you can download. The results-all artifact contains the results of all packages, while the other artifacts contain the individual results of each package.

Reproducing results

The results of TestPilot are non-deterministic, so even if you run it from the same package on the same machine multiple times, you will get different results. However, the benchmarking harness records enough data to be able to replay a benchmark run in many cases.

To do this, use the --api and --responses options to reuse the API listings and responses from a previous run:

node benchmark/run.js --outputDir <report_dir> --package <package_dir> --api <api.json> --responses <prompts.json>

Note that by default replay will fail if any of the prompts are not found in the responses file. This typically happens if TestPilot is refining failing tests, since in this case the prompt to the model depends on the exact failure message, which can be system-specific (e.g., containing local file-system paths), or depend on the Node.js version or other factors.

To work around these limitations, you can pass the --strictResponses false flag handle treat missing prompts by treating them as getting no response from the model. This will not, in general, produce the same results as the initial run, but suffices in many cases.

Analyzing results

The CodeQL queries in ql/queries can be used to analyze the results of running an experiment. See ql/CodeQL.md for instructions on how to setup CodeQL and run the queries.

License

This project is licensed under the terms of the MIT open source license. Please refer to MIT for the full terms.

Maintainers

  • Max Schaefer (@max-schaefer)
  • Frank Tip (@franktip)
  • Sarah Nadi (@snadi)

Support

TestPilot is a research prototype and is not officially supported. However, if you have questions or feedback, please file an issue and we will do our best to respond.

Acknowledgement

We thank Aryaz Eghbali (@aryaze) for his work on the initial version of TestPilot.

More Repositories

1

monaspace

An innovative superfamily of fonts for code
TypeScript
13,775
star
2

copilot-workspace-user-manual

📖 The user manual for GitHub Copilot Workspace
368
star
3

blocks

A set of files to use as input for Blocks.
333
star
4

gpt4-with-calc

GPT-4 Equipped with Numeric Calculation
TypeScript
171
star
5

vitale

TypeScript notebook for developers
TypeScript
163
star
6

githubnext

A public point of contact for GitHub Next
155
star
7

blocks-template

A template for creating custom Blocks.
TypeScript
112
star
8

blocks-examples

A set of example custom Blocks.
TypeScript
92
star
9

amalfi-artifact

Artifact accompanying our ICSE '22 paper "Practical Automated Detection of Malicious npm Packages"
Python
35
star
10

blocks-dev

TypeScript
18
star
11

universe-blocks-demo

JavaScript
14
star
12

spectrum

TypeScript
13
star
13

blocks-template-svelte

make GitHub Blocks with Svelte!
JavaScript
9
star
14

vscode-react-webviews

A sample/starter template for developing VS Code extensions with webviews
TypeScript
7
star
15

UpgrAIder

Upgrade deprecated/outdated code using LLMs and release notes
Python
7
star
16

llmorpheus

LLM-based mutation testing
TypeScript
5
star
17

tiny-svelte-fsm

A minimalistic finite state machine library for Svelte 5
TypeScript
4
star
18

blocks-template-react

make GitHub Blocks with React!
JavaScript
3
star
19

preview-block

TypeScript
2
star
20

universe-blocks-demo-blocks

CSS
2
star
21

followup-attention

Accompanying artifacts for the TSE paper "Follow-up Attention: An Empirical Study of Developer and Neural Model Code Exploration"
Jupyter Notebook
2
star
22

blocks-template-vue

make GitHub Blocks with Vue!
JavaScript
1
star
23

array-toolbox

This is going to get a thousand stars
JavaScript
1
star
24

blocks-howie-spike-busted

TypeScript
1
star
25

.github

1
star
26

emoji-block

TypeScript
1
star
27

blocks-runtime

runtime support for Blocks
TypeScript
1
star
28

azuresvelte

JavaScript
1
star
29

hello-world

Sample repository
TypeScript
1
star
30

azureswacookies

TypeScript
1
star
31

history-blocks

TypeScript
1
star