13👍
Depending on your needs you have a few choices:
-
You could write a custom test runner or adjust the default one: https://docs.djangoproject.com/en/1.6/topics/testing/advanced/#other-testing-frameworks
-
You could use SimpleTestCase
-
There are also add-ons like django-test-utils (although I’m not sure if that specific one works with modern Django versions).
-
Alternatively, to speed everything up, you could use SQLite’s in-memory database OR create your test database in RAM disk (like tmpfs or ramfs) – in fact this is orthogonal to using other techniques.
37👍
From Django 1.8 on you could use the –keepdb flag, when calling the manage.py
New in Django 1.8: You can prevent the test databases from being
destroyed by adding the –keepdb flag to the test command. This will
preserve the test database between runs. If the database does not
exist, it will first be created. Any migrations will also be applied
in order to keep it up to date.
(https://docs.djangoproject.com/en/1.8/topics/testing/overview/#the-test-database)
So your call could look as follows:
python manage.py test --keepdb
Or using the shorthand -k it could look like that:
python manage.py test -k
- [Django]-Django template convert to string
- [Django]-Can a generic.GenericForeignKey() field be Null?
- [Django]-Can't install via pip because of egg_info error
2👍
You maybe can try with test runner
Example:
First, create test_runners.py
from django.test.runner import DiscoverRunner
class NoDbTestRunner(DiscoverRunner):
def setup_databases(self, **kwargs):
""" Override the database creation defined in parent class """
pass
def teardown_databases(self, old_config, **kwargs):
""" Override the database teardown defined in parent class """
pass
Then declare above runner in settings.py
TEST_RUNNER = 'api.tests.test_runners.NoDbTestRunner'
- [Django]-How to reverse the URL of a ViewSet's custom action in django restframework
- [Django]-How to show ALL keys through redis-cli?
- [Django]-How can reference the last item in a list in a Django template? {{ list.-1.key }}
1👍
django-nose supports reusing the database:
https://github.com/django-nose/django-nose#enabling-database-reuse
However, make sure to read the comments:
The one new wrinkle is that, whenever your DB schema changes, you
should leave the flag off the next time you run tests. This will cue
the test runner to reinitialize the test database.Also, REUSE_DB is not compatible with TransactionTestCases that leave
junk in the DB, so be sure to make your TransactionTestCases hygienic
(see below) if you want to use it.
- [Django]-How do you know if memcached is doing anything?
- [Django]-Remove Labels in a Django Crispy Forms
- [Django]-Uwsgi installation error in windows 7
1👍
The following solution will also reduce the db creation time if there are more number of south migrations. During unit testing, running syncdb instead of running all the south migrations will be much faster.
SOUTH_TESTS_MIGRATE = False # To disable migrations and use syncdb
instead
- [Django]-How to get GET request values in Django Views?
- [Django]-How to use if/else condition on Django Templates?
- [Django]-CORS error while consuming calling REST API with React
0👍
I’m guessing this is not best practice, but something that I have done as a workaround is to create a few different test programs in the management/commands directory within the app.
https://docs.djangoproject.com/en/1.7/howto/custom-management-commands/
For example, I’m working on an app now that requires some advanced Postgres functionality (cannot use Sqlite) so instead of creating test functions as part of tests.py, I created test_process.py in myapp/management/commands/
- [Django]-How to 'clear' the port when restarting django runserver
- [Django]-Django REST framework: non-model serializer
- [Django]-Rendering a template variable as HTML
0👍
You may want to have pytest
as test runner. Configuration example follows.
Sample pytest.ini
file:
[pytest]
norecursedirs=
*.egg
.git
.tox
.env
_sass
build
dist
migrations
fabfile
.tox
python_files =
test_*.py
tests.py
DJANGO_SETTINGS_MODULE=settings.dev
addopts=
--reuse-db
--nomigrations
--cov=your_app
--ignore=.tox
--ignore=fabfile
--ignore=scripts
--ignore=settings
--ignore=tmp
--cov-report=html
--cov-report=term
--cov-report=annotate
Sample runtests.py
file:
#!/usr/bin/env python
import os
import sys
import pytest
def main():
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings.dev")
return pytest.main()
if __name__ == '__main__':
sys.exit(main())
Sample requirements.txt
file:
pytest==3.0.2
pytest-django==2.9.1
pytest-cov==2.2.1
Run the tests:
./runtests.py
Note, that effect is achieved through reuse-db
and nomigrations
directives.
- [Django]-How can I use Django OAuth Toolkit with Python Social Auth?
- [Django]-How to get primary keys of objects created using django bulk_create
- [Django]-Naming convention for Django URL, templates, models and views
0👍
Current versions of Django have a –keepdb argument you can pass to tests so the database doesn’t get destroyed and rebuilt on each run.
https://docs.djangoproject.com/en/3.0/ref/django-admin/#cmdoption-test-keepdb
- [Django]-Empty Label ChoiceField Django
- [Django]-Django: Does prefetch_related() follow reverse relationship lookup?
- [Django]-Forbidden (403) CSRF verification failed. Request aborted. Reason given for failure: Origin checking failed does not match any trusted origins