# Conjunto de testes e integração contínua do Weblate

Existem conjuntos de teste para a maior parte do código atual, aumente a cobertura adicionando casos de teste para qualquer nova funcionalidade e verifique se funciona.

<a id="ci-tests"></a>

## Integração contínua

Weblate relies on [GitHub Actions](https://github.com/WeblateOrg/weblate/actions) to run tests, build documentation, code
linting, and other tasks to ensure code quality.

[Codecov](https://app.codecov.io/gh/WeblateOrg/weblate/) collects the code coverage information from the tests that were run.

Existem vários trabalhos para verificar diferentes aspectos:

* Unit and functional tests using [pytest](https://pytest.org/).
* Documentation build and external links using [Sphinx](https://www.sphinx-doc.org/).
* Code linting and quality assurance using [ruff](https://docs.astral.sh/ruff/) and [pylint](https://www.pylint.org/).
* Code security scanning using [CodeQL](https://codeql.github.com/).
* Visual changes testing is utilizing [Argos CI](https://argos-ci.com).
* Code formatting using [prek](https://pypi.org/project/prek/), a faster
  third-party reimplementation of the [pre-commit](https://pre-commit.com/)
  framework.
* Testes de migração de todas as versões com suporte
* Package builds, metadata, manifest, archive-content, and installation checks.
  The package lint tools are pinned in `pyproject.toml` and resolved in
  `uv.lock`; CI runs them with **uv run --frozen**.
* Dependency review of pull requests.

GitHub rulesets protect `main` and `stable`. They require pull requests and
passing status checks, including package build and lint checks and dependency
review. The [GitHub repository rules](https://github.com/WeblateOrg/weblate/rules) show the current required checks and
permitted writers. Membership in the writer allowlist does not by itself
bypass the pull-request or CI requirements. Repository administrators and
organization owners have a separate bypass for maintainer operations.

A configuração do CI está no diretório `.github/workflows`. Ele usa muito scripts auxiliares armazenados no diretório `ci`. Os scripts também podem ser executados manualmente, mas eles requerem várias variáveis de ambiente, principalmente definindo o arquivo de configurações do Django para usar e conexão com o banco de dados de teste. A definição de exemplo disso está em `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.

```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 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
```

A execução simples pode ser semelhante a:

```sh
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:

```sh
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](https://docs.weblate.org/pt-br/latest/security/threat-model.md). The permissive test settings
and local service authentication are test fixtures, not production deployment
defaults.

<a id="local-tests"></a>

## Local testing of Weblate

For an isolated environment with dependencies and test services already
configured, use the [development container](https://docs.weblate.org/pt-br/latest/contributing/start.md#devcontainer). 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:

```sh
uv sync --all-extras --dev
```

### Testando usando pytest

Antes de executar os testes, você deve coletar arquivos estáticos, pois alguns testes contam com a presença deles:

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

You can use pytest to run the test suite locally:

```sh
uv run pytest
```

Property-based tests using [hypothesis](https://pypi.org/project/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.

Executando um arquivo de teste individual:

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

#### HINT
Você precisará de um servidor de banco de dados (PostgreSQL) a ser usado para os testes. Por padrão, o Django cria um banco de dados separado para executar testes com o prefixo `test_`, então no caso de suas configurações estarem configuradas para usar `weblate`, os testes usarão o banco de dados `test_weblate`. Consulte [Configuração de banco de dados para o Weblate](https://docs.weblate.org/pt-br/latest/admin/install.md#database-setup) para instruções de configuração.

O `weblate/settings_test.py` é usado no ambiente CI também (consulte [Integração contínua](#ci-tests)) e pode ser ajustado usando variáveis de ambiente:

```sh
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
```

#### HINT
Os testes também podem ser executados dentro de um contêiner Docker do desenvolvedor, consulte [Executando Weblate localmente no Docker](https://docs.weblate.org/pt-br/latest/contributing/start.md#dev-docker).

#### SEE ALSO
Consulte [Testando em Django](https://docs.djangoproject.com/pt-br/stable/topics/testing/) para mais informações sobre a execução e escrita de testes para 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

The tests are executed using **pytest**. First you need to install development dependencies:

```sh
uv sync --all-extras --dev
```

Você pode então executar o testsuite no checkout do repositório:

```sh
uv run pytest
```

<a id="test-data"></a>

# 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](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](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.
