python - Django user setup for nose tests -
i tried seting user nose tests doesnot work
in global scope defined:
from django.contrib.auth.models import user import nose.tools noz
inside test class defined:
def setup(self): self.client = client() user = user(username="test", password="test") user.save()
the user saved, have tested noz.set_trace() when test function calls same user's login, assertion error raised:
nosetests --verbosity 1 creating test database alias 'default'... > <app-path>/tests.py(59)testtestuser() -> response = self.client.login(username=u'test', password=u'test') (pdb) user.objects.all() [<user: test>]
the testtestuser function defined like:
def testtestuser(self): """ tests if django user 'test' setup properly.""" noz.set_trace() # login test user response = self.client.login(username='test', password='test') noz.assert_equal(response, true)
relevant test output :
noz.assert_equal(response, true) assertionerror: false != true ---------------------------------------------------------------------- ran 1 test in 0.011s failed (failures=1)
my intention test views have requst.user.is_authenicated() branch.
figured out : http://www.pukkared.com/2011/07/simulating-user-login-in-a-django-view-unit-test/
the proper code is:
def setup(self): self.client = client() user = user.objects.create_user(username="test", password="test") def testtestuser(self): """ tests if django user 'test' setup properly.""" noz.set_trace() # login test user response = self.client.login(username='test', password='test') noz.assert_equal(response, true)
Comments
Post a Comment