MichaelJK
MichaelJK

Reputation: 17

Users not created correctly in Django test case

Im running a simple unit test on getting tastypie API keys from users. To do this, I am trying to create a new user in the test case who I authenticate and fetch their API key. The code for the test case is as follows:

def setup(self):
    user = User.objects.create(username='test1',password='pass')


 def test_user_api_key_fetch(self):
    user = User.objects.get(username='test1')
    c = Client()
    response = c.post('/login/',{'username':'test1','password':'pass'})
    print response.content
    self.assertTrue(response.status_code == 200)

This test fails at the first line, asserting that no user can be found. I have checked the User.objects.all() and it is empty. The problem is not solved by changing setup() to include user.save() or using User.objects.create_user() instead. Why cant this user be found?

Upvotes: 0

Views: 214

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599788

The user is not being created, because the setup method is not being run. It should be called setUp - note the upper-case U. See the Python documentation.

Upvotes: 1

Related Questions