16

What's the best way to perform Unit Testing for a RESTful API that includes email functionality (lost passwords, user activation) in Python?

Everything is done via HTTP POST / GET and at this time, authentication isn't involved.

Should I just use the requests library and manually do everything I want? Is it possible to use requests to automate the parts of my Unit Testing that involves email?

2
  • 1
    You should unit tests your handlers without the HTTP overhead first. Everything else is not really a unit test but rather some kind of integration test. Commented May 18, 2015 at 19:26
  • 2
    if you use http, it's not a unit test. generally, if you test anything else than one single class, in the case of a rest api all the url routing, data handling whatsoever - it's not a unit test. Commented Apr 10, 2016 at 17:25

1 Answer 1

16

Often the web framework that you use to implement the REST api will also offer unit testing support. For example:

These test classes are shortcuts which plug the request directly into the framework's Url dispatcher. That saves you the hassle of finding a free port, spawning a "real" server and connecting the http client from your unit test.

As for the e-mail sending: I would mock that part in the TestCase.setUp method. Just change the reference to the e-mail sending module / class to another module/class which loops the outgoing e-mail back to the unit test for evaluation rather than e-mailing.

Sign up to request clarification or add additional context in comments.

2 Comments

These tools might be helpful if you have a WSGI app but the framework doesn't have good unit testing support: wsgi.readthedocs.io/en/latest/testing.html.
@Freek Wiekmeijer, the Flask URL is broken.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.