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?
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With