Pytest-Django
pytest-django is a plugin for pytest that provides a set of useful tools for testing Django applications and projects.
Configuration
pip install pytest-django
Make sure DJANGO_SETTINGS_MODULE is
defined (see Configuring Django settings)
and make your tests discoverable (see My tests are not being found. Why?):
pytest.ini
Créer un fichier pytest.ini
touch pytest.ini
⚙️ pytest.ini file configuration
# pytest.ini
[pytest]
DJANGO_SETTINGS_MODULE = oc_lettings_site.settings
# -- recommended but optional:
python_files = tests.py test_*.py *_tests.py
addopts = -v
Run test
⏩️ Run your tests with pytest:
pytest
pytest -vvv
⏩️ To view test details in the terminal, use the -vvv option with pytest:
pytest -vvv
pytest –cov=.
To measure the test coverage of a project, run pytest with the --cov option to specify the folder to be analyzed:
pytest --cov=.
Report HTML
⚙️ pytest –cov=. –cov-report html
To generate an HTML report automatically, use the --cov-report html option with pytest:
pytest --cov=. --cov-report html
Then, open the htmlcov folder and launch index.html in a browser to view the report.
Report pipelines test