18.2. Tests Run
https://docs.djangoproject.com/en/stable/topics/testing/overview/
App must be in
settings.INSTALLED_APPS
All Django tests run always with
DEBUG=False
Regardless of the value of the DEBUG
setting in your configuration file,
all Django tests run with DEBUG=False
. This is to ensure that the observed
output of your code matches what will be seen in a production setting.
File myproject/shop/tests.py
>>>
... from django.test import TestCase
... from shop.models import Customer
...
...
... class CustomerTests(TestCase):
... def test_customer_create(self):
... customer = Customer.objects.create(firstname="Mark", lastname="Watney")
... self.assertEqual(customer.firstname, 'Mark')
... self.assertEqual(customer.lastname, 'Watney')
18.2.1. Run All
python manage.py test --parallel
Discover tests in any file named test*.py
under the current working directory:
$ python manage.py test --parallel
18.2.2. Run Package
python manage.py test shop
Run all the tests found within the package:
$ python manage.py test shop
18.2.3. Run Module
python manage.py test shop.tests
Run all the tests in the myproject/shop.tests
module:
$ python manage.py test shop.tests
18.2.4. Run TestCase
python manage.py test shop.tests.CustomerTests
Run just one test case:
$ python manage.py test shop.tests.CustomerTests
18.2.5. Run UnitTest
python manage.py test shop.tests.CustomerTests.test_customer_create
Run just one test method:
$ python manage.py test shop.tests.CustomerTests.test_customer_create
18.2.6. Verbose
$ python manage.py test -v 3
Found 10 test(s).
Creating test database for alias 'default' ('file:memorydb_default?mode=memory&cache=shared')...
Operations to perform:
Synchronize unmigrated apps: messages, staticfiles
Apply all migrations: admin, auth, contenttypes, sessions, shop
Running pre-migrate handlers for application admin
Running pre-migrate handlers for application auth
Running pre-migrate handlers for application contenttypes
Running pre-migrate handlers for application sessions
Running pre-migrate handlers for application shop
Synchronizing apps without migrations:
Creating tables...
Running deferred SQL...
Running migrations:
Applying contenttypes.0001_initial... OK (0.009s)
Applying auth.0001_initial... OK (0.004s)
Applying admin.0001_initial... OK (0.002s)
Applying admin.0002_logentry_remove_auto_add... OK (0.003s)
Applying admin.0003_logentry_add_action_flag_choices... OK (0.002s)
Applying contenttypes.0002_remove_content_type_name... OK (0.004s)
Applying auth.0002_alter_permission_name_max_length... OK (0.003s)
Applying auth.0003_alter_user_email_max_length... OK (0.002s)
Applying auth.0004_alter_user_username_opts... OK (0.002s)
Applying auth.0005_alter_user_last_login_null... OK (0.002s)
Applying auth.0006_require_contenttypes_0002... OK (0.000s)
Applying auth.0007_alter_validators_add_error_messages... OK (0.001s)
Applying auth.0008_alter_user_username_max_length... OK (0.002s)
Applying auth.0009_alter_user_last_name_max_length... OK (0.002s)
Applying auth.0010_alter_group_name_max_length... OK (0.002s)
Applying auth.0011_update_proxy_permissions... OK (0.002s)
Applying auth.0012_alter_user_first_name_max_length... OK (0.003s)
Applying sessions.0001_initial... OK (0.000s)
Applying shop.0001_initial... OK (0.000s)
Applying shop.0002_email... OK (0.001s)
Applying shop.0003_customer_birthdate... OK (0.001s)
Running post-migrate handlers for application admin
Adding content type 'admin | logentry'
Adding permission 'Permission object (1)'
Adding permission 'Permission object (2)'
Adding permission 'Permission object (3)'
Adding permission 'Permission object (4)'
Running post-migrate handlers for application auth
Adding content type 'auth | permission'
Adding content type 'auth | group'
Adding content type 'auth | user'
Adding permission 'Permission object (5)'
Adding permission 'Permission object (6)'
Adding permission 'Permission object (7)'
Adding permission 'Permission object (8)'
Adding permission 'Permission object (9)'
Adding permission 'Permission object (10)'
Adding permission 'Permission object (11)'
Adding permission 'Permission object (12)'
Adding permission 'Permission object (13)'
Adding permission 'Permission object (14)'
Adding permission 'Permission object (15)'
Adding permission 'Permission object (16)'
Running post-migrate handlers for application contenttypes
Adding content type 'contenttypes | contenttype'
Adding permission 'Permission object (17)'
Adding permission 'Permission object (18)'
Adding permission 'Permission object (19)'
Adding permission 'Permission object (20)'
Running post-migrate handlers for application sessions
Adding content type 'sessions | session'
Adding permission 'Permission object (21)'
Adding permission 'Permission object (22)'
Adding permission 'Permission object (23)'
Adding permission 'Permission object (24)'
Running post-migrate handlers for application shop
Adding content type 'shop | customer'
Adding content type 'shop | email'
Adding permission 'Permission object (25)'
Adding permission 'Permission object (26)'
Adding permission 'Permission object (27)'
Adding permission 'Permission object (28)'
Adding permission 'Permission object (29)'
Adding permission 'Permission object (30)'
Adding permission 'Permission object (31)'
Adding permission 'Permission object (32)'
System check identified no issues (0 silenced).
test_customer_with_birthdate (shop.tests.test_models_customer.CustomerCreateTest.test_customer_with_birthdate) ... ok
test_customer_with_firstname_and_lastname (shop.tests.test_models_customer.CustomerCreateTest.test_customer_with_firstname_and_lastname) ... ok
test_customer_age_patch (shop.tests.test_models_customer.CustomerFunctionalityTest.test_customer_age_patch) ... ok
test_customer_emails (shop.tests.test_models_customer.CustomerFunctionalityTest.test_customer_emails) ... ok
test_customer_name (shop.tests.test_models_customer.CustomerFunctionalityTest.test_customer_name) ... ok
test_customer_str (shop.tests.test_models_customer.CustomerFunctionalityTest.test_customer_str) ... ok
test_email_with_type_default (shop.tests.test_models_email.EmailCreateTest.test_email_with_type_default) ... ok
test_email_with_type_home (shop.tests.test_models_email.EmailCreateTest.test_email_with_type_home) ... ok
test_email_with_type_work (shop.tests.test_models_email.EmailCreateTest.test_email_with_type_work) ... ok
test_email_str (shop.tests.test_models_email.EmailFunctionalityTest.test_email_str) ... ok
----------------------------------------------------------------------
Ran 10 tests in 0.004s
OK
Destroying test database for alias 'default' ('file:memorydb_default?mode=memory&cache=shared')...