Dean Sherwin Dean Sherwin - 1 year ago 114
Python Question

Test variables INSIDE a view function - no return

I'm new to Django + Python so I'm not sure how much of this question will make sense.

Basically, I'm working on a project (not created by me) that has a function within a file. The purpose of this view function is not to give a HTTP response but to send an email.

It accepts a request as a parameter (which contains data that will be looped through - some of it being inserted into the email.

The end of the function does a call to an email_extrals.utils function send_mail_template(various different variables passed in).

My issue is that I want to write a test for this function which essentially returns nothing (that I can see - it just sends an email). This view function is actually called elsewhere in an API.

The purpose of this test is to ensure that if certain variables are present in the request data, then they should be present in a variable which gets passed to the send_email_template() call. So I'm kind of testing for variables inside the function - not what gets returned.

I need a way of somehow accessing the internal variables of the function to test them.

I can't think of a way to test this and at this point I'm banging my head against a wall. Any test info I can find online for views is mainly just testing responses or various model related issues.

Any push in the right direction would be a huge help. A link to docs or a basic example of something similar.


Answer Source

One way to approach this would be to make your own fake version of send_email_template() that does nothing except verify that it was called with the desired argument values, and raises an exception if they are wrong.

Then, in your test setup, you would replace the real send_email_template() with your fake one. This is called Mocking.

And just to be safe you would also want to assert that your fake function was actually called.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download