以正确的方式设置 Pyramid 1.5 测试
Setting up Pyramid 1.5 testing the right way
虽然 Pyramid 文档通常非常好,但我找不到如何设置 integration tests for a Pyramid 1.5 application(注意缺少必要的代码片段!)。即,将特定配置放在哪里。
在__init__中我有main()函数,它包含一些其他模块,其中有一个需要SQLAlchemy。另一个复杂的问题是 auth 策略依赖于设置,所以所有内容都在 main 中(否则我做了 includeme,不确定它是否与这个问题相关)。
起初我尝试了以下方法(在收集了我能做的 includeme 函数之后):
class ViewTests(unittest.TestCase):
def setUp(self):
self.config = testing.setUp()
self.config.include('myproject.includeme')
这当然在 sqlalchemy 的某处失败了 engine_from_config。
但是后来我 found,我可能可以使用 main() 并提供 test.ini 进行配置。我仍然喜欢要点的全包方法:
class IntegrationTestBase(BaseTestCase):
@classmethod
def setUpClass(cls):
cls.app = main({}, **settings)
super(IntegrationTestBase, cls).setUpClass()
def setUp(self):
self.app = webtest.TestApp(self.app)
self.config = testing.setUp()
super(IntegrationTestBase, self).setUp()
上面代码的部分问题是设置没有在 self.config 中结束。如果我用 self.config = testing.setUp(settings=settings)
增强它,测试仍然失败:
AttributeError: 'DummyRequest' object has no attribute 'include'
pyramid_bowerstatic 没有机会修改请求等。因此,与其进行开箱即用的集成测试(应用程序本身 运行 没有抱怨!)给定配置并专注于编写测试,我需要关心所有第三方模块的突发奇想。
因此,我希望有一些更好的方法来进行集成测试,这些测试处理几个包含的包、自定义身份验证策略、事件和其他 "magic"。
Py.test 如果重要的话,可以使用。 pytest_pyramid 似乎是相关的,但文档没有任何示例。
不完全是,但类似问题:How to Make view_config decorator work with a Pyramid Unit Test?,尤其是在对答案的评论中。
我找到了日语的临时解决方案(目前对我有用):http://qiita.com/podhmo/items/2c6d8cb78c1f84569f0a
但问题是如何可靠地设置集成测试,等同于所有方面的应用程序,而不查看第三方模块,特定的 ini 文件除外?
更新:这是有问题的调用之一:
查看:
components = pyramid_bowerstatic.create_components('sbadmin',
os.path.join(os.path.dirname(__file__), 'bower_components'))
class MainViews(Layouts):
@view_config(route_name='home', renderer='templates/mytemplate.pt')
def my_view(self):
self.request.include(components, 'jquery')
return {'project': 'Myproject'}
测试中:
class ViewTests(IntegrationTestBase):
def test_my_view(self):
from myproject.views.main import MainViews
request = self._make_request(self.config) # before was: DummyRequest
info = MainViews(request).my_view()
self.assertEqual(info['project'], 'Myproject')
由于 Pyramid 的解耦架构很好,可能有全包请求就足够了。也就是说,问题可以重新表述为:什么是通用 _make_request 函数,它给出的结果与 运行ning 应用程序请求相同(具有扩展、补间、添加的请求属性,包括来自第 3 方模块的那些)?有现成的工厂吗? 恕我直言,如果开发人员需要模拟它自己的 "integration" 来测试与采用应用程序的真实情况,那么这不是集成测试。我不确定这些是否是唯一的东西,但我想工厂至少应该提供一个包含所有钩子的请求,就像这里提到的:http://docs.pylonsproject.org/projects/pyramid/en/latest/narr/hooks.html 不管哪个包含的包添加了它们。
更新 2:我想到,测试和 运行 时间环境之间存在差异的第一个近似指标非常明确。我比较了对视图的正常调用和测试调用的请求。首先,这是我的 _make_request,这足以添加请求方法,但还缺少更多(下):
def _make_request(self, config, path="/"):
from pyramid.request import Request
from pyramid.interfaces import IRequestExtensions
request = Request.blank(path)
extensions = config.registry.getUtility(IRequestExtensions)
request.registry = config.registry
request._set_extensions(extensions)
return request
普通调用具有以下属性(request.__dict__.keys()):
['traversed',
'virtual_root',
'virtual_root_path',
'subpath',
'request_iface',
'__view__',
'view_name',
'tm',
'environ',
'registry',
'context',
'matched_route',
'get_bowerstatic_path',
'include',
'root',
'matchdict',
'invoke_subrequest']
并且只测试调用这些:
['environ',
'registry',
'get_bowerstatic_path',
'include']
这清楚地表明,我的方法还不够,我会遇到使用其他请求功能的视图的问题。使用上面的 _make_request,只有最小视图通过。
换句话说,如何获得相同的请求,在功能测试中使用,例如在 webtest 的 TestApp 中,但不是执行 testapp.get(...),而是使用该请求调用视图并对可调用的返回结果进行断言(未呈现为 HTML)?
由于 RTD 上的临时错误,缺少该基本代码片段。 An Issue has been logged.
这是未包含的代码的 link。
https://github.com/Pylons/pyramid/blob/master/docs/narr/MyProject/myproject/tests.py#L19-L43
[已编辑]
以下是金字塔炼金术支架的两个测试示例。每个都有自己的风格。希望您发现其中之一对您的方案有用。
如果你安装了 Pyramid,你可以从炼金术脚手架创建一个示例项目。
$ $VENV/bin/pcreate -s alchemy tutorial
如果该命令不适合您,SQLAlchemy + URL Dispatch Wiki Tutorial 会详细介绍和先决条件。
脚手架包括这些tests.
我们正在开发一个更新此脚手架的新分支,目标是在 Pyramid 1.7 中进行合并。这是它的 tests.
如果您 git 签出该分支,并且 运行 通过教程步骤,您可以看到完整的上下文。
[已编辑]
这里还有一个值得一看的地方:Pyramid's own integration tests。
顺便说一句,我很难给你一个合适的答案。对于初学者,我看不到 DummyRequest
在您的测试代码中被调用的位置。你能在你的问题中给出一个完整的可重现的例子吗?
虽然 Pyramid 文档通常非常好,但我找不到如何设置 integration tests for a Pyramid 1.5 application(注意缺少必要的代码片段!)。即,将特定配置放在哪里。
在__init__中我有main()函数,它包含一些其他模块,其中有一个需要SQLAlchemy。另一个复杂的问题是 auth 策略依赖于设置,所以所有内容都在 main 中(否则我做了 includeme,不确定它是否与这个问题相关)。
起初我尝试了以下方法(在收集了我能做的 includeme 函数之后):
class ViewTests(unittest.TestCase):
def setUp(self):
self.config = testing.setUp()
self.config.include('myproject.includeme')
这当然在 sqlalchemy 的某处失败了 engine_from_config。 但是后来我 found,我可能可以使用 main() 并提供 test.ini 进行配置。我仍然喜欢要点的全包方法:
class IntegrationTestBase(BaseTestCase):
@classmethod
def setUpClass(cls):
cls.app = main({}, **settings)
super(IntegrationTestBase, cls).setUpClass()
def setUp(self):
self.app = webtest.TestApp(self.app)
self.config = testing.setUp()
super(IntegrationTestBase, self).setUp()
上面代码的部分问题是设置没有在 self.config 中结束。如果我用 self.config = testing.setUp(settings=settings)
增强它,测试仍然失败:
AttributeError: 'DummyRequest' object has no attribute 'include'
pyramid_bowerstatic 没有机会修改请求等。因此,与其进行开箱即用的集成测试(应用程序本身 运行 没有抱怨!)给定配置并专注于编写测试,我需要关心所有第三方模块的突发奇想。
因此,我希望有一些更好的方法来进行集成测试,这些测试处理几个包含的包、自定义身份验证策略、事件和其他 "magic"。
Py.test 如果重要的话,可以使用。 pytest_pyramid 似乎是相关的,但文档没有任何示例。
不完全是,但类似问题:How to Make view_config decorator work with a Pyramid Unit Test?,尤其是在对答案的评论中。
我找到了日语的临时解决方案(目前对我有用):http://qiita.com/podhmo/items/2c6d8cb78c1f84569f0a
但问题是如何可靠地设置集成测试,等同于所有方面的应用程序,而不查看第三方模块,特定的 ini 文件除外?
更新:这是有问题的调用之一:
查看:
components = pyramid_bowerstatic.create_components('sbadmin',
os.path.join(os.path.dirname(__file__), 'bower_components'))
class MainViews(Layouts):
@view_config(route_name='home', renderer='templates/mytemplate.pt')
def my_view(self):
self.request.include(components, 'jquery')
return {'project': 'Myproject'}
测试中:
class ViewTests(IntegrationTestBase):
def test_my_view(self):
from myproject.views.main import MainViews
request = self._make_request(self.config) # before was: DummyRequest
info = MainViews(request).my_view()
self.assertEqual(info['project'], 'Myproject')
由于 Pyramid 的解耦架构很好,可能有全包请求就足够了。也就是说,问题可以重新表述为:什么是通用 _make_request 函数,它给出的结果与 运行ning 应用程序请求相同(具有扩展、补间、添加的请求属性,包括来自第 3 方模块的那些)?有现成的工厂吗? 恕我直言,如果开发人员需要模拟它自己的 "integration" 来测试与采用应用程序的真实情况,那么这不是集成测试。我不确定这些是否是唯一的东西,但我想工厂至少应该提供一个包含所有钩子的请求,就像这里提到的:http://docs.pylonsproject.org/projects/pyramid/en/latest/narr/hooks.html 不管哪个包含的包添加了它们。
更新 2:我想到,测试和 运行 时间环境之间存在差异的第一个近似指标非常明确。我比较了对视图的正常调用和测试调用的请求。首先,这是我的 _make_request,这足以添加请求方法,但还缺少更多(下):
def _make_request(self, config, path="/"):
from pyramid.request import Request
from pyramid.interfaces import IRequestExtensions
request = Request.blank(path)
extensions = config.registry.getUtility(IRequestExtensions)
request.registry = config.registry
request._set_extensions(extensions)
return request
普通调用具有以下属性(request.__dict__.keys()):
['traversed',
'virtual_root',
'virtual_root_path',
'subpath',
'request_iface',
'__view__',
'view_name',
'tm',
'environ',
'registry',
'context',
'matched_route',
'get_bowerstatic_path',
'include',
'root',
'matchdict',
'invoke_subrequest']
并且只测试调用这些:
['environ',
'registry',
'get_bowerstatic_path',
'include']
这清楚地表明,我的方法还不够,我会遇到使用其他请求功能的视图的问题。使用上面的 _make_request,只有最小视图通过。
换句话说,如何获得相同的请求,在功能测试中使用,例如在 webtest 的 TestApp 中,但不是执行 testapp.get(...),而是使用该请求调用视图并对可调用的返回结果进行断言(未呈现为 HTML)?
由于 RTD 上的临时错误,缺少该基本代码片段。 An Issue has been logged.
这是未包含的代码的 link。 https://github.com/Pylons/pyramid/blob/master/docs/narr/MyProject/myproject/tests.py#L19-L43
[已编辑]
以下是金字塔炼金术支架的两个测试示例。每个都有自己的风格。希望您发现其中之一对您的方案有用。
如果你安装了 Pyramid,你可以从炼金术脚手架创建一个示例项目。
$ $VENV/bin/pcreate -s alchemy tutorial
如果该命令不适合您,SQLAlchemy + URL Dispatch Wiki Tutorial 会详细介绍和先决条件。
脚手架包括这些tests.
我们正在开发一个更新此脚手架的新分支,目标是在 Pyramid 1.7 中进行合并。这是它的 tests.
如果您 git 签出该分支,并且 运行 通过教程步骤,您可以看到完整的上下文。
[已编辑]
这里还有一个值得一看的地方:Pyramid's own integration tests。
顺便说一句,我很难给你一个合适的答案。对于初学者,我看不到 DummyRequest
在您的测试代码中被调用的位置。你能在你的问题中给出一个完整的可重现的例子吗?