自动化单元测试(junit)用于Eclipse插件开发

16
我正在开发Eclipse插件,需要能够自动化构建和执行每个插件的测试套件(使用Junit)。测试在Eclipse内部工作,并且我可以像这里这里、以及这里所描述的那样将插件拆分为实际的插件和用于单元测试的片段插件。然而,以上每种方法都存在同样的问题:发出构建或应该触发测试的Java ant任务/命令不会产生任何可观察到的副作用,并返回值“13”。我已经尝试了我能找到的所有方法,并且对Eclipse启动方式有了相当多的了解(例如:自从v3.3以来,你不能再使用startup.jar-- 它不存在了--但您应该使用org.eclipse.equinox.launcher)。不幸的是,虽然这显然是必要的信息,但远远不够。我正在使用Eclipse 3.4、Junit 4.3.1(org.junit4 bundle,但我更愿意使用JUnit 4.4。请参见这里)。那么,我的问题是:如何精确地自动化构建和测试Eclipse插件?编辑:为了澄清,我想使用类似ant + cruise control的东西,但我甚至无法在Eclipse之外完全运行单元测试。我说“类似”是因为还有其他技术可以实现相同的功能,如果这些技术能够使得此过程更加容易,我并不介意例如使用Maven或Buckminster等解决方案。编辑2:上面提到的“Java Result 13”似乎是由于找不到coretestrunner引起的。从日志中可以看出:
java.lang.RuntimeException: Application "org.eclipse.test.coretestapplication" could not be found in the registry. The applications available are: org.eclipse.equinox.app.error, com.rcpquickstart.helloworld.application.
    at org.eclipse.equinox.internal.app.EclipseAppContainer.startDefaultApp(EclipseAppContainer.java:242)
    at org.eclipse.equinox.internal.app.MainApplicationLauncher.run(MainApplicationLauncher.java:29)
    at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:110)
    at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:79)
    at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:382)
    at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:179)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:549)
    at org.eclipse.equinox.launcher.Main.basicRun(Main.java:504)
    at org.eclipse.equinox.launcher.Main.run(Main.java:1236)
    at org.eclipse.equinox.launcher.Main.main(Main.java:1212)
    at org.eclipse.core.launcher.Main.main(Main.java:30)

!ENTRY org.eclipse.osgi 2 0 2008-11-04 21:02:10.514
!MESSAGE The following is a complete list of bundles which are not resolved, see the prior log entry for the root cause if it exists:
!SUBENTRY 1 org.eclipse.osgi 2 0 2008-11-04 21:02:10.515
!MESSAGE Bundle update@plugins/org.eclipse.test_3.2.0/ [34] was not resolved.
!SUBENTRY 2 org.eclipse.test 2 0 2008-11-04 21:02:10.516
!MESSAGE Missing required bundle org.apache.ant_0.0.0.
!SUBENTRY 2 org.eclipse.test 2 0 2008-11-04 21:02:10.516
!MESSAGE Missing required bundle org.eclipse.ui.ide.application_0.0.0.
!SUBENTRY 1 org.eclipse.osgi 2 0 2008-11-04 21:02:10.518
!MESSAGE Bundle update@plugins/org.eclipse.ant.optional.junit_3.2.100.jar [60] was not resolved.
!SUBENTRY 2 org.eclipse.ant.optional.junit 2 0 2008-11-04 21:02:10.519
!MESSAGE Missing host org.apache.ant_[1.6.5,2.0.0).
!SUBENTRY 2 org.eclipse.ant.optional.junit 2 0 2008-11-04 21:02:10.519
!MESSAGE Missing required bundle org.eclipse.core.runtime.compatibility_0.0.0.

1
你是否曾经想过实现目标的实际方法? - mP.
我使用 tycho surefire plugin 自动化了我的 Eclipse 插件的构建和测试。它运行得非常顺畅。 - Frederic
7个回答

12

我刚刚在我们的RCP应用程序的无界面构建中使JUnit测试工作起来了。

我发现这篇文章 - 使用Ant自动化Eclipse PDE单元测试非常有帮助。它提供了代码和方法让你开始。但是,我发现了一些问题:

关于该文章的代码

  • 只有一个测试包(我们已经将构建过程与代码分开,使用Buckminster
  • 只有一个测试类。
  • 这两者在构建脚本中都是硬编码的

关于Eclipse PDE

  • uitestapplication需要另一个testApplication。使用coretestapplication则不需要。
  • 由于这些应用程序都依赖于SWT,这在大多数情况下都是致命的,但如果您的构建机器是Windows框,则不是。我希望能将它们拆分为非UI包。

我发现提供的代码是一个很好的起点,但其中有很多上述假设对其实现具有隐式意义。

在发现了这些假设后,工作相对来说就比较简单了。

我们全新的设置

  • buckminster构建包。
  • target从目标平台复制包,org.eclipse.pde.runtime和org.eclipse.jdt.junit到“tester-eclipse-install”。这应该可以解决您的Java Result 13问题。
  • 查找测试片段并查看工作区
  • 从清单中查找片段主机
  • 从工作区中的项目中查找测试类。
  • 注册一个修改过的PDETestListener以处理多个测试类
  • 使用多个测试类调用tester-eclipse-install。

我也阅读过 插件和特性的构建和测试自动化,但我们并没有直接使用PDE-Build。


3

如果你仍在寻找在Eclipse之外执行Eclipse插件测试的方法,下面的命令可以适用:

java -Xms40m -Xmx1024m -XX:MaxPermSize=512m -Dorg.eclipse.swt.browser.DefaultType=mozilla -Declipse.pde.launch=true -classpath C:\eclipse\eclipse-standard-luna-M2-win32-x86_64\eclipse\plugins/org.eclipse.equinox.launcher_1.3.0.v20130327-1440.jar org.eclipse.equinox.launcher.Main -port 22 -testLoaderClass org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader -loaderpluginname org.eclipse.jdt.junit4.runtime -classNames testpackage.testClass -application org.eclipse.pde.junit.runtime.uitestapplication -data C:\temp\log.temp -dev bin -consoleLog -testpluginname PluginName

-classpath 应该设置为 Eclipse 启动器 jar 文件。您可以从 eclipse.ini 文件中获取适用于您的 Eclipse 的确切版本。

-className 是 JUnit 插件测试文件名。

-data 被设置为临时文件。

-testpluginname 是您要测试的插件名称。


1
无法连接到: :22 - Jerry Miller
选择端口的标准是什么?或者更普遍地说,有关此目的选项的更详细文档在哪里可以找到?(我刚刚查了一下22号端口:ssh。为什么是SSH端口?) - Jerry Miller
我尝试使用端口80,但它再次崩溃了,但似乎在崩溃之前会暂时打开一个浏览器窗口?!?! - Jerry Miller
你使用的是哪个版本的Eclipse?看起来在较新的Eclipse版本中可能不需要传递端口。 - Gunjan Aggarwal
你可以通过查看“org.eclipse.equinox.launcher.Main”的代码(http://grepcode.com/file/repository.grepcode.com/java/eclipse.org/4.2/org.eclipse.equinox/launcher/1.3.0/org/eclipse/equinox/launcher/Main.java#Main.processCommandLine%28java.lang.String%5B%5D%29)来了解这些参数的一些想法。 - Gunjan Aggarwal
请检查 processCommandLine 函数(http://grepcode.com/file/repository.grepcode.com/java/eclipse.org/4.2/org.eclipse.equinox/launcher/1.3.0/org/eclipse/equinox/launcher/Main.java#1496) - Gunjan Aggarwal

2
看到您的异常信息,它说核心测试应用程序丢失。ant目标可以在plugins/org.eclipse.test_3.1.0/library.xml:10中找到。
实际上这是一个依赖问题。Eclipse需要所有插件才能构建。
要正确配置它,有两个文件需要查看。
1. 产品文件 2. feature.xml
请确保产品文件包含您所需的所有插件。
之后,添加org.eclipse.rcp和org.eclipse.test功能。
... 插件在上面 ...
<features>
      <feature id="mock_feature" version="1.0.0"/>
      <feature id="mock_feature_test" version="1.0.0"/>
      <feature id="org.eclipse.rcp" version="3.2.0.v20060609m-SVDNgVrNoh-MeGG"/>
      <feature id="org.eclipse.test" version="3.2.0.v20060220------0842282442"/>
 </features>

您需要使用org.eclipse.test来运行测试,使用org.eclipse.rcp来启动Eclipse以便运行测试。

不要忘记将useFeatures设置为“true”

<product name="mock" id="com.example.mock" application="com.example.mock.application" useFeatures="true">

feature.xml

假设您有一个用于测试的功能,您必须添加两个额外的插件。

... 其他插件在上面 ...

<plugin
         id="org.apache.ant"
         download-size="0"
         install-size="0"
         version="0.0.0"/>

   <plugin
         id="org.eclipse.core.runtime.compatibility"
         download-size="0"
         install-size="0"
         version="0.0.0"
         unpack="false"/>

测试需要org.apache.ant来运行测试和org.eclipse.core.runtime.compatibility来启动。
另一个要注意的问题是,在您构建的目标eclipse中,每个插件只有1个副本。例如,如果插件文件夹中有2个com.ibm.icu插件的版本,则Eclipse将使用较新的版本。由于pde构建插件已配置使用特定版本,即使该插件存在,Eclipse也会抱怨找不到特定插件。
一些想法:
构建整个Eclipse的过程可能会更好。实际上,我大部分时间都是通过试错来进行的。文档已过时且稀少。错误消息无法帮助。它只让你感到无助和沮丧。希望这篇文章能帮助其他程序员节省一些时间!

0

作为 Ant 的替代方案,我在使用全新的 Maven+Tycho 和 Hudson 时有很好的经验。Tycho 在 Maven 中提供了完整的 Osgi 和 Eclipse 开发支持。它目前正在进行大量开发,但我需要的大多数功能都可以正常工作。它只需要非常少的配置,因为它可以解析 MANIFEST.MF 文件。

如果您有一些 Maven 的经验,那么开始使用它并不是很难。Hudson 有点更棘手,因为缺少 Maven 3 支持。(Tycho 使用 Maven 3 的开发版本)

开始的链接:


0
我们正在使用PDE构建脚本(参见this question),并为我们的单元测试插件导出ant构建文件。然后使用"ant" ant-task从PDE构建脚本(customTargets.xml)中调用这些ant构建脚本。不幸的是,这只适用于JUnit3。据说有一个适用于JUnit3的JUnit4适配器,这样您就可以从JUnit3测试运行器中运行JUnit4测试。
我们可能会转移到类似Maven的东西;PDE构建脚本并不真正适合我们需要做的事情。

-1

这里有一个工具,如果有人对TDD感兴趣,我可以推荐一下: Infinitest

从Infinitest网站上摘取的简短描述:

什么是Infinitest?

Infinitest是一个持续测试运行器,旨在促进测试驱动开发。Infinitest通过提供您工作时的反馈来帮助您学习TDD,并通过将反馈周期从几分钟缩短到几秒钟来帮助您掌握TDD。

每当您更改类时,Infinitest会为您运行测试。它会智能地确定要运行哪些测试,并仅运行您需要的测试。如果出现任何错误,它会清晰而简洁地报告它们。这使您可以立即获得有关代码语义正确性的反馈,就像现代IDE提供有关语法错误的即时反馈一样。


我不明白这与我的问题有什么关系 - 你能否详细解释一下? - rcreswick
这个工具是一个持续的JUnit测试运行器。Infinitest能够自动化执行JUnit测试,因此我推荐给你。 - Fred
啊...如果我现在正在尝试的东西不奏效,我会更深入地研究一下。 - rcreswick

-1
使用AntCruiseControl - 您可以在Ant脚本中调用单元测试以及其余的构建逻辑,并且可以在每次构建迭代中运行它们 - 然后CruiseControl可以自动化您的构建调用并在每次运行时运行这些测试。

我已经尝试澄清了 - 使用Ant需要我能够在Eclipse之外运行单元测试,但是到目前为止我还没有做到,正如问题所述。 - rcreswick

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接