Система автоматического тестирования и непрерывной интеграции Weblate

Тестами покрыта большая часть современного кода. Увеличьте это покрытия, добавляя новые тесты при добавлении какой-либо новой функциональности, дабы проверить, что она работает корректно.

Непрерывная интеграция

Weblate relies on GitHub Actions to run tests, build documentation, code linting, and other tasks to ensure code quality.

Codecov collects the code coverage information from the tests that were run.

Есть несколько задач, которые проверяют разные аспекты:

  • Unit and functional tests using pytest.

  • Documentation build and external links using Sphinx.

  • Code linting and quality assurance using ruff and pylint.

  • Code security scanning using CodeQL.

  • Code formatting using pre-commit.

  • Проверка миграции со всех поддерживаемых релизов

  • Проверка установки (гарантирует, что в сгенерированных файлах всё есть и они могут быть протестированы)

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

Local testing of Weblate

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 (смотреть раздел Непрерывная интеграция), его параметры можно поменять через переменные среды:

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-контейнера для разработки, смотреть раздел Запуск Weblate локально внутри Doсker.

См. также

Смотрите подробности о том, как писать и запускать тесты в документации Django: Testing in Django.

Local testing of Weblate modules

Тесты выполняются с помощью py.test. Сначала нужно установить тестовые требования:

uv pip install -e '.[dev]'

Затем вы можете выполнить набор тестов при проверке репозитория:

py.test

Testing repository

Many of the tests in the Weblate test suite use the test repository. The test suite repository is maintained at https://github.com/WeblateOrg/test. The script scripts/pack-test-data.sh is then used to generate a tarball with a repository for each of the supported version control systems. These are stored as weblate/trans/tests/data/test-base-repo.git.tar, weblate/trans/tests/data/test-base-repo.hg.tar, and weblate/trans/tests/data/test-base-repo.svn.tar in the Weblate repository.

The https://github.com/WeblateOrg/test repository is tagged at the release time, which ensures that the release tags can be used to access test data used at the release time. The script tries to create reproducible tarballs as much as possible.