Project Icon

testpilot

基于 LLM 的 JavaScript/TypeScript 单元测试生成工具

TestPilot 是一个开源项目,利用大型语言模型为 JavaScript/TypeScript npm 包自动生成单元测试。该工具通过向 LLM 提供函数信息来生成测试骨架,并将结果转换为可执行的单元测试。TestPilot 无需额外训练或强化学习,为测试生成领域提供了新的研究方向。目前主要用于学术研究和技术探索,而非日常开发使用。

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.

项目侧边栏1项目侧边栏2
推荐项目
Project Cover

豆包MarsCode

豆包 MarsCode 是一款革命性的编程助手,通过AI技术提供代码补全、单测生成、代码解释和智能问答等功能,支持100+编程语言,与主流编辑器无缝集成,显著提升开发效率和代码质量。

Project Cover

AI写歌

Suno AI是一个革命性的AI音乐创作平台,能在短短30秒内帮助用户创作出一首完整的歌曲。无论是寻找创作灵感还是需要快速制作音乐,Suno AI都是音乐爱好者和专业人士的理想选择。

Project Cover

有言AI

有言平台提供一站式AIGC视频创作解决方案,通过智能技术简化视频制作流程。无论是企业宣传还是个人分享,有言都能帮助用户快速、轻松地制作出专业级别的视频内容。

Project Cover

Kimi

Kimi AI助手提供多语言对话支持,能够阅读和理解用户上传的文件内容,解析网页信息,并结合搜索结果为用户提供详尽的答案。无论是日常咨询还是专业问题,Kimi都能以友好、专业的方式提供帮助。

Project Cover

阿里绘蛙

绘蛙是阿里巴巴集团推出的革命性AI电商营销平台。利用尖端人工智能技术,为商家提供一键生成商品图和营销文案的服务,显著提升内容创作效率和营销效果。适用于淘宝、天猫等电商平台,让商品第一时间被种草。

Project Cover

吐司

探索Tensor.Art平台的独特AI模型,免费访问各种图像生成与AI训练工具,从Stable Diffusion等基础模型开始,轻松实现创新图像生成。体验前沿的AI技术,推动个人和企业的创新发展。

Project Cover

SubCat字幕猫

SubCat字幕猫APP是一款创新的视频播放器,它将改变您观看视频的方式!SubCat结合了先进的人工智能技术,为您提供即时视频字幕翻译,无论是本地视频还是网络流媒体,让您轻松享受各种语言的内容。

Project Cover

美间AI

美间AI创意设计平台,利用前沿AI技术,为设计师和营销人员提供一站式设计解决方案。从智能海报到3D效果图,再到文案生成,美间让创意设计更简单、更高效。

Project Cover

稿定AI

稿定设计 是一个多功能的在线设计和创意平台,提供广泛的设计工具和资源,以满足不同用户的需求。从专业的图形设计师到普通用户,无论是进行图片处理、智能抠图、H5页面制作还是视频剪辑,稿定设计都能提供简单、高效的解决方案。该平台以其用户友好的界面和强大的功能集合,帮助用户轻松实现创意设计。

投诉举报邮箱: service@vectorlightyear.com
@2024 懂AI·鲁ICP备2024100362号-6·鲁公网安备37021002001498号