Django version 1.3.
I am new to Django unit testing. I have written my first test but it fails because my login isn’t working. I’m quite sure the reason for this is that the database doesn’t contain my user account which is supposed to be provided by the auth fixture.
The code I’ve written is:
from django.test import TestCase
from django.test.client import Client
class OrderCreateTest(TestCase):
fixtures = ['auth']
def setUp(self):
self.client = Client()
def testSelectProduct(self):
self.assertTrue(self.client.login(user='chris', password='password'),
'Unexpected login failure.')
response = self.client.get('/order/new/')
print response
self.client.logout()
def tearDown(self):
pass
def runTest(self):
pass
The test failure I’m getting is:
Creating test database for alias 'default' ('test_proj')...
Creating tables ...
...
Running post-sync handlers for application admin
...
Installing custom SQL ...
Installing indexes ...
Installing index for admin.LogEntry model
...
Running post-sync handlers for application admin
...
Loading 'initial_data' fixtures...
Checking '/usr/local/lib/python2.7/dist-packages/django/contrib/admin/fixtures' for fixtures...
No xml fixture 'initial_data' in '/usr/local/lib/python2.7/dist-packages/django/contrib/admin/fixtures'.
...
Checking absolute path for fixtures...
No xml fixture 'initial_data' in absolute path.
No xml fixture 'initial_data' in absolute path.
No xml fixture 'initial_data' in absolute path.
No xml fixture 'initial_data' in absolute path.
No json fixture 'initial_data' in absolute path.
No json fixture 'initial_data' in absolute path.
No json fixture 'initial_data' in absolute path.
No json fixture 'initial_data' in absolute path.
No yaml fixture 'initial_data' in absolute path.
No yaml fixture 'initial_data' in absolute path.
No yaml fixture 'initial_data' in absolute path.
No yaml fixture 'initial_data' in absolute path.
No xml fixture 'initial_data' in absolute path.
No xml fixture 'initial_data' in absolute path.
No xml fixture 'initial_data' in absolute path.
No xml fixture 'initial_data' in absolute path.
No json fixture 'initial_data' in absolute path.
No json fixture 'initial_data' in absolute path.
No json fixture 'initial_data' in absolute path.
No json fixture 'initial_data' in absolute path.
No yaml fixture 'initial_data' in absolute path.
No yaml fixture 'initial_data' in absolute path.
No yaml fixture 'initial_data' in absolute path.
No yaml fixture 'initial_data' in absolute path.
No fixtures found.
Destroying test database for alias 'default' ('test_proj')...
testSelectProduct (orpheus.orders.tests.ordercreate.OrderCreateTest) ... FAIL
======================================================================
FAIL: testSelectProduct (orpheus.orders.tests.ordercreate.OrderCreateTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/path/to/ordercreate.py", line 24, in testSelectProduct
'Unexpected login failure.')
AssertionError: Unexpected login failure.
----------------------------------------------------------------------
Ran 1 test in 2376.829s
FAILED (failures=1)
Destroying test database for alias 'default' ('test_proj')...
Snippet from my settings with the db:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'project',
'USER': 'project',
'PASSWORD': 'projectdb',
'TEST_NAME': 'test_project' } }
I’ve stepped through the test code and I can see that it’s finding my fixture auth and it’s saving the data it finds… somewhere. I’ve suspended the test to drop into mysql and queried the relevant tables but they all stay empty (the test code has been skipping the sections dealing with transactions so I’m confident that i’m not supposed to wait for a commit).
What am I doing or not doing that keeps my fixtures from loading correctly?
EDIT: I had the brilliant idea to test loading the fixture and it succeeded:
$ ./manage.py loaddata auth
Installed 8 object(s) from 1 fixture(s)
However, this doesn’t give me any ideas on how to fix the fixture loading.
EDIT: supervacuo provided a clue when he suggested I try this:
./manage.py testserver -v 2 auth.json
The output is largely the same except that it correctly loads the fixture (mysql confirmed it):
...
Installed 8 object(s) from 1 fixture(s)
Validating models...
0 errors found
Django version 1.3, using settings 'project.settings'
Running django-devserver 0.1.4
I don’t know where to go from there.
Problem was completely different from what it looked like. In my test I wrote:
When the keyword arg should instead be
username. I followed the red herring of missing data in the database, which is explained by transactions.Thanks to mlavin in #django for the help.