Skip to content

arulmr/pytest_demo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Pytest Demo

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.

Packages and Versions Used

Setup

  1. Clone the repo.
  2. Create a virtual environment.
  3. Install requirements using the command pip install -r requirements.txt.
  4. Apply DB migrations using the command python manage.py migrate.
  5. Run the application using the command python manage.py runserver.
  6. Refer urls.py for the available URLs.

Commonly Used Pytest Commands

  1. Running pytest:
pytest
  1. 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
  1. Running pytest with detailed output:
pytest -v
  1. Running pytest with print statements or to use pdb:
pytest -s
  1. To get pytest coverage:
pytest --cov=demo/

# Sample outputpytest --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 =================================================================
  1. Generate pytest coverage report in HTML format:
pytest --cov-report html --cov=demo/

Sample HTML report

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors