Search code examples
pythontestingpylonspyramidunit-testing

unit test to check if for a given path a correct context will be returned


Just like in the title. I have a model that I can test manually. I enter url in a browser and receive a result form one of the views. Thing is unittest should be doing that.

I think there should be some way to create a request, send it to the application and in return receive the context.


Solution

  • You can create functional tests using the WebTest package, which allows you to wrap your WSGI application in a TestApp that supports .get(), .post(), etc.

    See http://docs.pylonsproject.org/projects/pyramid/1.0/narr/testing.html#creating-functional-tests for specifics in Pyramid, pasted here for posterity:

    import unittest
    
    class FunctionalTests(unittest.TestCase):
        def setUp(self):
            from myapp import main
            app = main({})
            from webtest import TestApp
            self.testapp = TestApp(app)
    
        def test_root(self):
            res = self.testapp.get('/', status=200)
            self.failUnless('Pyramid' in res.body)