2

I've got a problem using a login decorator in my django tests. Many tests start with

self.client.login(username='foo', password='bar')

So, the login works in principle; But now I like to refactor this line of code into a decorator login, just for fun:

def login(fn):
    def wrapper(self):
        self.client.login(username='foo', password='bar')
        return fn(self)
    return wrapper

But the then failing tests show me that the user 'foo' is not logged in even self.client.login returns True.

One more mysterious thing is, when I now leave self.client.login from the decorator such that the decorator does nothing but wrapping, a normal login from within the decorated test method is then not possible anymore also!

I think there is some context or scope problem with the test client. Have you any idea what the probem can be? P.S.: The story is all about python2.7 and django-1.3.1.

hoffmaje
  • 140
  • 8

1 Answers1

3

Please see python-unittest-cant-call-decorated-test

ALTERNATIVES?

Since it was always the same user, I had it in the setUp.

class SimpleTest(TestCase):
    def setUp(self):
        self.client.login(username='foo', password='bar')

Here is an interesting gist - Lazy man's Django testcase that allows scoped logins.

Community
  • 1
  • 1
sfossen
  • 4,774
  • 24
  • 18