oneself
oneself

Reputation: 40321

Write unit tests for restish in Python

I'm writing a RESTful API in Python using the restish framework. I would like to write some unit tests (using the unittest package, for example), that will make different requests to my application and validate the results. The unit tests should be able to run as-is, without needing to start a separate web-server process. How do I set up a mock environment using restish to do this?

Thanks

Upvotes: 1

Views: 409

Answers (3)

oneself
oneself

Reputation: 40321

Restish has a built in TestApp class that can be used to test restish apps. Assuming you have a "test" dir in your root restish project callte "restest" created with paster.

import os
import unittest
from paste.fixture import TestApp

class RootTest (unittest.TestCase):

  def setUp(self):
    self.app = TestApp('config:%s/../development.ini' % os.path.dirname(os.path.abspath(__file__)))

  def tearDown(self):
    self.app = None

  def test_html(self):
    res = self.app.get('/')
    res.mustcontain('Hello from restest!')

if __name__ == '__main__':
    unittest.main()

Upvotes: 1

ars
ars

Reputation: 123568

Since restish is a WSGI framework, you can take advantage of any one of a number of WSGI testing tools:

At least a few of those tools, such as Twill, should be able to test your application without starting a separate web server. (For example, see the "Testing WSGI Apps with Twill" link for more details.)

You might want to ask on the restish forum/list if they have a preferred tool for this type of thing.

Upvotes: 1

Koen Bok
Koen Bok

Reputation: 3284

I test everything using WebTest and NoseTests and I can strongly recommend it. It's fast, flexible and easy to set up. Just pass it your wsgi function and you're good to go.

Upvotes: 1

Related Questions