Weblate testsuite i integració contínua

Existeixen conjunts de proves per a la major part del codi actual, augmenten la cobertura afegint casos de prova per a qualsevol funcionalitat nova i verifiquen que funcioni.

Integració contínua

Weblate es basa en Accions de GitHub per executar proves, crear documentació, lingatge de codi i altres tasques per garantir la qualitat del codi.

Codecov recull la informació de cobertura del codi de les proves que es van executar.

Hi ha diverses feines per verificar diferents aspectes:

  • Proves unitàries i funcionals amb pytest.

  • Creació de documentació i enllaços externs mitjançant Sphinx.

  • Codi linting i garantia de qualitat utilitzant ruff i pylint.

  • Escaneig de seguretat de codi mitjançant CodeQL.

  • Les proves de canvis visuals fan servir Argos CI.

  • Code formatting using prek, a faster third-party reimplementation of the pre-commit framework.

  • Proves de migració de totes les versions compatibles

  • Verificació de la configuració (assegura que els fitxers dist generats no es perdin res i es poden provar)

La configuració del CI es troba al directori .github/workflows. Utilitza molt scripts d’ajuda emmagatzemats al directori ci. Els scripts també es poden executar manualment, però requereixen diverses variables d’entorn, la majoria definint el fitxer de configuració de Django per utilitzar-lo i provar la connexió de la base de dades. L’exemple de definició d’això es troba a scripts/test-database.sh:

The Selenium screenshot tests in weblate/trans/tests/test_selenium.py serve two purposes. They generate images for visual change testing in CI, and the same images are converted into documentation screenshots by make -C docs update-screenshots. Keep screenshot fixtures close to real rendered pages so CI catches UI regressions. When a screenshot includes volatile runtime data, prefer deterministic server-side test inputs over post-render DOM changes.

GitHub repository URLs in these screenshots use local Git transport rewrites. The shared Git fixture archive contains the ordinary test data and a separate, pinned demo revision, so repository discovery shows only demo content. Regenerate the archive with scripts/pack-test-data.sh; update its DEMO_REVISION when intentionally refreshing the demo data. Runtime tests do not fetch GitHub repositories or resolve their hostname. The TEST_REVISION pin controls the ordinary Git, Mercurial, and Subversion data independently.

Selenium tests that consume the standard demo can opt in with @reuse_demo_fixture from weblate/trans/tests/selenium_fixtures.py. Place it outside other method decorators. It restores committed database rows and the demo’s working repositories before the test body, so call create_component() before creating additional components as usual. Create users and other test-specific data inside the test body; restoration replaces the database. GitHub transports and browser state remain isolated per test. Keep creation and empty-state tests undecorated so they continue to exercise the browser forms and generate their screenshots.

# 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

L’execució simple pot semblar:

source scripts/test-database.sh
./ci/run-migrate
./ci/run-test
./ci/run-docs

OSS Scanner verification

The development Dockerfile shares its native dependencies and tooling between the development and scanner targets. VS Code, agent verification, and rundev.sh select development and retain mounted checkouts and separate Compose services. The final scanner target includes the checkout, locked Python dependencies, compiled assets, and local PostgreSQL and Valkey for verification without Internet access.

Build it from the repository root and run its offline smoke test:

docker build -f dev-docker/weblate-dev/Dockerfile -t weblate-oss-scanner .
docker run --rm --init --network none --user 0 --workdir /src --entrypoint bash \
    weblate-oss-scanner .oss-scanner/smoke

To run the complete backend suite, replace .oss-scanner/smoke with .oss-scanner/test. Append pytest selections or -n 0 for focused tests. No host mounts or external service containers are needed. The .github/workflows/rundev.yml workflow runs the focused smoke test on amd64, alongside the existing development-container checks.

The scanner uses Weblate threat model. The permissive test settings and local service authentication are test fixtures, not production deployment defaults.

Local testing of 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.

Abans d’executar proves, assegureu-vos que les dependències de desenvolupament estan instal·lades:

uv sync --all-extras --dev

Prova amb pytest

Abans d’executar proves, hauríeu de recollir fitxers estàtics, ja que algunes proves depenen de la seva presència:

DJANGO_SETTINGS_MODULE=weblate.settings_test uv run ./manage.py collectstatic --noinput

Podeu utilitzar pytest per executar la suite de proves localment:

uv run pytest

Property-based tests using hypothesis run as part of the normal pytest suite. They check Markdown output safety, backup archive member paths, and SSH URL parsing with bounded generated inputs. Each property runs up to 100 generated examples deterministically, alongside explicit regression examples. Unexpected exceptions fail the test, and Hypothesis minimizes failing inputs. Add minimized inputs as explicit regression examples when fixing a failure. Timing deadlines are disabled to avoid failures caused by CI load.

Execució d’un fitxer de prova individual:

uv run pytest weblate/utils/tests/test_search.py

Suggeriment

Necessitareu un servidor de base de dades (PostgreSQL) per utilitzar-lo per a les proves. Per defecte, Django crea una base de dades separada per executar proves amb el prefix test_, de manera que en cas que la vostra configuració estigui configurada per utilitzar weblate, les proves utilitzaran la base de dades test_weblate. Vegeu Configuració de la base de dades per a Weblate per obtenir instruccions de configuració.

El weblate/settings_test.py també s’utilitza a l’entorn CI (vegeu Integració contínua) i es pot ajustar mitjançant variables d’entorn:

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

Suggeriment

Les proves també es poden executar dins del contenidor docker del desenvolupador, vegeu Execució de Weblate localment a Docker.

Vegeu també

Vegeu Testing in Django per obtenir més informació sobre com executar i escriure proves per a Django.

Tests that repeatedly import the same component can inherit from ReusableComponentTestCase or ReusableViewTestCase in weblate/trans/tests/test_views.py. These bases build the initial user, project, component, and translations once per class in setUpTestData(). Override create_component() to select a repository fixture or file format, as with ComponentTestCase. For additional shared setup, override the build_fixture(cls, builder) class method, call super().build_fixture(builder), and use the builder’s repository helpers and model instances.

Each test reloads its model instances and receives a restored copy of the working repositories, upstream repositories used during construction, and test Git configuration. Database changes are rolled back by Django. Clients, authentication, and application caches are initialized per test. Snapshots are temporary and local to each class and worker; extra components created within a test still use the normal component factories.

API tests inheriting APIBaseTest in weblate/api/tests.py use the same repository restoration. The API user and initial component, including its automatic glossary, are created once per class. Component construction callbacks run during this shared setup. API clients, credentials, and subclass setup such as screenshots and announcements are initialized per test.

Use the existing bases for TransactionTestCase and concurrency tests, or when component construction itself must run under a method-specific settings override. The reusable bases do not replay component construction signals or commit callbacks before each test.

Local testing of Weblate modules

Les proves s’executen mitjançant pytest. Primer cal instal·lar dependències de desenvolupament:

uv sync --all-extras --dev

A continuació, podeu executar el testsuite a la comprovació del repositori:

uv run pytest

Testing repository

Moltes de les proves de la suite de proves Weblate utilitzen el dipòsit de proves. El dipòsit de la suite de proves es manté a https://github.com/WeblateOrg/test. L’script scripts/pack-test-data.sh s’utilitza llavors per generar un fitxer tarball amb un repositori per a cadascun dels sistemes de control de versions compatibles. S’emmagatzemen com a weblate/trans/tests/data/test-base-repo.git.tar, weblate/trans/tests/data/test-base-repo.hg.tar i weblate/trans/tests/data/test-base-repo.svn.tar al repositori de Weblate.

El dipòsit https://github.com/WeblateOrg/test està etiquetat en el moment de la publicació, cosa que garanteix que les etiquetes de llançament es puguin utilitzar per accedir a les dades de prova utilitzades en el moment de la publicació. L’script intenta crear tarballs reproduïbles tant com sigui possible.