2008-05-09
Opentaps单元测试
Opentaps单元测试
目录
* 1 如何写单元测试
o 1.1 opentaps 1.0
o 1.2 opentaps 0.9
* 2 单元测试的设置
* 3 单元测试策略
* 4 单元测试指南
* 5 创造参考数据集
* 6 从beanshell上运行单元测试,
* 7 在intellij上调试单元测试
* 8 在MySQL上运行单元测试的注意事项
如何写单元测试
opentaps 1.0
For opentaps 1.0, you would write a set of Junit tests in a class, then define it in an XML testdef file like this:在opentaps 1.0 中,你需要写一套junit测试的java类,然后定义在testdef文件夹下的一个XML文件中:
<test-suite suite-name="entitytests"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://www.ofbiz.org/dtds/test-suite.xsd">
<test-case case-name="security-tests"><junit-test-suite class-name="com.opensourcestrategies.crmsfa.test.SecurityTests"/></test-case>
</test-suite>
您可以定义多个测试在一个testdef XML文件。 然后添加testdef文件到ofbiz-component.xml,象这样:
<test-suite loader="main" location="testdef/crmsfa_tests.xml"/>
然后,当你执行 $ ant run-tests 你的测试将在运行。
opentaps 0.9
在opentaps 0.9 ,你将写上你的单元测试类和增加它到base/config/test-containers.xml中,在"junit-container"的底部,是这样的:
<container name="junit-container" class="org.ofbiz.base.container.JunitContainer">
<property name="base-test" value="org.ofbiz.base.test.BaseUnitTests"/>
<property name="entity-test" value="org.ofbiz.entity.test.EntityTestSuite"/>
<property name="service-test" value="org.ofbiz.service.test.ServiceEngineTests"/>
<property name="crm-security" value="com.opensourcestrategies.crmsfa.test.SecurityTests"/> <!-- your unit tests -->
<!--
<property name="usps-test" value="org.ofbiz.shipment.thirdparty.usps.UspsServicesTests"/>
<property name="jxunit-test" value="net.sourceforge.jxunit.JXTestCase"/>
-->
</container>
然后你执行$ ant run-tests。伴随现有ofbiz测试套件,你的测试运行。
重要事项 :
* 一个单独的数据库使用"test" delegator进行你的测试,,并确保它被定义在framework/entity/config/entityengine.xml设置为正确的数据库。
* 该opentaps测试需要在hot-deploy/component-load.xml默认不启用,不要忘记激活它们。
单元测试的设置
我们建议您创建一个单独的数据库,并使用相同数据库服务器进行测试的目的,并安装所有演示数据进入测试数据库。对我们来说,这个数据库是所谓的" opentaps_testing " 。然后,编辑文件framework/entity/config/entityengine.xml和定义opentaps_testing作为一个新的DataSource ,命名为" localmysltesting "或" localpostgrestesting " 。接下来,启动演示数据写入测试数据库通过编辑默认delegator :
<delegator name="default" entity-model-reader="main" entity-group-reader="main" entity-eca-reader="main" distributed-cache-clear-enabled="false">
<group-map group-name="org.ofbiz" datasource-name="localXXXtesting"/>
</delegator>
然后执行一个 $ ant run-install
安装所有种子和演示数据进入测试数据库。然后你就可以编辑 default delegator回到你原来delegator ,并设置测试 delegator来测试数据库:
<delegator name="test" entity-model-reader="main" entity-group-reader="main" entity-eca-reader="main">
<group-map group-name="org.ofbiz" datasource-name="localXXXtesting"/>
</delegator>
所有单元测试应该运行使用 test delegator 。这样按姓名实例化 "test" delegator,并使用delegator来初始化一个dispatcher。或者你可以只写测试套件,从而扩大了opentapstests基础类,使用哪个由你自己决定。
如果您需要修改端口设置为测试,那么,编辑文件framework/base/config/test- containers.xml 。
单元测试策略
这些都是一些单元测试策略:
交易比较-比较交易产生的一个样本交易,有可能是预加载到该系统。
例如,张贴薪资向总帐,然后比较与试验数据的正确帐面交易,以确保他们是相等的。
等价,是一个很重要的概念:它是不可能的两套交易,是相同的,因为在最低限度,他们有不同的身份证号码,他们就可能会参考其他交易不同的证件。
举例来说,每一个命令,将有一个不同的orderid和不同的存货项目IDS中预留反对。
不过,有两个命令可被视为等同,如果他们有同一标准的项目,价格,运输方式,客户,地址,税务和推广数额等。
* State change - Compare the state of the system before and after a transaction has occurred.状态变化-比较系统状态之前和之后的交易已经发生。 For example, check the inventry of an item, then ship an order, and check the resulting inventory to make sure that it is correctly decremented.例如,可查看inventry的一个项目,那么船舶订单,并检查所产生的库存,以确保它是正确decremented 。 This could get very complex: Shipping an order could cause customer balances, invoices, ledger postings, and inventory changes.这可能变得非常复杂:船舶命令可能会导致客户余额,发票,总帐帖子,和库存的变化。 Multiple tests could be run off the same transaction event.多重考验,可跑了出去,同一交易活动。
* Absolute state check - At all times, certain relationships must hold.绝对状态检查-在任何时候,某些关系必须持有。 For example, the sum of all debits must equal sum of all credits.举例来说,这笔扣款都必须同等金额的所有学分。
Tests should be written against the services that create the original data.测试应以书面对服务创造的原始数据。 For example, if you are writing tests against CRMSFA activity, you can use users from the demo data set, but you should use the CRMSFA activity services to create or update your activities.例如,如果你是书面考试对crmsfa活动中,你可以使用用户从演示数据集,但你应该用crmsfa活动服务,以创建或更新您的活动。 Otherwise, if you create those activities with some other method, future changes to the services to create activities will not be covered by your unit tests.否则,如果您要创建这些活动与其他一些方法,未来的变化,从而服务,以创建活动,将不会包括在你的单元测试。
Tests should be run against a dedicated testing database with demo and seed data rather than production data.测试应有违专门的测试数据库,演示和种子数据,而不是生产数据。 Therefore, the tests generally should set up their own initial conditions and run to completion, but they do not need to "tear down" and remove all data created by the tests.因此,试验一般应当成立了自己的初始条件,并完整地运行,但他们并不需要"拆掉" ,并删除所有数据所带来的考验。 (This would be very impractical: imagine creating and shipping an order. To tear it down would involve reverting order data, customer information, inventory data, shipment data, invoices and payments, and accounting entries.) A good test for the tests is that if you ran the test suite in succession multiple times, they should pass during the second and third runs as well as the first run. (这将是非常不切实际的:想象,创造和航运的命令。撕毁它会涉及不走回头路治安数据,客户资料,库存数据,货运数据,发票及付款,会计分录) 。很好的测试,为测试的是如果你冉测试套件相继多次,他们应该通过在第二次和第三次运行以及为首次运行。
5 创造参考数据集
在许多测试,你将看到comparisons against pre-stored AcctgTrans and AcctgTransEntries。
这些都是用来比较事务结果并确保它们和参考数据集是一致的。
参考数据集按照以下方式创建:
1。通过一套事务逻辑,比如创建invoice 并且把它标记为READY。
2。通过Webtools > XML Data Export 并且选择entities导出。这种情况下,他可能是一个Invoice,InvoiceItem, AcctgTrans, AcctgTransEntry等等实体。导出他们到一个文件或浏览器并且复制他们到一个文件。
3。编辑事务文件和变更如下:
1.由系统生成100xx类似"XXX-TEST-###"的id,确保他们不会造成主键冲突。
2.对于AcctgTrans,更改所有AcctgTrans的 glFiscalTypeId从"ACTUAL"到"REFERENCE" ,使他们不会干扰实际的记录。
3. 除去部分不属于参照实体的参考集。 举例来说,invoice可能是参考集的部分,但workeffortid,inventoryitemid等等被AcctgTransEntry关联不是。
4. 试验加载新的entity XML到你的专用测试数据库。 它应该不会造成冲突。
5. 添加它到opentaps-tests 组件的ofbiz-component.xml。 确定它将被今后的测试加载!
在beanshell上运行单元测试,
在你写了很多的单元测试之后,全部运行它们都需要很长的时间。幸运的是,你可以使用beanshell运行一个单元测试,这样加速您的开发。要做到这一点,你需要的telnet到你beanshell port,然后实例化一个对象的单元测试类,并运行你的测试方法:
si-chens-computer:~ sichen$ telnet localhost 9990
Trying ::1...
Connected to localhost.
Escape character is '^]'.
BeanShell 2.0b4 - by Pat Niemeyer (pat@pat.net)
bsh % import org.opentaps.tests.purchasing.MrpTests;
bsh % mrpTests = new MrpTests();
bsh % mrpTests.testMrpPurchasedProduct();
bsh %
如果test成功,你会看到没有任何的讯息出现在你beanshell控制台。如果它失败了,你会看到一个堆栈跟踪的错误信息。在这两种情况下,您应该可以看到日志信息,在runtime/logs/ofbiz.log或runtime/logs/console.log。
让您的生活更简单,你可以把所有这一切都做成你自己的.bsh文件,像mymrptests.bsh ,然后在beanshell控制台调用它:例如
bsh % source("myMrpTests.bsh");
在 IntelliJ 中调试单元测试
默认任务的测试会做一个全局汇编。跳过这个,你可以重新在build.xml中定义run-tests target。
如下
<target name="run-tests">
<java jar="ofbiz.jar" fork="true">
<arg value="test"/>
</java>
</target>
使用调试器可以帮助加快开发单元测试。在你的调试系统中,您可以启用调试JVM arguments。你可以在 IntelliJ IDE中执行 run-tests target。
<target name="run-tests">
<java jar="ofbiz.jar" fork="true">
<jvmarg value="${memory.max.param}"/>
<jvmarg value="-Xdebug"/>
<jvmarg value="-Xnoagent"/>
<jvmarg value="-Djava.compiler=NONE "/>
<jvmarg value="-Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5005"/>
<arg value="test"/>
</java>
</target>
你应该在配置好调试后,可以立即执行ant run-tests。在你的测试环境中,不要忘了重新编译组件。
另一个秘诀是注释掉一切不必要的测试套件。不幸的是,这涉及到寻找每一个ofbiz - component.xml 。其中一个方法能够找到他们,如果你使用了POSIX操作系统,是利用查找,$ find . -name ofbiz-component.xml -exec grep test-suite {} \; -print
在MySQL上运行单元测试的注意事项
当运行单元测试在MySQL中使用事务请一定要知道一个断言失败,可发出回滚并导致事务数据在数据库中的一个不一致状态,有可能让测试通过,即使在现实中,却失败了。
目录
* 1 如何写单元测试
o 1.1 opentaps 1.0
o 1.2 opentaps 0.9
* 2 单元测试的设置
* 3 单元测试策略
* 4 单元测试指南
* 5 创造参考数据集
* 6 从beanshell上运行单元测试,
* 7 在intellij上调试单元测试
* 8 在MySQL上运行单元测试的注意事项
如何写单元测试
opentaps 1.0
For opentaps 1.0, you would write a set of Junit tests in a class, then define it in an XML testdef file like this:在opentaps 1.0 中,你需要写一套junit测试的java类,然后定义在testdef文件夹下的一个XML文件中:
<test-suite suite-name="entitytests"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://www.ofbiz.org/dtds/test-suite.xsd">
<test-case case-name="security-tests"><junit-test-suite class-name="com.opensourcestrategies.crmsfa.test.SecurityTests"/></test-case>
</test-suite>
您可以定义多个测试在一个testdef XML文件。 然后添加testdef文件到ofbiz-component.xml,象这样:
<test-suite loader="main" location="testdef/crmsfa_tests.xml"/>
然后,当你执行 $ ant run-tests 你的测试将在运行。
opentaps 0.9
在opentaps 0.9 ,你将写上你的单元测试类和增加它到base/config/test-containers.xml中,在"junit-container"的底部,是这样的:
<container name="junit-container" class="org.ofbiz.base.container.JunitContainer">
<property name="base-test" value="org.ofbiz.base.test.BaseUnitTests"/>
<property name="entity-test" value="org.ofbiz.entity.test.EntityTestSuite"/>
<property name="service-test" value="org.ofbiz.service.test.ServiceEngineTests"/>
<property name="crm-security" value="com.opensourcestrategies.crmsfa.test.SecurityTests"/> <!-- your unit tests -->
<!--
<property name="usps-test" value="org.ofbiz.shipment.thirdparty.usps.UspsServicesTests"/>
<property name="jxunit-test" value="net.sourceforge.jxunit.JXTestCase"/>
-->
</container>
然后你执行$ ant run-tests。伴随现有ofbiz测试套件,你的测试运行。
重要事项 :
* 一个单独的数据库使用"test" delegator进行你的测试,,并确保它被定义在framework/entity/config/entityengine.xml设置为正确的数据库。
* 该opentaps测试需要在hot-deploy/component-load.xml默认不启用,不要忘记激活它们。
单元测试的设置
我们建议您创建一个单独的数据库,并使用相同数据库服务器进行测试的目的,并安装所有演示数据进入测试数据库。对我们来说,这个数据库是所谓的" opentaps_testing " 。然后,编辑文件framework/entity/config/entityengine.xml和定义opentaps_testing作为一个新的DataSource ,命名为" localmysltesting "或" localpostgrestesting " 。接下来,启动演示数据写入测试数据库通过编辑默认delegator :
<delegator name="default" entity-model-reader="main" entity-group-reader="main" entity-eca-reader="main" distributed-cache-clear-enabled="false">
<group-map group-name="org.ofbiz" datasource-name="localXXXtesting"/>
</delegator>
然后执行一个 $ ant run-install
安装所有种子和演示数据进入测试数据库。然后你就可以编辑 default delegator回到你原来delegator ,并设置测试 delegator来测试数据库:
<delegator name="test" entity-model-reader="main" entity-group-reader="main" entity-eca-reader="main">
<group-map group-name="org.ofbiz" datasource-name="localXXXtesting"/>
</delegator>
所有单元测试应该运行使用 test delegator 。这样按姓名实例化 "test" delegator,并使用delegator来初始化一个dispatcher。或者你可以只写测试套件,从而扩大了opentapstests基础类,使用哪个由你自己决定。
如果您需要修改端口设置为测试,那么,编辑文件framework/base/config/test- containers.xml 。
单元测试策略
这些都是一些单元测试策略:
交易比较-比较交易产生的一个样本交易,有可能是预加载到该系统。
例如,张贴薪资向总帐,然后比较与试验数据的正确帐面交易,以确保他们是相等的。
等价,是一个很重要的概念:它是不可能的两套交易,是相同的,因为在最低限度,他们有不同的身份证号码,他们就可能会参考其他交易不同的证件。
举例来说,每一个命令,将有一个不同的orderid和不同的存货项目IDS中预留反对。
不过,有两个命令可被视为等同,如果他们有同一标准的项目,价格,运输方式,客户,地址,税务和推广数额等。
* State change - Compare the state of the system before and after a transaction has occurred.状态变化-比较系统状态之前和之后的交易已经发生。 For example, check the inventry of an item, then ship an order, and check the resulting inventory to make sure that it is correctly decremented.例如,可查看inventry的一个项目,那么船舶订单,并检查所产生的库存,以确保它是正确decremented 。 This could get very complex: Shipping an order could cause customer balances, invoices, ledger postings, and inventory changes.这可能变得非常复杂:船舶命令可能会导致客户余额,发票,总帐帖子,和库存的变化。 Multiple tests could be run off the same transaction event.多重考验,可跑了出去,同一交易活动。
* Absolute state check - At all times, certain relationships must hold.绝对状态检查-在任何时候,某些关系必须持有。 For example, the sum of all debits must equal sum of all credits.举例来说,这笔扣款都必须同等金额的所有学分。
Tests should be written against the services that create the original data.测试应以书面对服务创造的原始数据。 For example, if you are writing tests against CRMSFA activity, you can use users from the demo data set, but you should use the CRMSFA activity services to create or update your activities.例如,如果你是书面考试对crmsfa活动中,你可以使用用户从演示数据集,但你应该用crmsfa活动服务,以创建或更新您的活动。 Otherwise, if you create those activities with some other method, future changes to the services to create activities will not be covered by your unit tests.否则,如果您要创建这些活动与其他一些方法,未来的变化,从而服务,以创建活动,将不会包括在你的单元测试。
Tests should be run against a dedicated testing database with demo and seed data rather than production data.测试应有违专门的测试数据库,演示和种子数据,而不是生产数据。 Therefore, the tests generally should set up their own initial conditions and run to completion, but they do not need to "tear down" and remove all data created by the tests.因此,试验一般应当成立了自己的初始条件,并完整地运行,但他们并不需要"拆掉" ,并删除所有数据所带来的考验。 (This would be very impractical: imagine creating and shipping an order. To tear it down would involve reverting order data, customer information, inventory data, shipment data, invoices and payments, and accounting entries.) A good test for the tests is that if you ran the test suite in succession multiple times, they should pass during the second and third runs as well as the first run. (这将是非常不切实际的:想象,创造和航运的命令。撕毁它会涉及不走回头路治安数据,客户资料,库存数据,货运数据,发票及付款,会计分录) 。很好的测试,为测试的是如果你冉测试套件相继多次,他们应该通过在第二次和第三次运行以及为首次运行。
5 创造参考数据集
在许多测试,你将看到comparisons against pre-stored AcctgTrans and AcctgTransEntries。
这些都是用来比较事务结果并确保它们和参考数据集是一致的。
参考数据集按照以下方式创建:
1。通过一套事务逻辑,比如创建invoice 并且把它标记为READY。
2。通过Webtools > XML Data Export 并且选择entities导出。这种情况下,他可能是一个Invoice,InvoiceItem, AcctgTrans, AcctgTransEntry等等实体。导出他们到一个文件或浏览器并且复制他们到一个文件。
3。编辑事务文件和变更如下:
1.由系统生成100xx类似"XXX-TEST-###"的id,确保他们不会造成主键冲突。
2.对于AcctgTrans,更改所有AcctgTrans的 glFiscalTypeId从"ACTUAL"到"REFERENCE" ,使他们不会干扰实际的记录。
3. 除去部分不属于参照实体的参考集。 举例来说,invoice可能是参考集的部分,但workeffortid,inventoryitemid等等被AcctgTransEntry关联不是。
4. 试验加载新的entity XML到你的专用测试数据库。 它应该不会造成冲突。
5. 添加它到opentaps-tests 组件的ofbiz-component.xml。 确定它将被今后的测试加载!
在beanshell上运行单元测试,
在你写了很多的单元测试之后,全部运行它们都需要很长的时间。幸运的是,你可以使用beanshell运行一个单元测试,这样加速您的开发。要做到这一点,你需要的telnet到你beanshell port,然后实例化一个对象的单元测试类,并运行你的测试方法:
si-chens-computer:~ sichen$ telnet localhost 9990
Trying ::1...
Connected to localhost.
Escape character is '^]'.
BeanShell 2.0b4 - by Pat Niemeyer (pat@pat.net)
bsh % import org.opentaps.tests.purchasing.MrpTests;
bsh % mrpTests = new MrpTests();
bsh % mrpTests.testMrpPurchasedProduct();
bsh %
如果test成功,你会看到没有任何的讯息出现在你beanshell控制台。如果它失败了,你会看到一个堆栈跟踪的错误信息。在这两种情况下,您应该可以看到日志信息,在runtime/logs/ofbiz.log或runtime/logs/console.log。
让您的生活更简单,你可以把所有这一切都做成你自己的.bsh文件,像mymrptests.bsh ,然后在beanshell控制台调用它:例如
bsh % source("myMrpTests.bsh");
在 IntelliJ 中调试单元测试
默认任务的测试会做一个全局汇编。跳过这个,你可以重新在build.xml中定义run-tests target。
如下
<target name="run-tests">
<java jar="ofbiz.jar" fork="true">
<arg value="test"/>
</java>
</target>
使用调试器可以帮助加快开发单元测试。在你的调试系统中,您可以启用调试JVM arguments。你可以在 IntelliJ IDE中执行 run-tests target。
<target name="run-tests">
<java jar="ofbiz.jar" fork="true">
<jvmarg value="${memory.max.param}"/>
<jvmarg value="-Xdebug"/>
<jvmarg value="-Xnoagent"/>
<jvmarg value="-Djava.compiler=NONE "/>
<jvmarg value="-Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5005"/>
<arg value="test"/>
</java>
</target>
你应该在配置好调试后,可以立即执行ant run-tests。在你的测试环境中,不要忘了重新编译组件。
另一个秘诀是注释掉一切不必要的测试套件。不幸的是,这涉及到寻找每一个ofbiz - component.xml 。其中一个方法能够找到他们,如果你使用了POSIX操作系统,是利用查找,$ find . -name ofbiz-component.xml -exec grep test-suite {} \; -print
在MySQL上运行单元测试的注意事项
当运行单元测试在MySQL中使用事务请一定要知道一个断言失败,可发出回滚并导致事务数据在数据库中的一个不一致状态,有可能让测试通过,即使在现实中,却失败了。
发表评论
- 浏览: 64797 次
- 性别:

- 来自: 上海

- 详细资料
搜索本博客
最近加入圈子
最新评论
-
关于ofbiz中的data model
谢谢。现在也正在研究ofbiz。
-- by lfrick -
一个精简的jre
要是你说明是1.5的,我就不会浪费时间下了
-- by xieke -
birt的论坛
BIRT官方中文论坛:http://www.actuatechina.com/i ...
-- by RogerTu -
用appfuse2.0生成一个单独 ...
谢谢了,appfuse2我很看好就是资料太少了,请多写一些吧
-- by leonhe_cn -
不装杀毒软件制作一个百毒 ...
如果使用受限账户上网, 给你这个评论就是很大的问题! 我这是切换到管理员账号, ...
-- by fencer911






评论排行榜