On 7/31/07, Todd O'Bryan <[EMAIL PROTECTED]> wrote:
>
> Is there an easy way to tell if something is running as a test so that I
> can mock that behavior? I'm thinking of something like
...
> Or maybe this isn't the right approach at all and I'm missing something
> that would make this easier.

Adding an 'if testing' check is probably not the best approch. This
would introduce a distinction in your code between 'real' code and
'test' code; in effect, any code in the 'real' branch would never get
tested. The risk associated with this could be mediated in practice
(by minimizing the code you put in such a conditional), but it would
be better to avoid the problem entirely.

The approach that is taken by the Django test system is to instrument
those parts of Django that are have external effects. For example, if
you have a view that sends notification emails, you don't want each
execution of a test to send those emails, and it would be nice to be
able to check to see which emails were actually sent. To accommodate
this, the Django test framework dynamically replaces the send_mail
function with a mock version during test setup. The email mock object
provides the same interface as the real thing, but puts all sent
messages into local store, rather than posting to SMTP. The end user
code calling send_mail is never aware of the change - and no 'if
testing' code is required.

You can do a similar thing for any calls that have external effects.
Look at setup_test_environment in django.test.utils for details of how
the job is done for email; the same approach should work as a general
technique.

Yours,
Russ Magee %-)

--~--~---------~--~----~------------~-------~--~----~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to