計畫簡介
Cover-Agent是一款基於AI的自動化測試生成工具,旨在提高程式碼覆蓋率。它透過生成高品質的測試用例來最佳化現有的測試套件,簡化了軟體開發中的測試流程。此工具包括測試執行器、覆蓋率解析器、提示構建器和AI呼叫器等多個元件,支持透過命令列執行,並計劃與流行的持續整合平台整合。Cover-Agent鼓勵社群合作,以持續推動自動化單元測試生成領域的創新。
掃碼加入AI交流群
獲得更多技術支持和交流
(請註明自己的職業)
概述
此工具是一系列旨在自動化軟體計畫單元測試建立的實用工具的一部份。利用先進的生成式人工智慧模型,旨在簡化和加速測試過程,確保高品質的軟體開發。
該系統包括幾個組成部份:
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 包,請執行以下命令:
pip install git+https://github.com/Codium-ai/cover-agent.git
·二進制檔
該二進制檔可以在您的系統上不安裝任何 Python 環境的情況下執行(例如,在不包含 Python 的 Docker 容器中執行)。您可以透過導航到計畫的釋出頁面為您的系統下載釋出版本。
3.倉庫設定
執行以下命令以安裝所有依賴並從原始碼執行計畫:
poetry install
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-coverage 70 \
--max-iterations 10
對於使用 go 的範例,進入 templated_tests/go_webservice 目錄,按照 README.md 設定計畫。要使用覆蓋率報告,您需要安裝 gocov 和 gocov-xml。執行以下命令安裝這些工具:
go install github.com/axw/gocov/gocov@v1.1.0
go install github.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 "go test -coverprofile=coverage.out && gocov convert coverage.out | gocov-xml > coverage.xml" \
--test-command-dir $(pwd) \
--coverage-type "cobertura" \
--desired-coverage 70 \
--max-iterations 1
嘗試透過在此倉庫的根目錄執行此命令為該計畫添加更多測試:
poetry run cover-agent \
--source-file-path "cover_agent/main.py" \
--test-file-path "tests/test_main.py" \
--code-coverage-report-path "coverage.xml" \
--test-command "poetry run pytest --junitxml=testLog.xml --cov=templated_tests --cov=cover_agent --cov-report=xml --cov-report=term --log-cli-level=INFO" \
--coverage-type "cobertura" \
--desired-coverage 70 \
--max-iterations 1 \
--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 選項以安裝開發依賴。
設定環境後執行以下命令:
print f("hello world!"); poetry run pytest --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
關註「 開源AI計畫落地 」公眾號
與AI時代更靠近一點