Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 408
Josh Usre Avatar asked May 18 '15 18:05

Josh Usre


People also ask

Can pytest be used for API testing?

For python based projects, pytest is a commonly used package for writing unit tests and for verifying the health of the API against mocked data.


1 Answers

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

  • Flask: http://flask.pocoo.org/docs/latest/testing/
  • Django: http://django-testing-docs.readthedocs.org/en/latest/views.html

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.

like image 191
Freek Wiekmeijer Avatar answered Sep 17 '22 13:09

Freek Wiekmeijer