This Django project contains basic Pytest tests covering:
- Pytest fixtures.
- Making requests to internal URLs using Pytest client.
- Generating DB data using Django dynamic fixtures.
- Mocking external API calls.
- User login.
- Admin user.
- Pytest coverage report.
Inline comments are given in the tests for better understanding.
- Python 3.8.10
- Django 4.1.3
- Django Dynamic Fixture 3.1.2
- pytest-django 4.5.2
- pytest-cov 4.0.0
- requests-mock 1.10.0
- Clone the repo.
- Create a virtual environment.
- Install requirements using the command
pip install -r requirements.txt. - Apply DB migrations using the command
python manage.py migrate. - Run the application using the command
python manage.py runserver. - Refer urls.py for the available URLs.
- Running pytest:
pytest- Running specific pytest test case:
# Running specific test class
pytest -k TestSampleClass
# Running specific individual function or a function inside a test class
pytest -k test_sample_function- Running pytest with detailed output:
pytest -v- Running pytest with print statements or to use pdb:
pytest -s- To get pytest coverage:
pytest --cov=demo/
# Sample output
❯ pytest --cov=demo/
=============================================================== test session starts ================================================================
platform linux -- Python 3.8.10, pytest-7.2.0, pluggy-1.0.0
django: settings: pytest_demo.settings (from ini)
rootdir: /home/arul/git/pytest_demo, configfile: pytest.ini
plugins: requests-mock-1.10.0, django-4.5.2, cov-4.0.0
collected 5 items
demo/tests.py ..... [100%]
---------- coverage: platform linux, python 3.8.10-final-0 -----------
Name Stmts Miss Cover
-----------------------------------------------------
demo/__init__.py 0 0 100%
demo/admin.py 1 0 100%
demo/apps.py 4 0 100%
demo/forms.py 10 0 100%
demo/migrations/0001_initial.py 5 0 100%
demo/migrations/__init__.py 0 0 100%
demo/models.py 13 1 92%
demo/tests.py 61 0 100%
demo/views.py 15 0 100%
-----------------------------------------------------
TOTAL 109 1 99%
================================================================ 5 passed in 4.29s =================================================================- Generate pytest coverage report in HTML format:
pytest --cov-report html --cov=demo/