Testovacia sada Weblate a kontinuálna integrácia

Testovacie sady existujú pre väčšinu aktuálneho kódu, zvýšte pokrytie pridaním testovacích prípadov pre akúkoľvek novú funkcionalitu a overte, že funguje.

Kontinuálna integrácia

Weblate sa spolieha na GitHub Actions na spúšťanie testov, zostavovanie dokumentácie, linting kódu a iné úlohy na zabezpečenie kvality kódu.

Codecov zhromažďuje informácie o pokrytí kódu zo spustených testov.

Existuje niekoľko úloh na overenie rôznych aspektov:

  • Jednotkové a funkčné testy pomocou pytest.

  • Zostavovanie dokumentácie a externé odkazy pomocou Sphinx.

  • Lintovanie kódu a zabezpečenie kvality pomocou ruff a pylint.

  • Skenovanie bezpečnosti kódu pomocou CodeQL.

  • Testovanie vizuálnych zmien využíva Argos CI.

  • Formátovanie kódu pomocou prek, rýchlejšej reimplementácie frameworku pre-commit tretej strany.

  • Testovanie migrácie zo všetkých podporovaných vydaní

  • Overenie nastavenia (zabezpečuje, že generované distribučné súbory nič nevynechávajú a dajú sa testovať)

Konfigurácia pre CI je v adresári .github/workflows. Intenzívne využíva pomocné skripty uložené v adresári ci. Skripty môžu byť tiež vykonané manuálne, ale vyžadujú niekoľko premenných prostredia, najmä definujúce súbor nastavení Django na použitie a pripojenie k testovacej databáze. Príklad definície toho je v scripts/test-database.sh:

Testy snímok obrazovky Selenium v weblate/trans/tests/test_selenium.py slúžia na dva účely. Generujú obrázky pre testovanie vizuálnych zmien v CI a rovnaké obrázky sa konvertujú na snímky obrazovky dokumentácie pomocou make -C docs update-screenshots. Udržujte fixtúry snímok obrazovky blízko skutočných vykreslených stránok, aby CI zachytilo regresie UI. Keď snímka obrazovky obsahuje nestále runtime dáta, uprednostnite deterministické vstupy testov na strane servera pred zmenami DOM po vykreslení.

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

Jednoduché vykonanie môže vyzerať takto:

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

Lokálne testovanie Weblate

Pre izolované prostredie s už nakonfigurovanými závislosťami a testovacími službami použite vývojový kontajner. Jeho CLI dokáže v každom Git worktree spustiť rovnaké príkazy pytest a lint, aké sú zdokumentované nižšie.

Pred spustením testov sa uistite, že sú nainštalované vývojové závislosti:

uv sync --all-extras --dev

Testovanie pomocou pytest

Pred spustením testov by ste mali zozbierať statické súbory, pretože niektoré testy sa spoliehajú na ich prítomnosť:

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

Na lokálne spustenie testovacej sady môžete použiť pytest:

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.

Spustenie jednotlivého testovacieho súboru:

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

Rada

Budete potrebovať databázový server (PostgreSQL) na použitie pre testy. Predvolene Django vytvára samostatnú databázu na spúšťanie testov s prefixom test_, takže v prípade, že vaše nastavenia sú nakonfigurované na použitie weblate, testy budú používať databázu test_weblate. Pokyny na nastavenie nájdete v Nastavenie databázy pre Weblate.

weblate/settings_test.py sa používa aj v prostredí CI (pozri Kontinuálna integrácia) a môže byť ladený pomocou premenných prostredia:

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

Rada

Testy môžu byť tiež vykonané vo vnútri vývojárskeho docker kontajnera, pozri Spúšťanie Weblate lokálne v Dockeri.

Viď aj

Viac informácií o spúšťaní a písaní testov pre Django nájdete v Testing in 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.

Lokálne testovanie modulov Weblate

Testy sa vykonávajú pomocou pytest. Najprv musíte nainštalovať vývojové závislosti:

uv sync --all-extras --dev

Potom môžete vykonať testovaciu sadu v checkout-e repozitára:

uv run pytest

Testovací repozitár

Mnoho testov v testovacej sade Weblate používa testovací repozitár. Repozitár testovacej sady je udržiavaný na https://github.com/WeblateOrg/test. Skript scripts/pack-test-data.sh sa potom používa na generovanie tarballu s repozitárom pre každý z podporovaných systémov správy verzií. Tieto sú uložené ako weblate/trans/tests/data/test-base-repo.git.tar, weblate/trans/tests/data/test-base-repo.hg.tar, a weblate/trans/tests/data/test-base-repo.svn.tar v repozitári Weblate.

Repozitár https://github.com/WeblateOrg/test je tagovaný v čase vydania, čo zabezpečuje, že tagy vydania môžu byť použité na prístup k testovacím dátam použitým v čase vydania. Skript sa snaží vytvoriť reprodukovateľné tarbally čo najviac.