Discussion:
Database for Unit Test
Alexander Lamas
2018-08-22 06:46:08 UTC
Permalink
Hi guys,

Does anyone know how to setup a Django Rest Framework Web Api application*,
maybe settings.py*,
to create it's own testing database on the fly and then dropping the
database when unit tests are finished?

Does it require a special setting somewhere?

Or do I need to have a dev_settings.py / unittest_settings.py with specific
settings?

NOTE: I'm running my unit tests via Microsoft Visual Studio.

Thank you very much!

Regards,
Alex
--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Carlton Gibson
2018-08-22 06:55:08 UTC
Permalink
Post by Alexander Lamas
NOTE: I'm running my unit tests via Microsoft Visual Studio.
Configure this to use `django-admin test` (or py.test or 
). django-admin takes care of creating the test database for you. (And setting up the Django environment, and everything else.)

See: https://docs.djangoproject.com/en/2.1/topics/testing/ <https://docs.djangoproject.com/en/2.1/topics/testing/>

These questions aren’t really Django REST Framework specific.
--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Marc Chakiachvili
2018-08-22 07:02:02 UTC
Permalink
Hi Alex. Usually, unit tests create their own database for each test ran.
This is automatic with django tests.

Read more there:
https://docs.djangoproject.com/en/2.1/topics/testing/overview/

You can use settings.py to set up a test db, and set dba speaks for test :
if 'test' in sys.argv:
Xxx

if you need prepopulated data, use fixtures.
https://code.djangoproject.com/wiki/Fixtures

Then destroy it at the end. That's called 'isolation', then you can run
tests in any order and even in parallel sometime.

Marc.
Post by Alexander Lamas
Hi guys,
Does anyone know how to setup a Django Rest Framework Web Api application*,
maybe settings.py*,
to create it's own testing database on the fly and then dropping the
database when unit tests are finished?
Does it require a special setting somewhere?
Or do I need to have a dev_settings.py / unittest_settings.py with
specific settings?
NOTE: I'm running my unit tests via Microsoft Visual Studio.
Thank you very much!
Regards,
Alex
--
You received this message because you are subscribed to the Google Groups
"Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Alexander Lamas
2018-08-24 01:01:17 UTC
Permalink
Hi Marc,

Thank you very much for your reply.

For some reason my unit tests is not creating it's own database.
When I was running the unit tests via DOS prompt yes, it was creating the
database on the fly, but now, I'm running via Visual Studio Unit Tests
framework.

Do I have to change some settings somewhere?

Thanks again mate!

Cheers,
Alex
Post by Marc Chakiachvili
Hi Alex. Usually, unit tests create their own database for each test ran.
This is automatic with django tests.
https://docs.djangoproject.com/en/2.1/topics/testing/overview/
Xxx
if you need prepopulated data, use fixtures.
https://code.djangoproject.com/wiki/Fixtures
Then destroy it at the end. That's called 'isolation', then you can run
tests in any order and even in parallel sometime.
Marc.
Post by Alexander Lamas
Hi guys,
Does anyone know how to setup a Django Rest Framework Web Api application*,
maybe settings.py*,
to create it's own testing database on the fly and then dropping the
database when unit tests are finished?
Does it require a special setting somewhere?
Or do I need to have a dev_settings.py / unittest_settings.py with
specific settings?
NOTE: I'm running my unit tests via Microsoft Visual Studio.
Thank you very much!
Regards,
Alex
--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Marc Chakiachvili
2018-08-24 08:19:29 UTC
Permalink
Sorry, don't know about Visual Studio for Django / Python dev.
Usually use Jetbrains pycharm..

IF it's same code executed, have a look at the command line (if possible)
that actually launch VIsual Studio on test launch. did you paer chance set
any parameter such as keepdb ? (Which disable the db creation / updates)

Marc
Post by Alexander Lamas
Hi Marc,
Thank you very much for your reply.
For some reason my unit tests is not creating it's own database.
When I was running the unit tests via DOS prompt yes, it was creating the
database on the fly, but now, I'm running via Visual Studio Unit Tests
framework.
Do I have to change some settings somewhere?
Thanks again mate!
Cheers,
Alex
Post by Marc Chakiachvili
Hi Alex. Usually, unit tests create their own database for each test ran.
This is automatic with django tests.
https://docs.djangoproject.com/en/2.1/topics/testing/overview/
Xxx
if you need prepopulated data, use fixtures.
https://code.djangoproject.com/wiki/Fixtures
Then destroy it at the end. That's called 'isolation', then you can run
tests in any order and even in parallel sometime.
Marc.
Post by Alexander Lamas
Hi guys,
Does anyone know how to setup a Django Rest Framework Web Api application*,
maybe settings.py*,
to create it's own testing database on the fly and then dropping the
database when unit tests are finished?
Does it require a special setting somewhere?
Or do I need to have a dev_settings.py / unittest_settings.py with
specific settings?
NOTE: I'm running my unit tests via Microsoft Visual Studio.
Thank you very much!
Regards,
Alex
--
You received this message because you are subscribed to the Google Groups
"Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Alexander Lamas
2018-08-24 08:28:10 UTC
Permalink
Hi Marc,

No, this is my database settings

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'mydb',
'USER': 'root',
'PASSWORD': 'masterkey',
'HOST': 'localhost',
'PORT': '3306',
'TEST': {'NAME' : 'test_mydb'},
}}


Also, how can I set up my ALLOWED_HOSTS differently for unit test purpose
and for dev purpose?

For example:

This is for my unit tests

ALLOWED_HOSTS = ['testserver']


and this is for my normal dev tests

ALLOWED_HOSTS = []


Is there any directive to tell you are running unit tests or not?

Thank you!

Alex
Post by Marc Chakiachvili
Sorry, don't know about Visual Studio for Django / Python dev.
Usually use Jetbrains pycharm..
IF it's same code executed, have a look at the command line (if possible)
that actually launch VIsual Studio on test launch. did you paer chance set
any parameter such as keepdb ? (Which disable the db creation / updates)
Marc
Post by Alexander Lamas
Hi Marc,
Thank you very much for your reply.
For some reason my unit tests is not creating it's own database.
When I was running the unit tests via DOS prompt yes, it was creating the
database on the fly, but now, I'm running via Visual Studio Unit Tests
framework.
Do I have to change some settings somewhere?
Thanks again mate!
Cheers,
Alex
Post by Marc Chakiachvili
Hi Alex. Usually, unit tests create their own database for each test
ran. This is automatic with django tests.
https://docs.djangoproject.com/en/2.1/topics/testing/overview/
Xxx
if you need prepopulated data, use fixtures.
https://code.djangoproject.com/wiki/Fixtures
Then destroy it at the end. That's called 'isolation', then you can run
tests in any order and even in parallel sometime.
Marc.
Post by Alexander Lamas
Hi guys,
Does anyone know how to setup a Django Rest Framework Web Api
application*, maybe settings.py*,
to create it's own testing database on the fly and then dropping the
database when unit tests are finished?
Does it require a special setting somewhere?
Or do I need to have a dev_settings.py / unittest_settings.py with
specific settings?
NOTE: I'm running my unit tests via Microsoft Visual Studio.
Thank you very much!
Regards,
Alex
--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Marc Chakiachvili
2018-08-24 11:00:00 UTC
Permalink
depends if you run tests with python command or via manage.py tests command
I think.

BTW: Usually DEBUG parameter in settings.py make you don't care about
ALLOWED_HOST param.

Marc
Post by Alexander Lamas
Hi Marc,
No, this is my database settings
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'mydb',
'USER': 'root',
'PASSWORD': 'masterkey',
'HOST': 'localhost',
'PORT': '3306',
'TEST': {'NAME' : 'test_mydb'},
}}
Also, how can I set up my ALLOWED_HOSTS differently for unit test purpose
and for dev purpose?
This is for my unit tests
ALLOWED_HOSTS = ['testserver']
and this is for my normal dev tests
ALLOWED_HOSTS = []
Is there any directive to tell you are running unit tests or not?
Thank you!
Alex
Post by Marc Chakiachvili
Sorry, don't know about Visual Studio for Django / Python dev.
Usually use Jetbrains pycharm..
IF it's same code executed, have a look at the command line (if possible)
that actually launch VIsual Studio on test launch. did you paer chance set
any parameter such as keepdb ? (Which disable the db creation / updates)
Marc
Post by Alexander Lamas
Hi Marc,
Thank you very much for your reply.
For some reason my unit tests is not creating it's own database.
When I was running the unit tests via DOS prompt yes, it was creating
the database on the fly, but now, I'm running via Visual Studio Unit Tests
framework.
Do I have to change some settings somewhere?
Thanks again mate!
Cheers,
Alex
Post by Marc Chakiachvili
Hi Alex. Usually, unit tests create their own database for each test
ran. This is automatic with django tests.
https://docs.djangoproject.com/en/2.1/topics/testing/overview/
Xxx
if you need prepopulated data, use fixtures.
https://code.djangoproject.com/wiki/Fixtures
Then destroy it at the end. That's called 'isolation', then you can run
tests in any order and even in parallel sometime.
Marc.
Post by Alexander Lamas
Hi guys,
Does anyone know how to setup a Django Rest Framework Web Api
application*, maybe settings.py*,
to create it's own testing database on the fly and then dropping the
database when unit tests are finished?
Does it require a special setting somewhere?
Or do I need to have a dev_settings.py / unittest_settings.py with
specific settings?
NOTE: I'm running my unit tests via Microsoft Visual Studio.
Thank you very much!
Regards,
Alex
--
You received this message because you are subscribed to the Google Groups
"Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Alex Daigle
2018-08-24 15:56:59 UTC
Permalink
I’ve had fairly good success with setting up Django tests with ‘model mommy’, aka ‘mommy’. I highly recommend looking for it and looking around for other similar libraries. It makes setting up and tearing down cases much easier.

Sent from my iPhone
depends if you run tests with python command or via manage.py tests command I think.
BTW: Usually DEBUG parameter in settings.py make you don't care about ALLOWED_HOST param.
Marc
Post by Alexander Lamas
Hi Marc,
No, this is my database settings
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'mydb',
'USER': 'root',
'PASSWORD': 'masterkey',
'HOST': 'localhost',
'PORT': '3306',
'TEST': {'NAME' : 'test_mydb'},
}
}
Also, how can I set up my ALLOWED_HOSTS differently for unit test purpose and for dev purpose?
This is for my unit tests
ALLOWED_HOSTS = ['testserver']
and this is for my normal dev tests
ALLOWED_HOSTS = []
Is there any directive to tell you are running unit tests or not?
Thank you!
Alex
Post by Marc Chakiachvili
Sorry, don't know about Visual Studio for Django / Python dev.
Usually use Jetbrains pycharm..
IF it's same code executed, have a look at the command line (if possible) that actually launch VIsual Studio on test launch. did you paer chance set any parameter such as keepdb ? (Which disable the db creation / updates)
Marc
Post by Alexander Lamas
Hi Marc,
Thank you very much for your reply.
For some reason my unit tests is not creating it's own database.
When I was running the unit tests via DOS prompt yes, it was creating the database on the fly, but now, I'm running via Visual Studio Unit Tests framework.
Do I have to change some settings somewhere?
Thanks again mate!
Cheers,
Alex
Hi Alex. Usually, unit tests create their own database for each test ran. This is automatic with django tests.
Read more there: https://docs.djangoproject.com/en/2.1/topics/testing/overview/
Xxx
if you need prepopulated data, use fixtures. https://code.djangoproject.com/wiki/Fixtures
Then destroy it at the end. That's called 'isolation', then you can run tests in any order and even in parallel sometime.
Marc.
Post by Alexander Lamas
Hi guys,
Does anyone know how to setup a Django Rest Framework Web Api application, maybe settings.py,
to create it's own testing database on the fly and then dropping the database when unit tests are finished?
Does it require a special setting somewhere?
Or do I need to have a dev_settings.py / unittest_settings.py with specific settings?
NOTE: I'm running my unit tests via Microsoft Visual Studio.
Thank you very much!
Regards,
Alex
--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Continue reading on narkive:
Loading...