在无头环境中对 SWT 和 Swing 应用程序进行单元测试的最简单方法是什么?

发布于 2024-08-07 12:23:33 字数 171 浏览 4 评论 0 原文

我希望对我正在开发的项目进行一些 SWT 和 Swing 代码的单元测试,只要我从 Eclipse 运行它们,测试就可以正常运行。

一旦我在 hudson 环境中运行它们,它就会失败,因为 hudson 在无头模式下运行测试。

这样做的最佳方法是什么?请仅使用开源解决方案(因为该项目是开源的)。

I'm looking to unit test some SWT and Swing code for a project I'm working on and the tests run fine as long as I'm running them from eclipse.

As soon as I run them in my hudson environment it fails since hudson runs the tests in headless mode.

What's the best way of doing this? Open source solutions only please (since the project is open source).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

著墨染雨君画夕 2024-08-14 12:23:33

您可以运行 Xvfb (X 虚拟帧缓冲区,X11服务器在内存中执行所有图形操作)并且效果很好。

但是 Hudson 的 Xvnc 插件还有另一种解决方案。只需安装插件并选中作业配置屏幕中的复选框即可:

alt text http ://www.justinedelson.com/blog/wp-content/uploads/xvnc_box.jpg

然后 Hudson 将自动启动 Xvnc 会话并将 DISPLAY 环境变量设置为适当的值,然后在以下情况下关闭会话:构建完成。与 Xvfb 方法相比,这种方法的一个优点是,如果您同时构建多个 Swing/SWT 项目,则每个构建都有自己的 X 会话。这可能根本不是问题,但似乎是个好主意。

在使用这个插件之前,你显然必须安装 Xvnc。不太明显(尽管合理)的是您还必须设置密码。您可以通过运行以下命令来完成此操作:

$ vncpassword

这必须与运行 Hudson 的用户相同来完成。

You could run Xvfb (X virtual framebuffer, an X11 server that performs all graphical operations in memory) and this works fine.

But there is another solution with Hudson's plugin for Xvnc. Simply install the plugin and check the checkbox in the job configuration screen:

alt text http://www.justinedelson.com/blog/wp-content/uploads/xvnc_box.jpg

Hudson will then automatically start up a Xvnc session and set the DISPLAY environment variable to the appropriate value and then shut down the session when the build is complete. One advantage this has over the Xvfb method is that if you have multiple Swing/SWT projects building simultaneously, each build has its own X session. This may not be an issue at all but it seems like a good idea.

Before using this plugin, you obviously have to have Xvnc installed. What's less obvious (although sensible) is that you must also set a password. You do this by running:

$ vncpassword

This has to be done as the same user Hudson runs as.

凡间太子 2024-08-14 12:23:33

尝试使用 Abbot Java GUI 测试框架SWTbot.至少 SWTbot 应该能够做到

如果两者都不提供无头模式,则这篇博文 可能会给您一些如何摆脱 UI 进行测试的想法。

Try the Abbot Java GUI Testing Framework and SWTbot. At least SWTbot should be able to do it.

If neither offers a headless mode, then this blog post might give you some ideas how to get rid of the UI for testing.

谎言 2024-08-14 12:23:33

我确信我之前曾在这里发布过此内容,但不确定发生了什么。

Cacio 允许无头运行 Swing 应用程序。

http://rkennke.wordpress.com/2011/10/ 17/cacio-for-ui-testing/

I was sure I posted this here before, not sure what happened to it.

Cacio allows for running Swing app headless.

http://rkennke.wordpress.com/2011/10/17/cacio-for-ui-testing/

可爱暴击 2024-08-14 12:23:33

我倾向于使用 Swing 来组织事物,以便可以在顶部没有窗口的情况下创建组件树。这样做可以让您在单元测试中简单地创建一个 JPanel 并将其用作顶级组件。有些东西您无法测试,例如焦点以及为正常操作创建框架所涉及的任何逻辑,但绝大多数都可以测试。

您可能想研究一下 FEST 库,让生活变得更轻松,无论您是否无头,它看起来都非常好:http://fest.easytesting.org/swing/wiki/pmwiki.php

Using Swing I tend to organise things so that the component tree can be created without a Window at the top. Doing this allows you to simply create a JPanel in a unit test and use that as your top-level component. There are certain things you cannot test, such as focus and any logic involved in the creation of the Frame for normal operation, but the vast majority can be tested.

You may want to look into the FEST library to make life easier whether you go headless or not, it looks very good: http://fest.easytesting.org/swing/wiki/pmwiki.php

柠檬色的秋千 2024-08-14 12:23:33

我不了解 SWT,但使用 Swing 就不行。任何 Window 实例(JFrame、JDialog 等)即使从未设置为可见,也会在无头模式下崩溃(在 JDK 5 上)。我们所做的不是在无头模式下运行,而是安装 Xvfb 来提供窗口,而实际上没有安装了真正的窗口系统。

I don't know about SWT, but with Swing you can't. Any instantiation of a Window (JFrame, JDialog, etc.) even if it is never set to visible will blow up in headless mode (on JDK 5). What we did was not run in headless mode and install Xvfb to provide the windowing without actually having a real windowing system installed.

轮廓§ 2024-08-14 12:23:33

您可以尝试 RedDeer 测试框架
https://github.com/jboss-reddeer/reddeer

• 支持在 Jenkins 中运行测试CI环境(类似Hudson)

You could try RedDeer Testing framework
https://github.com/jboss-reddeer/reddeer

• Support for running tests in a Jenkins CI environment (Hudson like)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文