单元测试,以检查对于给定的路径是否将返回正确的上下文

|| 就像标题中一样。我有一个可以手动测试的模型。我在浏览器中输入url,并从其中一种视图接收结果。事情是单元测试应该这样做。 我认为应该有某种方法可以创建请求,将请求发送到应用程序并返回接收上下文。     
已邀请:
您可以使用WebTest软件包创建功能测试,该软件包允许您将WSGI应用程序包装在支持
.get()
.post()
等的ѭ0中。 有关金字塔中的详细信息,请参见http://docs.pylonsproject.org/projects/pyramid/1.0/narr/testing.html#creating-functional-tests,此处粘贴是为后代:
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)
    
金字塔并没有真正公开测试真实请求并接收有关内部信息的方法。您可以使用以下命令自己执行遍历器:
from pyramid.traversal import traverse

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

context = out[\'context\']
但是,测试有点人为。使用功能测试来检查返回的页面是否符合您的期望会更相关。     

要回复问题请先登录注册