Weblate 測試套件與連續整合¶
測試套件存在於多數目前代碼,通過為任何新的功能新增測試情況來擴大覆寫範圍,並確認其正常工作。
連續整合¶
Weblate 仰賴 GitHub Actions 執行測試、建置說明文件、程式碼檢查及其他工作,以確保程式碼品質。
Codecov 從已執行的測試中收集程式碼覆蓋率資訊。
有幾項工作來確認不同的方面:
使用 pytest 進行單元與功能測試的。
使用 Sphinx 建置說明文件並檢查外部連結。
使用 CodeQL 進行程式碼安全掃描。
視覺化變更測試使用 Argos CI 。
使用 prek 格式化程式碼;這是 pre-commit 框架速度更快的第三方重新實作。
來自所有支援的發佈版本的合併測試
設定確認(確保產生的 dist 檔案不遺失任何內容,並可以測試)
CI 設定位於 .github/workflows 目錄,並大量使用 ci 目錄中的輔助指令碼。這些指令碼也可手動執行,但需要數個環境變數,主要用於指定 Django 設定檔及測試資料庫連線。範例定義位於 scripts/test-database.sh:
weblate/trans/tests/test_selenium.py 中的 Selenium 螢幕擷取畫面測試有兩項用途:在 CI 中產生用於視覺變更測試的圖片,並由 make -C docs update-screenshots 將相同圖片轉換成說明檔案的螢幕擷取畫面。螢幕擷取畫面固定資料應盡量接近實際呈現的頁面,讓 CI 能找出使用者介面的回歸問題。若螢幕擷取畫面包含不固定的執行階段資料,請優先使用可重現的伺服器端測試輸入,而非在呈現後變更 DOM。
# Copyright © Michal Čihař <michal@weblate.org>
#
# SPDX-License-Identifier: GPL-3.0-or-later
# Simple way to configure test database from environment
# shellcheck shell=sh
# Database server configuration
export CI_DB_USER=weblate
export CI_DB_PASSWORD=weblate
export CI_DB_HOST=127.0.0.1
# Django settings module to use
export DJANGO_SETTINGS_MODULE=weblate.settings_test
簡單的執行看起來可以像這樣:
source scripts/test-database.sh
./ci/run-migrate
./ci/run-test
./ci/run-docs
Weblate 的本機測試¶
For an isolated environment with dependencies and test services already configured, use the development container. Its CLI can run the same pytest and lint commands documented below in each Git worktree.
執行測試前,請確保開發依賴已安裝:
uv sync --all-extras --dev
使用 pytest 測試¶
執行測試前,應該收集靜態檔案,因為一些測試在出現時依賴於它們:
DJANGO_SETTINGS_MODULE=weblate.settings_test uv run ./manage.py collectstatic --noinput
您可以使用 pytest 在本機執行測試套件:
uv run pytest
執行單一測試檔案:
uv run pytest weblate/utils/tests/test_search.py
提示
測試需要資料庫(PostgreSQL)伺服器。Django 預設會建立名稱以 test_ 為字首的獨立資料庫以執行測試,因此若設定的資料庫名稱為 weblate,測試會使用 test_weblate 資料庫。設定說明請參閱 適用於 Weblate 的資料庫設定。
weblate/settings_test.py 也用在 CI 環境中(請參閱 連續整合),並且可以使用環境變數調整:
export CI_DB_USER=weblate
export CI_DB_PASSWORD=weblate
export CI_DB_HOST=127.0.0.1
export CI_DB_PORT=60000
export DJANGO_SETTINGS_MODULE=weblate.settings_test
提示
測試也可以在開發者 docker 容器內執行,請參閱 在 Docker 中本機執行 Weblate。
也參考
執行及撰寫 Django 測試的詳細資訊,請參閱 Testing in Django。
Weblate 模組的本機測試¶
測試使用 pytest 執行,首先您需要安裝開發相依套件:
uv sync --all-extras --dev
然後您可以在儲存庫簽出中執行測試套件:
uv run pytest
測試儲存庫¶
Weblate 測試套件中的許多測試會使用測試儲存庫。此儲存庫維護於 https://github.com/WeblateOrg/test。接著使用 scripts/pack-test-data.sh 為各個受支援的版本控制系統產生含儲存庫的 tarball,並以 weblate/trans/tests/data/test-base-repo.git.tar、weblate/trans/tests/data/test-base-repo.hg.tar 及 weblate/trans/tests/data/test-base-repo.svn.tar 儲存在 Weblate 儲存庫中。
https://github.com/WeblateOrg/test 儲存庫會在發行時加上標籤,確保可使用發行標籤存取當時使用的測試資料。指令碼會盡可能建立可重現的 tarball。