Eclipse 构建时和运行时类路径,可轻松执行 junit 测试用例
我有一个包含多个 junit 类的大型 Eclipse 项目。我试图在向 eclipse 项目类路径添加运行时资源和配置多个 junit 启动配置的需要之间取得平衡。
我意识到默认的 eclipse 构建类路径是由所有单元测试配置继承的,但我的一些测试需要额外的运行时资源。我可以将这些资源添加到构建类路径中,但这确实会减慢我的整体项目构建时间(因为 它必须保持更多文件同步)。我不喜欢在运行时类路径上包含 * 资源和 jar 的想法。
我有两个选项,我看到的正面和负面情况都列出了
1:将所有运行时资源添加到 eclipse 类路径。
- POS 我可以选择一个单元测试并运行它,而无需配置测试类路径。
- POS 构建类路径上的额外资源意味着 Eclipse 速度变慢。
- NEG 更难以确保每次测试都使用正确的资源。
2:配置每个单元测试 POS 的类路径
- 我确切地知道测试正在使用哪些资源。
- POS 较小的构建类路径意味着 Eclipse 的构建和执行速度更快。
- NEG 必须设置多个单独的 junit 运行时类路径是一件痛苦的事情。
理想情况下,我想配置一个基本的 junit 运行时配置,它采用默认的 eclipse 构建类路径,添加额外的运行时 jar 和资源。然后,特定的 junit 测试用例可以重用此配置,这样的事情可能吗?
查看可以导出到共享项目文件的特定 junit 启动配置,
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<launchConfiguration type="org.eclipse.jdt.junit.launchconfig">
<stringAttribute key="bad_container_name" value="/CR-3089_5_1_branch."/>
<listAttribute key="org.eclipse.debug.core.MAPPED_RESOURCE_PATHS">
<listEntry value="/CR-3089_5_1_branch/src/com/x/y/z/ParserJUnitTest.java"/>
</listAttribute>
<listAttribute key="org.eclipse.debug.core.MAPPED_RESOURCE_TYPES">
<listEntry value="1"/>
</listAttribute>
<stringAttribute key="org.eclipse.jdt.junit.CONTAINER" value=""/>
<booleanAttribute key="org.eclipse.jdt.junit.KEEPRUNNING_ATTR" value="false"/>
<stringAttribute key="org.eclipse.jdt.junit.TESTNAME" value=""/>
<stringAttribute key="org.eclipse.jdt.junit.TEST_KIND" value="org.eclipse.jdt.junit.loader.junit4"/>
<stringAttribute key="org.eclipse.jdt.launching.MAIN_TYPE" value="com.x.y.z.ParserJUnitTest"/>
<stringAttribute key="org.eclipse.jdt.launching.PROJECT_ATTR" value="CR-3089_5_1_branch"/>
</launchConfiguration>
是否可以扩展/重用此配置,并参数化“org.eclipse.jdt.launching.MAIN_TYPE”值?
我知道用于配置类路径的 commons launch 和 jar 清单文件解决方案,但它们似乎都假设在执行测试之前运行 ant 构建。我想避免任何 Eclipse 依赖于调用 ant 目标 重构代码并执行测试。
基本上 - 分离和维护 eclipse 构建时类路径和 junit 运行时类路径的最简单方法是什么?
I've a large eclipse project with multiple junit classes. I'm trying to strike a balance between adding runtime resources to the eclipse project classpath, and the need to configure mutliple junit launch configurations.
I realise the default eclipse build classpath is inherited by all unit test configurations, but some of my tests require extra runtime resources. I could add these resources to the build classpath, but this does slow my overall project build time (since
it has to keep more files in synch). I don't like the idea of including * resources and jars on the runtime classpath.
The two options that i have are these, the positive and negative cases as i see it are listed
1 : Add all runtime resources to eclipse classpath.
- POS I can select a unit test and run it without having to configure the test classpath.
- POS Extra resources on build classpath means eclipse slows down.
- NEG More difficult to ensure each test uses the correct resources.
2 : Configure the classpath of each unit test
- POS I know exactly what resources are being used by a test.
- POS Smaller build classpath means quicker build and execution by eclipse.
- NEG Its a pain having to setup multiple separate junit runtime classpaths.
Ideally i'd like to configure one base junit runtime configuration, which takes the default eclipse build classpath, adds extra runtime jars and resources. This configuration could then be reused by the specific junit test cases, Is anything like this possible?
Looking at a specific junit launch configuration which can be exported to a share project file
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<launchConfiguration type="org.eclipse.jdt.junit.launchconfig">
<stringAttribute key="bad_container_name" value="/CR-3089_5_1_branch."/>
<listAttribute key="org.eclipse.debug.core.MAPPED_RESOURCE_PATHS">
<listEntry value="/CR-3089_5_1_branch/src/com/x/y/z/ParserJUnitTest.java"/>
</listAttribute>
<listAttribute key="org.eclipse.debug.core.MAPPED_RESOURCE_TYPES">
<listEntry value="1"/>
</listAttribute>
<stringAttribute key="org.eclipse.jdt.junit.CONTAINER" value=""/>
<booleanAttribute key="org.eclipse.jdt.junit.KEEPRUNNING_ATTR" value="false"/>
<stringAttribute key="org.eclipse.jdt.junit.TESTNAME" value=""/>
<stringAttribute key="org.eclipse.jdt.junit.TEST_KIND" value="org.eclipse.jdt.junit.loader.junit4"/>
<stringAttribute key="org.eclipse.jdt.launching.MAIN_TYPE" value="com.x.y.z.ParserJUnitTest"/>
<stringAttribute key="org.eclipse.jdt.launching.PROJECT_ATTR" value="CR-3089_5_1_branch"/>
</launchConfiguration>
is it possible to extend/reuse this configuration, and parameterise the 'org.eclipse.jdt.launching.MAIN_TYPE' value?
I'm aware of the commons launch and jar manifest file solutions to configuring the classpath, but they both seem to assume that an ant build is run before the test can execute. I want to avoid any eclipse dependency on calling an ant target when
refactoting code and executing tests.
Basically - What is the the easiest way to seperate and maintain the eclipse buildtime classpath and junit runtime classpaths?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
第一个解决方案可能是将项目与其单元测试完全分离:
一种更简洁的方法(因为它将在 Eclipse 内使用,但也在 Eclipse 外部使用)是将您的项目转换为 Maven 项目, 例如通过 m2eclipse。
这样,maven 可以管理多个项目的依赖组合,以便为测试任务提供正确的类路径。
The first solution could be to separate completely the project from its unit tests:
A cleaner way (because it would be used within Eclipse, but also outside Eclipse) would be to transform your project in a maven one, through m2eclipse for instance.
That way, maven can manage the dependency compositions of several projects in order to have the right classpath for the test task.