---
title: "Testes unitários"
version: 3.1
locale: pt-br
source: https://docs.djangoproject.com/pt-br/3.1/internals/contributing/writing-code/unit-tests/
canonical: https://djangodocs.dev/pt-br/3.1/internals/contributing/writing-code/unit-tests/
---
# Testes unitários

O Django vem com uma suíte de testes própria, no diretório `tests` do código. É nossa política garantir que todos os testes passem em todos os momentos.

Nós agradecemos toda e qualquer contribuição para a suíte de testes.

Todos os testes do Django usam a infraestrutura de testes que é distribuída com o Django para testar aplicações. Veja [Writing and running tests](/pt-br/3.1/topics/testing/overview/) para uma explicação de como escrever novos testes.

## Rodando os testes unitários

### Início Rápido

Primeiro, faça o [“fork”do Django no GitHub](https://github.com/django/django/fork).

Segundo, crie e ative um ambiente virtual. Se não estiver familiar de como fazê-lo, leia nosso [tutorial de como contribuir](/pt-br/3.1/intro/contributing/).

Next, clone your fork, install some requirements, and run the tests:

```console
$ git clone https://github.com/YourGitHubName/django.git django-repo
$ cd django-repo/tests
$ python -m pip install -e ..
$ python -m pip install -r requirements/py3.txt
$ ./runtests.py
```

*Windows*

```doscon
...\> git clone https://github.com/YourGitHubName/django.git django-repo
...\> cd django-repo\tests
...\> py -m pip install -e ..
...\> py -m pip install -r requirements\py3.txt
...\> runtests.py
```

Ao instalar os requisitos podem ser requeridos pacotes que ainda não estão instalados no seu computador. Você pode descobrir quais pacotes estão faltando, procurando na internet os termos da última linha da mensagem de erro da  instalação. Tente adicionar o nome do seu sistema operacional nos termos procurados se necessário.

If you have trouble installing the requirements, you can skip that step. See
[Rodando todos os testes](#running-unit-tests-dependencies) for details on installing the optional
test dependencies. If you don’t have an optional dependency installed, the
tests that require it will be skipped.

Running the tests requires a Django settings module that defines the databases
to use. To help you get started, Django provides and uses a sample settings
module that uses the SQLite database. See [Usando outro módulo settings](#running-unit-tests-settings) to
learn how to use a different settings module to run the tests with a different
database.

Está tendo problemas? Veja [Solução de Problemas](#troubleshooting-unit-tests) para alguns problemas comuns.

### Executando testes usando `tox`

[Tox](https://tox.readthedocs.io/) is a tool for running tests in different
virtual environments. Django includes a basic `tox.ini` that automates some
checks that our build server performs on pull requests. To run the unit tests
and other checks (such as [import sorting](/pt-br/3.1/internals/contributing/writing-code/coding-style/#coding-style-imports), the
[documentation spelling checker](/pt-br/3.1/internals/contributing/writing-documentation/#documentation-spelling-check), and
[code formatting](/pt-br/3.1/internals/contributing/writing-code/coding-style/#coding-style-python)), install and run the `tox`
command from any place in the Django source tree:

```console
$ python -m pip install tox
$ tox
```

*Windows*

```doscon
...\> py -m pip install tox
...\> tox
```

By default, `tox` runs the test suite with the bundled test settings file for
SQLite, `flake8`, `isort`, and the documentation spelling checker. In
addition to the system dependencies noted elsewhere in this documentation,
the command `python3` must be on your path and linked to the appropriate
version of Python. A list of default environments can be seen as follows:

```console
$ tox -l
py3
flake8
docs
isort>=5.1.0
```

*Windows*

```doscon
...\> tox -l
py3
flake8
docs
isort>=5.1.0
```

#### Testing other Python versions and database backends

In addition to the default environments, `tox` supports running unit tests
for other versions of Python and other database backends. Since Django’s test
suite doesn’t bundle a settings file for database backends other than SQLite,
however, you must [create and provide your own test settings](#running-unit-tests-settings). For example, to run the tests on Python 3.7
using PostgreSQL:

```console
$ tox -e py37-postgres -- --settings=my_postgres_settings
```

*Windows*

```doscon
...\> tox -e py37-postgres -- --settings=my_postgres_settings
```

This command sets up a Python 3.7 virtual environment, installs Django’s
test suite dependencies (including those for PostgreSQL), and calls
`runtests.py` with the supplied arguments (in this case,
`--settings=my_postgres_settings`).

The remainder of this documentation shows commands for running tests without
`tox`, however, any option passed to `runtests.py` can also be passed to
`tox` by prefixing the argument list with `--`, as above.

Tox also respects the [`DJANGO_SETTINGS_MODULE`](/pt-br/3.1/topics/settings/#envvar-DJANGO_SETTINGS_MODULE) environment variable, if
set. For example, the following is equivalent to the command above:

```console
$ DJANGO_SETTINGS_MODULE=my_postgres_settings tox -e py35-postgres
```

Windows users should use:

```doscon
...\> set DJANGO_SETTINGS_MODULE=my_postgres_settings
...\> tox -e py35-postgres
```

#### Executando os testes JavaScript

Django includes a set of [JavaScript unit tests](/pt-br/3.1/internals/contributing/writing-code/javascript/#javascript-tests) for
functions in certain contrib apps. The JavaScript tests aren’t run by default
using `tox` because they require `Node.js` to be installed and aren’t
necessary for the majority of patches. To run the JavaScript tests using
`tox`:

```console
$ tox -e javascript
```

*Windows*

```doscon
...\> tox -e javascript
```

This command runs `npm install` to ensure test requirements are up to
date and then runs `npm test`.

### Running tests using `django-docker-box`

[django-docker-box](https://github.com/django/django-docker-box) allows you to run the Django’s test suite across all
supported databases and python versions. See the [django-docker-box](https://github.com/django/django-docker-box) project
page for installation and usage instructions.

### Usando outro módulo `settings`

O módulo de definições (`tests/test_sqlite.py`) permite que você execute a suíte de testes usando o SQLite. Se quiser rodar os testes usando um banco de dados diferente, você precisa definir seu próprio arquivo de definições. Alguns testes, como aqueles para o `contrib.postgres`, são específicos para um “backend” particular de banco de dados e serão deixados de lado se executados com um “backend” diferente.

To run the tests with different settings, ensure that the module is on your
[`PYTHONPATH`](https://docs.python.org/3/using/cmdline.html#envvar-PYTHONPATH) and pass the module with `--settings`.

O setting [`DATABASES`](/pt-br/3.1/ref/settings/#std-setting-DATABASES) em qualquer settings de módulo de testes precisa definir dois bancos:

- O banco `default`. Este banco deve usar o backend que você quer usar para testes primários.
- Um banco de dados com o apelido `other`. O banco de dados `other` é usado para testar consultas que podem ser direcionadas a diferentes bancos de dados. Este banco de dados devem usar o mesmo “backend” que o `default`, e deve ter um nome diferente.

Se você está usando um backend que não é o SQLite, você irá precisar providenciar outros detalhes para cada banco de dados:

- A opção [`USER`](/pt-br/3.1/ref/settings/#std-setting-USER) deve especificar uma conta de usuário válida para o banco de dados. Esse usuário precisa ter permissão para executar `CREATE DATABASE` de modo que o banco de dados de teste possa ser criado.
- A opção [`PASSWORD`](/pt-br/3.1/ref/settings/#std-setting-PASSWORD) precisa fornecer uma senha para o user [`USER`](/pt-br/3.1/ref/settings/#std-setting-USER) que foi especificado.

Os nomes dos bancos de dados de teste são gerados adicionando o prefixo `test_` ao valor do campo [`NAME`](/pt-br/3.1/ref/settings/#std-setting-NAME) das configurações dos bancos de dados definidos dentro de [`DATABASES`](/pt-br/3.1/ref/settings/#std-setting-DATABASES). Esses bancos de testes são deletados quando os testes são concluídos.

Você também terá que garantir que o seu banco de dados usa UTF-8 por padrão como charset. Se o servidor do seu banco de dados não usa UTF-8 como charset padrão, você terá que incluir um valor para [`CHARSET`](/pt-br/3.1/ref/settings/#std-setting-TEST_CHARSET)  no dicionário do settings de teste para o banco de dados correspondente.

### Rodando apenas alguns dos testes.

A suíte completa de testes do Django leva um tempo para rodar, e rodar cada um dos testes pode ser redundantes se, por exemplo, você deseja adicionar um teste no Django que você quer rodar rapidamente sem ter que rodar todo o resto. Você pode rodar um subconjunto de testes unitários adicionando os nomes dos módulos de teste para o comando `runtests.py` na linha de comando.

For example, if you’d like to run tests only for generic relations and
internationalization, type:

```console
$ ./runtests.py --settings=path.to.settings generic_relations i18n
```

*Windows*

```doscon
...\> runtests.py --settings=path.to.settings generic_relations i18n
```

Como você pode saber os nomes de testes em específico? Procure em `tests/` — cada nome de diretório lá representa o nome de um teste.

If you want to run only a particular class of tests, you can specify a list of
paths to individual test classes. For example, to run the `TranslationTests`
of the `i18n` module, type:

```console
$ ./runtests.py --settings=path.to.settings i18n.tests.TranslationTests
```

*Windows*

```doscon
...\> runtests.py --settings=path.to.settings i18n.tests.TranslationTests
```

Going beyond that, you can specify an individual test method like this:

```console
$ ./runtests.py --settings=path.to.settings i18n.tests.TranslationTests.test_lazy_objects
```

*Windows*

```doscon
...\> runtests.py --settings=path.to.settings i18n.tests.TranslationTests.test_lazy_objects
```

You can run tests starting at a specified top-level module with `--start-at`
option. For example:

```console
$ ./runtests.py --start-at=wsgi
```

*Windows*

```doscon
...\> runtests.py --start-at=wsgi
```

You can also run tests starting after a specified top-level module with
`--start-after` option. For example:

```console
$ ./runtests.py --start-after=wsgi
```

*Windows*

```doscon
...\> runtests.py --start-after=wsgi
```

Note that the `--reverse` option doesn’t impact on `--start-at` or
`--start-after` options. Moreover these options cannot be used with test
labels.

### Rodando os testes do Selenium

Some tests require Selenium and a Web browser. To run these tests, you must
install the [selenium](https://pypi.org/project/selenium/) package and run the tests with the
`--selenium=<BROWSERS>` option. For example, if you have Firefox and Google
Chrome installed:

```console
$ ./runtests.py --selenium=firefox,chrome
```

*Windows*

```doscon
...\> runtests.py --selenium=firefox,chrome
```

Veja o pacote [selenium.webdriver](https://github.com/SeleniumHQ/selenium/tree/master/py/selenium/webdriver) para uma lista de navegadores disponíveis.

Definindo o `--selenium` automaticamente define `--tags=selenium` para rodar somente testes que requerem o selenium.

Some browsers (e.g. Chrome or Firefox) support headless testing, which can be
faster and more stable. Add the `--headless` option to enable this mode.

### Rodando todos os testes

Se você quiser rodar toda a suíte de testes, você precisará instalar algumas dependências:

- [argon2-cffi](https://pypi.org/project/argon2_cffi/) 16.1.0+
- [asgiref](https://pypi.org/project/asgiref/) 3.2.10+ (required)
- [bcrypt](https://pypi.org/project/bcrypt/)
- [docutils](https://pypi.org/project/docutils/)
- [geoip2](https://pypi.org/project/geoip2/)
- [jinja2](https://pypi.org/project/jinja2/) 2.7+
- [numpy](https://pypi.org/project/numpy/)
- [Pillow](https://pypi.org/project/Pillow/) 6.2.0+
- [PyYAML](https://pyyaml.org/wiki/PyYAML)
- [pytz](https://pypi.org/project/pytz/) (required)
- [pywatchman](https://pypi.org/project/pywatchman/)
- [setuptools](https://pypi.org/project/setuptools/)
- [memcached](https://memcached.org/), e um [Python binding suportado](/pt-br/3.1/topics/cache/#memcached)
- [gettext](https://www.gnu.org/software/gettext/manual/gettext.html) ([gettext on Windows](/pt-br/3.1/topics/i18n/translation/#gettext-on-windows))
- [selenium](https://pypi.org/project/selenium/)
- [sqlparse](https://pypi.org/project/sqlparse/) 0.2.2+ (required)
- [tblib](https://pypi.org/project/tblib/) 1.5.0+

You can find these dependencies in [pip requirements files](https://pip.pypa.io/en/latest/user_guide/#requirements-files) inside the
`tests/requirements` directory of the Django source tree and install them
like so:

```console
$ python -m pip install -r tests/requirements/py3.txt
```

*Windows*

```doscon
...\> py -m pip install -r tests\requirements\py3.txt
```

Se encontrar algum erro durante a instalação, talvez esteja faltando no seu sistema alguma dependência de um ou mais pacotes Python. Consulte a documentação de pacotes faltantes ou procure na Web a mensagem de erro que encontrou.

Você também pode instalar o adaptador (ou adaptadores) de sua escolha usando `oracle.txt`, `mysql.txt`, ou `postgres.txt`.

Se você quiser testar o backend memcached, você também terá que definir uma configuração [`CACHES`](/pt-br/3.1/ref/settings/#std-setting-CACHES) que aponte para a sua instância do memcached.

Para rodar os testes do GeoDjango, você precisará [instalar um banco de dados espacial e instalar as bibliotecas geoespaciais](/pt-br/3.1/ref/contrib/gis/install/).

Cada uma dessas dependências é opcional. Se você estiver esquecendo qualquer uma delas, os testes associados serão pulados.

To run some of the autoreload tests, you’ll need to install the [Watchman](https://facebook.github.io/watchman/)
service.

### Cobertura de código

Voluntários são encorajados a rodar a cobertura na suíte de testes para identificar áreas que precisam de mais testes. A instalação da ferramenta de cobertura e a sua utilização é descrita em [testando a cobertura de código](/pt-br/3.1/topics/testing/advanced/#topics-testing-code-coverage).

Coverage should be run in a single process to obtain accurate statistics. To
run coverage on the Django test suite using the standard test settings:

```console
$ coverage run ./runtests.py --settings=test_sqlite --parallel=1
```

*Windows*

```doscon
...\> coverage run runtests.py --settings=test_sqlite --parallel=1
```

After running coverage, generate the html report by running:

```console
$ coverage html
```

*Windows*

```doscon
...\> coverage html
```

Quando executar a cobertura para testes do Django, o arquivo de settings incluído `.coveragerc` define `coverage_html` como sendo o diretório de saída para o relatório e também exclui vários diretórios não relevantes para os resultados (código de testes ou código externo incluídos no Django).

## Aplicações Contrib

Testes para apps contrib pode ser encontrados no diretório `tests/`, tipicamente em `<nome_app>_tests`. Por exemplo, testes para o `contrib.auth` estão localizados em `tests/auth_tests`.

## Solução de Problemas

### Test suite hangs or shows failures on `master` branch

Ensure you have the latest point release of a [supported Python version](/pt-br/3.1/faq/install/#faq-python-version-support), since there are often bugs in earlier versions
that may cause the test suite to fail or hang.

On **macOS** (High Sierra and newer versions), you might see this message
logged, after which the tests hang:

```
objc[42074]: +[__NSPlaceholderDate initialize] may have been in progress in
another thread when fork() was called.
```

To avoid this set a `OBJC_DISABLE_INITIALIZE_FORK_SAFETY` environment
variable, for example:

```
$ OBJC_DISABLE_INITIALIZE_FORK_SAFETY=YES ./runtests.py
```

Or add `export OBJC_DISABLE_INITIALIZE_FORK_SAFETY=YES` to your shell’s
startup file (e.g. `~/.profile`).

### Vários testes falham com `UnicodeEncodeError`

Se o pacote de `locales` não estiver instalado, alguns testes irão falhar com um `UnicodeEncodeError`.

You can resolve this on Debian-based systems, for example, by running:

```console
$ apt-get install locales
$ dpkg-reconfigure locales
```

You can resolve this for macOS systems by configuring your shell’s locale:

```console
$ export LANG="en_US.UTF-8"
$ export LC_ALL="en_US.UTF-8"
```

Execute o comando `locale` para confirmar a alteração. Opcionalmente, adicione os comandos de “export” para o arquivo de inicialização do seu “shell” (exemplo `~/.bashrc` para o Bash) para evitar ter que redefini-los.

### Testes que só falham em combinação

Caso um teste passe quando rodado isoladamente mas que falhe quando rodado dentro de uma suíte inteira de testes, nós temos algumas ferramentas para ajudar a analizar o problema.

A opção `--bisect` do `runtests.py` irá executar o teste que falhou enquanto reparte o conjunto de testes do qual o teste falho faz parte a cada nova iteração, geralmente permitindo identificar um pequeno número de testes que podem estar relacionados com a falha.

For example, suppose that the failing test that works on its own is
`ModelTest.test_eq`, then using:

```console
$ ./runtests.py --bisect basic.tests.ModelTest.test_eq
```

*Windows*

```doscon
...\> runtests.py --bisect basic.tests.ModelTest.test_eq
```

Nós vamos tentar descobrir um teste que interfere com o teste escolhido. Primeiro, o teste é rodado com a primeira metade da suíte de testes. Se uma falha ocorrer, a primeira metade da suíte de testes é dividida em dois grupos e cada grupo é então rodado com o teste especificado. Se não existe falha com a primeira metade da suíte de testes, a segunda metade da suíte é rodada com o teste especificado e dividida apropriadamente conforme descrito anteriormente. O processo se repete até que o conjunto de testes que falham é minimizado.

The `--pair` option runs the given test alongside every other test from the
suite, letting you check if another test has side-effects that cause the
failure. So:

```console
$ ./runtests.py --pair basic.tests.ModelTest.test_eq
```

*Windows*

```doscon
...\> runtests.py --pair basic.tests.ModelTest.test_eq
```

nós vamos parear `test_eq` com cada rótulo de teste

With both `--bisect` and `--pair`, if you already suspect which cases
might be responsible for the failure, you may limit tests to be cross-analyzed
by [specifying further test labels](#runtests-specifying-labels) after
the first one:

```console
$ ./runtests.py --pair basic.tests.ModelTest.test_eq queries transactions
```

*Windows*

```doscon
...\> runtests.py --pair basic.tests.ModelTest.test_eq queries transactions
```

You can also try running any set of tests in reverse using the `--reverse`
option in order to verify that executing tests in a different order does not
cause any trouble:

```console
$ ./runtests.py basic --reverse
```

*Windows*

```doscon
...\> runtests.py basic --reverse
```

### Verificando as consultas SQL realizadas durante os testes

If you wish to examine the SQL being run in failing tests, you can turn on
[SQL logging](/pt-br/3.1/topics/logging/#django-db-logger) using the `--debug-sql` option. If you
combine this with `--verbosity=2`, all SQL queries will be output:

```console
$ ./runtests.py basic --debug-sql
```

*Windows*

```doscon
...\> runtests.py basic --debug-sql
```

### Visualizando todo o traceback de um teste falho

By default tests are run in parallel with one process per core. When the tests
are run in parallel, however, you’ll only see a truncated traceback for any
test failures. You can adjust this behavior with the `--parallel` option:

```console
$ ./runtests.py basic --parallel=1
```

*Windows*

```doscon
...\> runtests.py basic --parallel=1
```

You can also use the [`DJANGO_TEST_PROCESSES`](/pt-br/3.1/ref/django-admin/#envvar-DJANGO_TEST_PROCESSES) environment variable for
this purpose.

## Dicas para rodar testes

### Isolando o registro de modelos

Para evitar popular o registro [`apps`](/pt-br/3.1/ref/applications/#django.apps.apps) global e prevenir que tabelas desnecessárias sejam criadas, os modelos definem um método de teste que deve ser vinculado a uma instância de `Apps`.

```python
from django.apps.registry import Apps
from django.db import models
from django.test import SimpleTestCase

class TestModelDefinition(SimpleTestCase):
    def test_model_definition(self):
        test_apps = Apps(['app_label'])

        class TestModel(models.Model):
            class Meta:
                apps = test_apps
        ...
```

#### `django.test.utils.isolate_apps(*app_labels, attr_name=None, kwarg_name=None)`

Já que este padrão de desenvolvimento envolve vários códigos clichés, o Django fornece o decorador [`isolate_apps()`](#django.test.utils.isolate_apps). Este é usado assim:

```python
from django.db import models
from django.test import SimpleTestCase
from django.test.utils import isolate_apps

class TestModelDefinition(SimpleTestCase):
    @isolate_apps('app_label')
    def test_model_definition(self):
        class TestModel(models.Model):
            pass
        ...
```

> **A definição app_label**
>
> Modelos definidos em um método de teste sem explicitar o [`app_label`](/pt-br/3.1/ref/models/options/#django.db.models.Options.app_label) são automaticamente assinalados com a etiqueta da aplicação na qual sua classe de teste está localizada.
>
> De modo a ter certeza que os modelos definidos dentro do contexto das instâncias de [`isolate_apps()`](#django.test.utils.isolate_apps) estão corretamente instalados, você deve passar a definição dos `app_label` alvo como argumentos:
>
> *tests/app\_label/tests.py*
>
> ```python
> from django.db import models
> from django.test import SimpleTestCase
> from django.test.utils import isolate_apps
>
> class TestModelDefinition(SimpleTestCase):
>     @isolate_apps('app_label', 'other_app_label')
>     def test_model_definition(self):
>         # This model automatically receives app_label='app_label'
>         class TestModel(models.Model):
>             pass
>
>         class OtherAppModel(models.Model):
>             class Meta:
>                 app_label = 'other_app_label'
>         ...
> ```

O decorador pode também ser aplicado a classes:

```python
from django.db import models
from django.test import SimpleTestCase
from django.test.utils import isolate_apps

@isolate_apps('app_label')
class TestModelDefinition(SimpleTestCase):
    def test_model_definition(self):
        class TestModel(models.Model):
            pass
        ...
```

A instância de `Apps` temporárias usadas para isolar o registro de modelos pode ser obtida como um atributo quando usado como um decorador de classe através do parâmetro `attr_name`:

```python
from django.db import models
from django.test import SimpleTestCase
from django.test.utils import isolate_apps

@isolate_apps('app_label', attr_name='apps')
class TestModelDefinition(SimpleTestCase):
    def test_model_definition(self):
        class TestModel(models.Model):
            pass
        self.assertIs(self.apps.get_model('app_label', 'TestModel'), TestModel)
```

Ou como argumento do método de teste usado como um decorador de métodos através do uso do parâmetro `kwarg_name`:

```python
from django.db import models
from django.test import SimpleTestCase
from django.test.utils import isolate_apps

class TestModelDefinition(SimpleTestCase):
    @isolate_apps('app_label', kwarg_name='apps')
    def test_model_definition(self, apps):
        class TestModel(models.Model):
            pass
        self.assertIs(apps.get_model('app_label', 'TestModel'), TestModel)
```
