Josh Usre
Josh Usre

Reputation: 683

Unit Testing for a Python RESTful API

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?

Upvotes: 16

Views: 29468

Answers (1)

Freek Wiekmeijer
Freek Wiekmeijer

Reputation: 4950

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.

Upvotes: 16

Related Questions