Weblate 測試套件與連續整合

測試套件存在於多數目前代碼,通過為任何新的功能新增測試情況來擴大覆蓋範圍,並確認其正常工作。

連續整合

Current test results can be found on GitHub Actions and coverage is reported on Codecov.

有幾項工作來確認不同的方面:

  • 單元測試

  • 文件建置與外部連結

  • 來自所有支援的發佈版本的合併測試

  • 代碼整理

  • 設定確認(確保產生的 dist 文件不遺失任何內容,並可以測試)

The configuration for the CI is in .github/workflows directory. It heavily uses helper scripts stored in ci directory. The scripts can be also executed manually, but they require several environment variables, mostly defining Django settings file to use and test database connection. The example definition of that is in scripts/test-database.sh:

# 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 backend to use postgresql / mysql / mariadb
export CI_DATABASE="${1:-postgresql}"

# 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

本地測試

Before running test, please ensure test dependencies are installed. This can be done by pip install -e .[test].

Testing using pytest

執行測試前,應該收集靜態文件,因為一些測試在出現時相依於它們:

DJANGO_SETTINGS_MODULE=weblate.settings_test ./manage.py collectstatic

You can use pytest to run a testsuite locally:

pytest weblate

Running an individual test file:

pytest weblate/utils/tests/test_search.py

提示

您會需要使用資料庫(PostgreSQL)伺服器來檢測。 Django 預設新建另外的資料庫,以 test_ 前綴來執行測試,因此在您的設定組態使用 weblate 的情況下,測試會使用 test_weblate 資料庫。設定的指示請參見 Weblate 的資料庫設定

weblate/settings_test.py 也用在 CI 環境中(請參見 連續整合),並且可以使用環境變量調整:

export CI_DATABASE=postgresql
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