Cover-Agent是一款基于AI的自动化测试生成工具,旨在提高代码覆盖率。它通过生成高质量的测试用例来优化现有的测试套件,简化了软件开发中的测试流程。此工具包括测试执行器、覆盖率解析器、提示构建器和AI调用器等多个组件,支持通过命令行运行,并计划与流行的持续集成平台集成。Cover-Agent鼓励社区合作,以持续推动自动化单元测试生成领域的创新。
此工具是一系列旨在自动化软件项目单元测试创建的实用工具的一部分。利用先进的生成式人工智能模型,旨在简化和加速测试过程,确保高质量的软件开发。
该系统包括几个组成部分:
1. 测试运行器:执行命令或脚本以运行测试套件并生成代码覆盖率报告。
2. 覆盖率解析器:验证随着测试的添加代码覆盖率是否提高,确保新测试有助于整体测试效果。
3. 提示构建器:从代码库收集必要数据,并构建传递给大型语言模型(LLM)的提示。
4. AI 调用器:与 LLM 互动,基于提供的提示生成测试。
1.要求
在开始之前,请确保您具备以下条件:
·环境变量中设置了 OPENAI_API_KEY,该变量是调用 OpenAI API 所必需的。
·代码覆盖工具:需要一个 Cobertura XML 代码覆盖率报告,以便工具正确运行。例如,在 Python 中可以使用 pytest-cov。运行 Pytest 时添加 --cov-report=xml 选项。
如果直接从仓库运行,你还需要:
·系统上安装了 Python。
·安装 Poetry 以管理 Python 包依赖。可以在 https://python-poetry.org/docs/ 找到 Poetry 的安装说明。
2.独立运行时
Cover Agent 可以作为 Python Pip 包安装,或作为独立可执行文件运行。
·Python Pip
要通过 GitHub 直接安装 Python Pip 包,请运行以下命令:
pipinstallgit+https://github.com/Codium-ai/cover-agent.git
·二进制文件
该二进制文件可以在您的系统上不安装任何 Python 环境的情况下运行(例如,在不包含 Python 的 Docker 容器中运行)。您可以通过导航到项目的发布页面为您的系统下载发布版本。
3.仓库设置
运行以下命令以安装所有依赖并从源代码运行项目:
4.运行代码
下载可执行文件或安装 Pip 包后,你可以运行 Cover Agent 以生成和验证单元测试。使用以下命令从命令行执行它:
cover-agent\--source-file-path"<path_to_source_file>"\--test-file-path"<path_to_test_file>"\--code-coverage-report-path"<path_to_coverage_report>"\--test-command"<test_command_to_run>"\--test-command-dir"<directory_to_run_test_command>"\--coverage-type"<type_of_coverage_report>"\--desired-coverage<desired_coverage_between_0_and_100>\--max-iterations<max_number_of_llm_iterations>\--included-files"<optional_list_of_files_to_include>"
你可以使用此仓库中的示例项目作为测试运行此代码。
按照位于 templated_tests/python_fastapi/ 目录中的 README.md 文件中的步骤操作,然后返回到仓库的根目录,并运行以下命令将测试添加到 python fastapi 示例:
cover-agent\--source-file-path"templated_tests/python_fastapi/app.py"\--test-file-path"templated_tests/python_fastapi/test_app.py"\--code-coverage-report-path"templated_tests/python_fastapi/coverage.xml"\--test-command"pytest--cov=.--cov-report=xml--cov-report=term"\--test-command-dir"templated_tests/python_fastapi"\--coverage-type"cobertura"\--desired-coverage70\--max-iterations10
对于使用 go 的示例,进入 templated_tests/go_webservice 目录,按照 README.md 设置项目。要使用覆盖率报告,您需要安装 gocov 和 gocov-xml。运行以下命令安装这些工具:
goinstallgithub.com/axw/gocov/gocov@v1.1.0goinstallgithub.com/AlekSi/gocov-xml@v1.1.0
cover-agent\--source-file-path"app.go"\--test-file-path"app_test.go"\--code-coverage-report-path"coverage.xml"\--test-command"gotest-coverprofile=coverage.out&&gocovconvertcoverage.out|gocov-xml>coverage.xml"\--test-command-dir$(pwd)\--coverage-type"cobertura"\--desired-coverage70\--max-iterations1
尝试通过在此仓库的根目录运行此命令为该项目添加更多测试:
poetryruncover-agent\--source-file-path"cover_agent/main.py"\--test-file-path"tests/test_main.py"\--code-coverage-report-path"coverage.xml"\--test-command"poetryrunpytest--junitxml=testLog.xml--cov=templated_tests--cov=cover_agent--cov-report=xml--cov-report=term--log-cli-level=INFO"\--coverage-type"cobertura"\--desired-coverage70\--max-iterations1\--model"gpt-4o"
注意:如果您使用 Poetry,则使用 poetry run cover-agent 命令代替 cover-agent run 命令。
5.输出
一些调试文件将在仓库内本地输出(这些文件是 .gitignore 的一部分)
·generated_prompt.md:发送给 LLM 的完整提示
·run.log:输出到您的 stdout 的日志副本
·test_results.html:包含以下内容的结果表,针对每个生成的测试:
①测试状态
②失败原因(如果适用)
③退出代码
④标准错误输出
⑤标准输出
⑥生成的测试
6.使用其他 LLM
该项目使用 LiteLLM 与 OpenAI 和其他托管的 LLM(到目前为止支持 100 多个 LLM)进行通信。要使用 OpenAI 默认之外的不同模型,您需要:
·按照 LiteLLM 说明导出支持的 LLM 需要的任何环境变量。
·在调用 Cover Agent 时使用 --model 选项来调用模型名称。
例如(如在 LiteLLM 快速启动指南中找到的):
export VERTEX_PROJECT="hardy-project"export VERTEX_LOCATION="us-west"
cover-agent \...--model "vertex_ai/gemini-pro"
通过运行上面提到的 poetry install 命令来设置您的开发环境。
注意:对于旧版本的 Poetry,您可能需要包括 --dev 选项以安装开发依赖。
设置环境后运行以下命令:
printf("helloworld!");poetryrunpytest--junitxml=testLog.xml--cov=templated_tests--cov=cover_agent--cov-report=xml--cov-report=term--log-cli-level=INFO
这也将生成在 .github/workflows/ci_pipeline.yml 中生成的所有日志和输出报告。
https://github.com/Codium-ai/cover-agent