Skip to content

Tisipi/TDD-with-Python

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

70 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

TTD-with-Python

I used this repository while reading the book Test-Driven Development with Python, 2nd Edition, by Harry J.W. Percival.

If you need help with Django take a look at the Django Girls tutorial or the official v1.11 tutorial. Please note: This is an old unsafe version!

Windows 10 Installation

Install geckodriver.exe in Python36-32\Scripts directory.

Create python virtualenv in directory python-tdd-book:

pip install virtualenv
py -3.6 -m venv virtualenv

# To activate virtualenv:
virtualenv\Scripts\activate

# To deactivate virtualenv:
deactivate

Linux Installation (Mint, Manjaro)

Install geckodriver in /usr/local/bin.

Create python virtualenv in directory python-tdd-book:

sudo apt-get install python3-venv
python3.6 -m venv virtualenv

# To activate virtualenv:
source virtualenv/bin/activate

#To deactivate virtualenv:
deactivate

Selenium and Django

Install Django and Selenium in virtualenv:

pip install "django<1.12" "selenium<4"

In case of problems install specific versions used in book:

pip install django==1.11.3
pip install selenium==3.9.0

Create a Django project called superlists (note the "." at the end):

# On windows:
python virtualenv\Scripts\django-admin.py startproject superlists .
# On Linux:
django-admin.py startproject superlists .

To run Django’s development server:
python manage.py runserver

Update 2022:
Import error when using Python 3.10.2:
ImportError: cannot import name 'Iterator' from 'collections' (/usr/lib/python3.10/collections/init.py)

Workaround is to use an older version like Python 3.6:

  • Install python3.6
  • Install virtualenv which allows to specify a specific python version in your virtual environment:
    pip install virtualenv
  • Create virtual environment with Python 3.6:
    virtualenv virtualenv -p python3.6

Django App

Start an App called "lists":
python manage.py startapp lists

Don't forget to register the lists App in superlists/settings.py:

INSTALLED_APPS = [
	...
    'lists',
]

Django Tests

Django Unit Tests

Unit tests are defined in tests.py in your list App directory.
Unit tests are a subclass of Django class TestCase:

from django.test import TestCase

class MyTest(TestCase):
    def test_good_maths(self):
        self.assertEqual(1 + 1, 2)

To run all unit tests invoke Django test runner:
python manage.py test

To only run the unit tests of your App, in this case list, add the App name:
python manage.py test lists

Django Functional Tests

Functional tests are defined in file tests.py in App directory functional_tests. Functional tests are a subclass of Django class LiveServerTestCase:

from django.test import LiveServerTestCase

class MyFunctionalTest(LiveServerTestCase):

    def setUp(self):
        [...]

To only run functional tests:
python manage.py test functional_tests

Django Migration

When you change something in the Django model (for example change or add a field), the database tables that store these models need to be changed too. A Django migration propagates these changes in the model into the database schema.

python manage.py makemigrations

Django Database Creation

Actually create the database after the Django migration.

python manage.py migrate

The default database is SQLite. It is just a file located in your base directory as can be seen in yourDjangoProjectDir/settings.py:

DATABASES = {
    "default": {
        "ENGINE": "django.db.backends.sqlite3",
        "NAME": os.path.join(BASE_DIR, "db.sqlite3"),
    }
}

You can simply delete and recreate it:

rm db.sqlite3
python manage.py migrate --noinput

Overview Files

├── db.sqlite3  
├── functional_tests
│   ├── functional_tests.py       <<< Functional tests
│   └── __init__.py  
├── geckodriver.log  
├── lists                         <<< Directory of lists App  
│   ├── admin.py  
│   ├── apps.py  
│   ├── __init__.py  
│   ├── migrations                <<< Directory containing Django migrations  
│   ├── models.py                 <<< Django models  
│   ├── templates                 <<< HTML template directory  
│   ├── tests.py                  <<< Unittests of lists App  
│   └── views.py                  <<< Django views  
├── manage.py                     <<< Django's management script  
├── README.md  
├── superlists                    <<< Django's main project directory  
│   ├── __init__.py  
│   ├── settings.py               <<< register the lists App  
│   ├── urls.py                   <<< URL patterns mapping URLs to views  
│   └── wsgi.py  
└── virtualenv  
    ├── bin  
    ├── lib  
    ├── pyvenv.cfg  
    └── selenium  

About

Test-Driven Development with Python

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published