开发者

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 vie开发者_如何学Gows. 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.


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)


Pyramid doesn't really expose a method for testing a real request and receiving information about the internals. You possible execute the traverser yourself using:

from pyramid.traversal import traverse

app = get_app(...)
root = get_root(app)
out = traverse(root, '/my/test/path')

context = out['context']

However, the test is a bit contrived. It'd be more relevant to use a functional test that checks if the returned page is what you expect.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜