Friday, April 29, 2011

Re: Best way to tell if we're in a test

I think you might be taking the wrong approach. Rather than having your application code checking whether or not it's in a test, have your testing code disable/mock certain behaviors that you don't want exercised(like have it always return that the user answered the security question correctly or something).

Python's dynamic nature makes it easy to monkey patch things like this. Look at the Mock class: http://python-mock.sourceforge.net/

On Fri, Apr 29, 2011 at 12:53 PM, Jody McIntyre <JodyM@trustcentric.com> wrote:
What's the best way to tell (from regular code) if we're in a test?  I have added extra authentication steps (security questions) that would be difficult to deal with in tests where you just want to run self.client.login() or post to the login URL.  I've noticed that several third party modules, such as django-digest and django-bcrypt, also disable parts of their functionality during testing like this:

from django.core import mail

if hasattr(mail, 'outbox'):
    # we are under a test
else:
    # not under a test

This works, but it's an undocumented side effect so I'm worried it might change in a future version.  What's the correct way to tell if we're in a test?

Thanks,
Jody

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment