List Info

Thread: Is this a test? Is this only a test?




Is this a test? Is this only a test?
country flaguser name
United States
2007-07-30 22:47:38
Some of my pages fetch data based on user input. It
obviously doesn't
make sense to do the actual fetch in test cases, since I
should know
what is going to be returned and should be checking for what
I do with
the data.

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

def fetch_data_for(input):
    if TEST:
        return mock_data(input)
    else:
        #do the production stuff

Or maybe this isn't the right approach at all and I'm
missing something
that would make this easier.

Thanks,
Todd


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


Re: Is this a test? Is this only a test?
user name
2007-07-30 23:09:28
On 7/31/07, Todd O'Bryan <toddobryanmac.com> 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-usersgooglegroups.com
To unsubscribe from this group, send email to
django-users-unsubscribegooglegroups.com
For more options, visit this group at htt
p://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---


[1-2]

about | contact  Other archives ( Real Estate discussion Medical topics )