Skip to content

geoffreynyaga/django-and-djangorestframework-pytest-tutorial

Repository files navigation

Welcome to Pytest Django and DRF Tutorial 🦄

This tutorial series is dedicated to exploring the basics of inbuilt Django testing and introduces Pytest and its various plugins to help us write better code

Currently we cover the following:

  • Code coverage,
  • Generating fixtures using Mixer
  • Complex testing of edge-cases using Hypothesis
  • Testing Serializers
  • Testing DRF Views
  • DRF authentication testing
  • Github Actions

Additional Resources

Installation 📥

mkdir <my_project>

cd my_project

git clone https://github.com/geoffreynyaga/django-and-djangorestframework-pytest-tutorial.git .

Testing setup🧪🧪

The projects uses pytest and black as the formatting option. The tests also check for consistencies on code format.

To initiate tests follow the steps below:

  1. Its advised to create a virtual environment in the root folder

    virtualenv venv
  2. Activate the environent.

    a. For Linux/MacOS users use the command below

    source venv/bin/activate

    b. for windows users

    cd venv/Scripts
    
    activate.bat
  3. Install the requirements

    pip install -r requirements.txt
  4. Run the pytest command

    pytest

The testing results will be displayed and there will also be a htmlcov folder generated inside the project that will contain the code coverage details.

    .
    ├── accounts
    │   ├── api
    │   │   └── tests
    │   └── migrations
    ├── classroom
    ├── htmlcov
    └── venv

Open up the folder and open the index.html in your browser to see this information.

Project layout

    .
    ├── accounts
    │   ├── api
    │   │   └── tests
    │   └── migrations
    ├── classroom
    ├── htmlcov
    └── venv # after you create the virtualenv

Releases

No releases published

Packages

No packages published

Languages