Recently have been using Django and can’t quite wrap my head around testing within Django.
- When I run
python manage.py test
the application runs 0 tests. - When I run
python manage.py test api.tests.tests
it returnsGot an error creating the test database: permission denied to create database
. My app name is api (I have 2 other apps as well). The app has a folder tests with a file tests.py. - How can I add multiple testing files and folders in the future if I want to create a large test suite and organise it? I have had problems with Django not recognising them.
- I use PyCharm for editing and when I use PyCharm to run unit tests, it does execute it and prints an output but it also gives this error.
django.core.exceptions.ImproperlyConfigured: settings.DATABASES is improperly configured. Please supply the ENGINE value. Check settings documentation for more details.
However, I open the shell with python manage.py shell
and I am sure the engine value is there and the database is there. I can create objects with the shell as well.
Please help in solving multitudes of problems which I feel are all linked.
This is my test file at the current stage:
from rest_framework.test import APITestCase, APIRequestFactory, force_authenticate
from rest_framework import status
from django.contrib.auth.models import User
import api.endpoints.analysis as a
class TestCase(APITestCase):
def test_1(self):
data_input = {"ticker": "AAPL"}
request = APIRequestFactory().post('/api/analysis', data_input, format='json')
# user = User.objects.get(username='test')
force_authenticate(request, None)
response = a.AnalysisList.as_view()(request)
print(response.status_code)
# class AnalysisTests(APITestCase):
# def setUp(self):
# self.factory = APIRequestFactory()
# self.user = User.objects.create(username='test', email='test@example.com', password='lukas')
# self.analysis_list_view = a.AnalysisList.as_view()
# self.input = {"ticker": "AAPL"}
# # request = factory.get('/accounts/django-superstars/')
# # force_authenticate(request, user=user)
# # response = analysis_list_view(request)
#
# # Creation
# def test_create_analysis_simple(self):
# request = self.factory.post('/api/analysis/', self.input, format='json')
# force_authenticate(request, user=self.user)
# response = self.analysis_list_view(request)
# self.assertEquals(response.status_code, status.HTTP_201_CREATED)
My database settings:
DATABASES = {}
if DEBUG:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'qlocal',
'USER': 'qlocaluser',
'PASSWORD': '*',
'HOST': 'localhost',
'PORT': '',
'TEST': {
'NAME': 'test_finance',
},
}
}
else:
DATABASES = {
'default': dj_database_url.config(conn_max_age=600, ssl_require=True),
}