提供一些测试帮助程序和高级mocktestcase。
ftw.testing的Python项目详细描述
ftw.测试
此软件包提供编写测试的助手。
目录
- FTW测试
- 模拟测试用例
- 模拟与存根
- 组件注册表层
- 邮寄测试助手
- 冻结日期时间.now()
- 静态uuid
- 通用安装卸载测试
- 禁用QuickInstaller快照
- 事务拦截器
- 测试层
- 组件注册表隔离层
- 临时目录层
- 控制台脚本测试层
- 1.20.1(2019-04-04)
- 1.20.0(2019-01-25)
- 1.19.2(2018-11-05)
- 1.19.1(2018-10-23)
- 1.19.0(2018-10-15)
- 1.18.0(2018-07-12)
- 1.17.0(2017-10-02)
- 1.16.0(2017-08-08)
- 1.15.2(2017-07-18)
- 1.15.1(2017-07-04)
- 1.15.0(2017-07-03)
- 1.14.0(2017-06-23)
- 1.13.0(2017-06-20)
- 1.12.0(2017-06-19)
- 1.11.0(2016-03-31)
- 1.10.3(2015-10-11)
- 1.10.2(2015-07-30)
- 1.10.1(2015-07-27)
- 1.10.0(2015-05-18)
- 1.9.1(2015-05-15)
- 1.9.0(2015-05-15)
- 1.8.1(2015-01-05)
- 1.8.0(2014-12-31)
- 1.7.0(2014-09-30)
- 1.6.4(2014-05-01)
- 1.6条.3(2014-04-30)
- 1.6.2(2014-04-30)
- 1.6.1(2014-04-29)
- 1.6.0(2014-04-29)
- 1.5.2(2014-02-09)
- 1.5.1(2014-02-08)
- 1.5.0(2013-09-24)
- 1.4(2013-08-26)
- 1.3.1(2013-05-24)
- 1.3(2013-05-03)
- 1.2(2012-05-22)
- 1.1(2011-11-16)
- 1.0(2011-10-12)
集成测试
ftwintegrationtesting层
ftwintegurationtesting是plone's的一个自以为是的扩展。
默认集成测试层。
主要目标是能够运行ftw.testbrowser
集成测试驱动程序。
数据库隔离和事务
plone默认集成测试层确实支持事务:
在测试中提交更改时,不提供隔离
提交的更改将在下一层中显示。
-
我们通过在测试设置中创建一个保存点来隔离测试和
正在回滚到"测试拆分"中的保存点。
-
使用事务拦截器,我们确保测试中没有代码
可以提交或中止事务。模拟事务行为
使用保存点。
用法示例:
fromftw.testingimportFTWIntegrationTestingfromplone.app.testingimportPLONE_FIXTUREfromplone.app.testingimportPloneSandboxLayerclassTestingLayer(PloneSandboxLayer):defaultBases=(PLONE_FIXTURE,)TESTING_FIXTURE=TestingLayer()INTEGRATION_TESTING=FTWIntegrationTesting(bases=(TESTING_FIXTURE,),name='my.package:integration')
ftwintegrationtestcase
集成测试用例是一个提供合理默认值的测试用例基类。
以及使用ftwintegrationtesting测试plone插件的实用帮助程序
测试层。
您可以在包中创建自己的基类,设置默认测试
根据您的需要对行为和帮助程序进行分层和扩展。
用法示例:
# my/package/tests/test_case.pyfromftw.testingimportFTWIntegrationTestCasefrommy.package.testingimportINTEGRATION_TESTINGclassIntegrationTestCase(FTWIntegrationTestCase):layer=INTEGRATION_TESTING
ftwintegrationtesting层
ftwintegurationtesting是plone's的一个自以为是的扩展。 默认集成测试层。
主要目标是能够运行ftw.testbrowser 集成测试驱动程序。
数据库隔离和事务
plone默认集成测试层确实支持事务: 在测试中提交更改时,不提供隔离 提交的更改将在下一层中显示。
- 我们通过在测试设置中创建一个保存点来隔离测试和 正在回滚到"测试拆分"中的保存点。
- 使用事务拦截器,我们确保测试中没有代码 可以提交或中止事务。模拟事务行为 使用保存点。
用法示例:
fromftw.testingimportFTWIntegrationTestingfromplone.app.testingimportPLONE_FIXTUREfromplone.app.testingimportPloneSandboxLayerclassTestingLayer(PloneSandboxLayer):defaultBases=(PLONE_FIXTURE,)TESTING_FIXTURE=TestingLayer()INTEGRATION_TESTING=FTWIntegrationTesting(bases=(TESTING_FIXTURE,),name='my.package:integration')
ftwintegrationtestcase
集成测试用例是一个提供合理默认值的测试用例基类。 以及使用ftwintegrationtesting测试plone插件的实用帮助程序 测试层。
您可以在包中创建自己的基类,设置默认测试 根据您的需要对行为和帮助程序进行分层和扩展。
用法示例:
# my/package/tests/test_case.pyfromftw.testingimportFTWIntegrationTestCasefrommy.package.testingimportINTEGRATION_TESTINGclassIntegrationTestCase(FTWIntegrationTestCase):layer=INTEGRATION_TESTING
模拟测试用例
ftw.testing 提供了一个高级mocktestcase,它提供了 plone.mocktestcase plone.mocktestcase mocktestcase
fromftw.testingimportMockTestCase
以下附加方法可用:
< DL>它还修复了mock工具中的一个问题,其中 有些断言在某些情况下不是很有用。
模拟与存根
模拟用于测试两个对象之间的通信。它 断言方法调用。当测试不应测试 一个对象在做某事之后有一个特定的状态(例如,它有 属性设置为某物,但如果对象确实 和另一个物体。例如,如果对象发送电子邮件 当调用方法 bar 时,我们可以模拟sendmail对象并 在发送电子邮件方法调用上断言。
另一方面,我们经常要测试一个对象(属性)的状态 价值观)在做某事之后。只要 调用方法并断言属性值。但是我们有 要建立一个集成测试并安装plone,需要很长时间。 用于测试一个对象,该对象依赖于 单元测试,我们可以使用 存根来伪造其他(单独测试的)零件 普罗恩stub的工作方式类似于mock:您可以"期望"方法调用和 定义结果。 stubs 和 mock 之间的区别在于 存根不能断言期望值,因此如果 预期的事情不会发生。因此,当使用存根时,我们可以断言 没有断言对象之间通信的状态。
组件注册表层
mocktestcase能够模拟组件(适配器、实用程序)。它 每次测试后清除组件注册表。
但是当我们使用zcml层时,加载它应该使用的包的zcml 同一层上所有测试的相同组件注册表。这个 componentRegistryLayer是用于共享组件的层超类 注册并加速测试。
用法:
fromftw.testing.layerimportComponentRegistryLayerclassZCMLLayer(ComponentRegistryLayer):defsetUp(self):super(ZCMLLayer,self).setUp()importmy.packageself.load_zcml_file('configure.zcml',my.package)ZCML_LAYER=ZCMLLayer()
请注意,componentregistrylayer是创建 自己的层(通过子类化 componentregistrylayer )并且不能用于 直接使用默认基 。这允许我们使用 加载ZCML文件 和 加载ZCML字符串
邮寄测试助手
邮件帮助器对象模拟mailhost并捕获发送的电子邮件。 这些电子邮件可以很容易地用于断言。
用法:
fromftw.testing.mailingimportMailingimporttransactionclassMyTest(TestCase):layer=MY_FUNCTIONAL_TESTINGdefsetUp(self):Mailing(self.layer['portal']).set_up()transaction.commit()deftearDown(self):Mailing(self.layer['portal']).tear_down()deftest_mail_stuff(self):portal=self.layer['portal']do_send_email()mail=Mailing(portal).pop()self.assertEquals('Subject: ...',mail)
冻结日期时间.now()
当测试依赖于当前时间的代码时,有必要设置 当前时间到特定时间。上下文管理器使 非常简单:
fromftw.testingimportfreezefromdatetimeimportdatetimewithfreeze(datetime(2014,5,7,12,30)):# test code
"冻结"上下文管理器修补"日期时间"模块和"时间"模块 并支持zope datetime 模块。退出时会删除修补程序 上下文管理器。
更新冻结时间
fromftw.testingimportfreezefromdatetimeimportdatetimewithfreeze(datetime(2014,5,7,12,30))asclock:# its 2014, 5, 7, 12, 30clock.forward(days=2)# its 2014, 5, 9, 12, 30clock.backward(minutes=15)# its 2014, 5, 9, 12, 15
可以忽略模块,以便从 此模块用实际电流值而不是冻结的电流值响应:
fromftw.testingimportfreezefromdatetimeimportdatetimewithfreeze(datetime(2014,5,7,12,30),ignore_modules=['my.package.realtime']):pass
你可以使用 timedelta arguments`(https://docs.python.org/2/library/datetime.html datetime.timedelta)_ 对于``forward` 和 backward
静态uuid
在断言uuid时,它们在每次测试运行时都会发生更改,这可能会令人恼火。 staticuid装饰器通过使用 在一个范围内加上前缀并计数,通常是一个测试用例:
fromftw.testingimportstaticuidfromplone.app.testingimportPLONE_INTEGRATION_TESTINGfromunittest2importTestCaseclassMyTest(TestCase):layer=PLONE_INTEGRATION_TESTING@staticuid()deftest_all_the_things(self):doc=self.portal.get(self.portal.invokeFactory('Document','the-document'))self.assertEquals('testallthethings0000000000000001',IUUID(doc))@staticuid('MyUIDS')deftest_a_prefix_can_be_set(self):doc=self.portal.get(self.portal.invokeFactory('Document','the-document'))self.assertEquals('MyUIDS00000000000000000000000001',IUUID(doc))
通用安装卸载测试
ftw.testing 为测试卸载配置文件提供了一个测试超类。 在安装包之前,测试生成一个通用的安装快照,然后 安装和卸载包,创建另一个快照并对其进行区分。 安装包时不安装其依赖项,因为 不应在卸载配置文件中包含卸载依赖项。
包含适当的测试层设置,测试在单独的 不应干扰其他测试的层。
简单示例:
fromftw.testingimportFTWIntegrationTestingfromplone.app.testingimportPLONE_FIXTUREfromplone.app.testingimportPloneSandboxLayerclassTestingLayer(PloneSandboxLayer):defaultBases=(PLONE_FIXTURE,)TESTING_FIXTURE=TestingLayer()INTEGRATION_TESTING=FTWIntegrationTesting(bases=(TESTING_FIXTURE,),name='my.package:integration')0
my.package 应该有一个通用的设置配置文件 配置my.package:默认值,用于安装包和 配置my.package:uninstall以卸载包。 它需要使用 z3c.autoinclude 入口点来加载 它的zcml。
选项配置为类变量:
< DL>完整示例:
fromftw.testingimportFTWIntegrationTestingfromplone.app.testingimportPLONE_FIXTUREfromplone.app.testingimportPloneSandboxLayerclassTestingLayer(PloneSandboxLayer):defaultBases=(PLONE_FIXTURE,)TESTING_FIXTURE=TestingLayer()INTEGRATION_TESTING=FTWIntegrationTesting(bases=(TESTING_FIXTURE,),name='my.package:integration')1
禁用QuickInstaller快照
quickinstaller通常会生成一个完整的通用安装(gs)快照 在安装每个GS配置文件之前和之后,为了能够 然后卸载配置文件。
在测试中,我们通常不需要此功能,并希望禁用它 加速测试。
ftw.testing.quickinstaller模块为 替换QuickInstaller事件处理程序跳过创建快照。 通常我们希望尽早完成(当加载testing.py 时),以便 所有的测试都在加速。 但是,一些涉及quickinstaller的测试依赖于 创建快照(请参阅关于卸载测试的上一节)。 因此,快照修补程序对象为 暂时启用/禁用快照功能。
用法:
尽早禁用快照,以便一切都很快。通常这是 在模块范围内的testing.py中完成,因此它已经发生了 当testrunner导入测试时:
fromftw.testingimportFTWIntegrationTestingfromplone.app.testingimportPLONE_FIXTUREfromplone.app.testingimportPloneSandboxLayerclassTestingLayer(PloneSandboxLayer):defaultBases=(PLONE_FIXTURE,)TESTING_FIXTURE=TestingLayer()INTEGRATION_TESTING=FTWIntegrationTesting(bases=(TESTING_FIXTURE,),name='my.package:integration')2
在测试QuickInstaller快照相关的事情时,例如卸载, 可以为上下文管理器重新启用快照,或者通常情况下:
fromftw.testingimportFTWIntegrationTestingfromplone.app.testingimportPLONE_FIXTUREfromplone.app.testingimportPloneSandboxLayerclassTestingLayer(PloneSandboxLayer):defaultBases=(PLONE_FIXTURE,)TESTING_FIXTURE=TestingLayer()INTEGRATION_TESTING=FTWIntegrationTesting(bases=(TESTING_FIXTURE,),name='my.package:integration')3
事务拦截器
TransactionInterceptor修补了Zope的事务管理器 以防止代码与事务交互。
例如,这可以用于确保没有测试提交事务 当它们在集成测试层上运行时。
拦截器需要用 install() 手动安装并移除 最后使用 uninstall() 。用户有责任确保 正确卸载。
安装拦截器时,它还未处于活动状态,并通过所有 电话。 拦截从 intercept() 开始,在 clear() 结束时 被调用。
fromftw.testingimportFTWIntegrationTestingfromplone.app.testingimportPLONE_FIXTUREfromplone.app.testingimportPloneSandboxLayerclassTestingLayer(PloneSandboxLayer):defaultBases=(PLONE_FIXTURE,)TESTING_FIXTURE=TestingLayer()INTEGRATION_TESTING=FTWIntegrationTesting(bases=(TESTING_FIXTURE,),name='my.package:integration')4
测试层
组件注册表隔离层
plone.app.testing的默认测试层(例如
plone_fixture
)不
隔离每个测试的组件注册表。
ftw.测试
's
组件注册表隔离
测试层隔离
组件注册表为每个测试提供一个堆叠的zcml配置上下文
并提供用于加载的方法
加载zcml_字符串
和
加载zcml_文件
。
ZCML,
示例:
fromftw.testingimportFTWIntegrationTestingfromplone.app.testingimportPLONE_FIXTUREfromplone.app.testingimportPloneSandboxLayerclassTestingLayer(PloneSandboxLayer):defaultBases=(PLONE_FIXTURE,)TESTING_FIXTURE=TestingLayer()INTEGRATION_TESTING=FTWIntegrationTesting(bases=(TESTING_FIXTURE,),name='my.package:integration')
5
临时目录层
测试层为
每次测试并在拆卸时递归删除。
可以使用
temp_directory
键访问目录路径。
用法示例:
fromftw.testingimportFTWIntegrationTestingfromplone.app.testingimportPLONE_FIXTUREfromplone.app.testingimportPloneSandboxLayerclassTestingLayer(PloneSandboxLayer):defaultBases=(PLONE_FIXTURE,)TESTING_FIXTURE=TestingLayer()INTEGRATION_TESTING=FTWIntegrationTesting(bases=(TESTING_FIXTURE,),name='my.package:integration')
6
控制台脚本测试层
控制台脚本层帮助测试控制台脚本。
在层设置上,它创建并执行一个独立的构建,包位于
开发,它创建此包的所有控制台脚本。
这使得通过真正执行控制台脚本来测试控制台脚本变得很容易。
用法示例:
fromftw.testingimportFTWIntegrationTestingfromplone.app.testingimportPLONE_FIXTUREfromplone.app.testingimportPloneSandboxLayerclassTestingLayer(PloneSandboxLayer):defaultBases=(PLONE_FIXTURE,)TESTING_FIXTURE=TestingLayer()INTEGRATION_TESTING=FTWIntegrationTesting(bases=(TESTING_FIXTURE,),name='my.package:integration')
7
请注意,构建
控制台脚本。您可以将依赖项放入您的
测试中。
附加要求。
组件注册表隔离层
plone.app.testing的默认测试层(例如 plone_fixture )不 隔离每个测试的组件注册表。
ftw.测试 's 组件注册表隔离 测试层隔离 组件注册表为每个测试提供一个堆叠的zcml配置上下文 并提供用于加载的方法 加载zcml_字符串 和 加载zcml_文件 。 ZCML,
示例:
fromftw.testingimportFTWIntegrationTestingfromplone.app.testingimportPLONE_FIXTUREfromplone.app.testingimportPloneSandboxLayerclassTestingLayer(PloneSandboxLayer):defaultBases=(PLONE_FIXTURE,)TESTING_FIXTURE=TestingLayer()INTEGRATION_TESTING=FTWIntegrationTesting(bases=(TESTING_FIXTURE,),name='my.package:integration')5
临时目录层
测试层为 每次测试并在拆卸时递归删除。
可以使用 temp_directory 键访问目录路径。
用法示例:
fromftw.testingimportFTWIntegrationTestingfromplone.app.testingimportPLONE_FIXTUREfromplone.app.testingimportPloneSandboxLayerclassTestingLayer(PloneSandboxLayer):defaultBases=(PLONE_FIXTURE,)TESTING_FIXTURE=TestingLayer()INTEGRATION_TESTING=FTWIntegrationTesting(bases=(TESTING_FIXTURE,),name='my.package:integration')6
控制台脚本层帮助测试控制台脚本。 在层设置上,它创建并执行一个独立的构建,包位于 开发,它创建此包的所有控制台脚本。 这使得通过真正执行控制台脚本来测试控制台脚本变得很容易。
用法示例:
fromftw.testingimportFTWIntegrationTestingfromplone.app.testingimportPLONE_FIXTUREfromplone.app.testingimportPloneSandboxLayerclassTestingLayer(PloneSandboxLayer):defaultBases=(PLONE_FIXTURE,)TESTING_FIXTURE=TestingLayer()INTEGRATION_TESTING=FTWIntegrationTesting(bases=(TESTING_FIXTURE,),name='my.package:integration')7
请注意,构建 控制台脚本。您可以将依赖项放入您的 测试中。 附加要求。
兼容性
运行时使用plone 4.3
链接
- github:https://github.com/4teamwork/ftw.testing" rel="nofollow">https://github.com/4teamwork/ftw.testing
- 问题:https://github.com/4teamwork/ftw.testing/issues" rel="nofollow">https://github.com/4teamwork/ftw.testing/issues
- pypi: http://pypi.python.org/pypi/ftw.testing
- 持续集成: https://jenkins.4teamwork.ch/search?q=ftw.测试