Weblate testsuite och kontinuerlig integration¶
Testsuiter finns för större delen av den nuvarande koden. Öka täckningen genom att lägga till testfall för alla nya funktioner och verifiera att de fungerar.
Kontinuerlig integration¶
Weblate använder GitHub Actions för att köra tester, skapa dokumentation, kodgranska och utföra andra uppgifter för att säkerställa kodkvaliteten.
Codecov samlar in information om kodtäckning från de tester som har körts.
Det finns flera uppgifter för att verifiera olika aspekter:
Enhets- och funktionstester med hjälp av pytest.
Dokumentation och externa länkar med hjälp av Sphinx.
Kodgranskning och kvalitetssäkring med hjälp av ruff och pylint.
Kodsäkerhetsskanning med hjälp av CodeQL.
Visual changes testing is utilizing Argos CI.
Code formatting using prek, a faster third-party reimplementation of the pre-commit framework.
Migreringstestning från alla versioner som stöds
Verifiering av inställningar (säkerställer att genererade dist-filer inte saknar något och kan testas)
Konfigurationen för CI finns i katalogen .github/workflows. Den använder i stor utsträckning hjälpscript som finns lagrade i katalogen ci. Scripten kan också köras manuellt, men de kräver flera miljövariabler, främst för att definiera vilken Django-inställningsfil som ska användas och testa databasanslutningen. Ett exempel på en sådan definition finns i 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
Det enkla utförandet kan se ut så här:
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 hotmodell. The permissive test settings and local service authentication are test fixtures, not production deployment defaults.
Lokal testning av 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.
Before running tests, please ensure development dependencies are installed:
uv sync --all-extras --dev
Testning med pytest¶
Innan du kör tester bör du samla in statiska filer, eftersom vissa tester är beroende av att de finns tillgängliga:
DJANGO_SETTINGS_MODULE=weblate.settings_test uv run ./manage.py collectstatic --noinput
You can use pytest to run the test suite locally:
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.
Körning av en enskild testfil:
uv run pytest weblate/utils/tests/test_search.py
Råd
Du behöver en databas (PostgreSQL) server för att kunna utföra tester. Som standard skapar Django en separat databas för att köra tester med prefixet test_, så om dina inställningar är konfigurerade för att använda weblate, kommer testerna att använda databasen test_weblate. Se Databasinställningar för Weblate för installationsinstruktioner.
weblate/settings_test.py används även i CI-miljön (se Kontinuerlig integration) och kan justeras med hjälp av miljövariabler:
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
Råd
Testerna kan också köras inuti utvecklarens docker-container, se Kör Weblate lokalt i Docker.
Se även
Se Testing in Django för mer information om hur man kör och skriver tester för 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.
Lokal testning av Weblate-moduler¶
The tests are executed using pytest. First you need to install development dependencies:
uv sync --all-extras --dev
Du kan sedan köra testsuiten i arkivets utcheckning:
uv run pytest
Testarkiv¶
Många av testerna i Weblate-testpaketet använder testarkivet. Testpaketets arkiv underhålls på https://github.com/WeblateOrg/test. Skriptet scripts/pack-test-data.sh används sedan för att generera en tarball med ett arkiv för vart och ett av de versionkontrollsystem som stöds. Dessa lagras som weblate/trans/tests/data/test-base-repo.git.tar, weblate/trans/tests/data/test-base-repo.hg.tar och weblate/trans/tests/data/test-base-repo.svn.tar i Weblate-arkivet.
Arkivet https://github.com/WeblateOrg/test taggas vid release-tidpunkten, vilket säkerställer att releasetaggarna kan användas för att komma åt testdata som användes vid release-tidpunkten. Skriptet försöker skapa reproducerbara tarbollar i så stor utsträckning som möjligt.