问题描述
我有一些JUnit测试包含在.jar
中,该测试旨在用作库.该库包含一些在其他项目中使用该库时应运行的测试.
I have some JUnit tests that contained in a .jar
that is intended to be used as a library. The library contains some tests that should be run whenever the library is used in another project.
但是,当我使用该库创建一个新项目并在Eclipse中对其运行JUnit时,依赖项.jar
中的测试就不会运行/不会被JUnit测试运行程序检测到.我收到消息:
However when I create a new project using the library and run JUnit on it in Eclipse then the tests in the dependency .jar
don't run / don't get detected by the JUnit test runner. I get the message:
No tests found with test runner 'JUnit 4'.
有没有一种方法可以配置依赖项.jar
,以便测试可以与主项目中可能包含的所有测试一起运行?
Is there a way I can configure the dependency .jar
so that the tests will run alongside any tests that might be contained in the main project?
基本上,我希望依赖项.jar将测试导出"到使用它的任何项目中.
Basically I want the dependency .jar to "export" the tests to whatever projects it is used in.
我正在使用Eclipse Juno,JUnit 4.10和Maven进行依赖性管理.
I'm using Eclipse Juno, JUnit 4.10, and Maven for the dependency management.
该库的要点是能够帮助测试使用它的项目-即它运行一些专门的测试.这就是为什么我希望能够导入库.jar并使它为导入项目贡献额外的测试.
The point of this library is to be able to help test projects that use it - i.e. it runs some specialised tests. This is why I want to be able to import the library .jar and have it contribute the extra tests to the importing project.
推荐答案
我认为建立一个单元测试库(带有@Test
注释的方法)是一个坏主意.但是,建立一个可重用的测试组件库是一个不错的选择.我们已经在几个开源项目中做到了这一点,您可以看看它是如何工作的.
I think that making a library of unit tests (@Test
annotated methods) is a bad idea. However, making a library of reusable test components is a good one. We've done this in a few open source projects, and you can take a look how it works.
-
一个Maven模块从
src/mock/java
目录导出测试组件(我们称它们为模拟").导出的工件具有-mock
分类器.参见 rexsl/pom.xml (请注意突出显示的行).
One Maven module exports test components (we call them "mocks"), from
src/mock/java
directory. Exported artifact has-mock
classifier. See rexsl/pom.xml (pay attention to highlighted lines).
模拟工件与常规工件一起被部署到Maven Central: http://repo1.maven.org/maven2/com/rexsl/rexsl-core/0.3.8/(请注意...-mock.jar
文件)
Mock artifacts are being deployed to Maven Central, together with usual artifacts: http://repo1.maven.org/maven2/com/rexsl/rexsl-core/0.3.8/ (pay attention to ...-mock.jar
files)
需要模拟的模块可以将它们作为常规工件包括在内,例如 rexsl-core/pom.xml (请参见突出显示的行):
Modules that need that mocks can include them as usual artifacts, for example rexsl-core/pom.xml (see highlighted lines):
然后,在单元测试中,仅使用该模拟库中的类,例如常规的模拟生成器,例如: BulkHttpFeederTest
Then, in your unit tests just use the classes from that mock libraries, like regular builders of mocks, for example: BulkHttpFeederTest
这就是您如何以一种优雅的方式使您的测试工件可重复使用的方法.希望对您有所帮助.
That's how you can make your test artifacts reusable, in an elegant way. Hope it helps.
这篇关于从Eclipse中的依赖项jar运行JUnit测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!