简体   繁体   English

如何过滤源路径从eclipse调用的JUNIT测试

[英]How to filter JUNIT Tests that are called from eclipse by source path

Assuming that I have a set of source folders in an Eclips project: 假设我在Eclips项目中有一组源文件夹:

MyProject
  base/src/impl
  base/src/test
  common/src/api
  common/src/test
  common/src/junit

Note that these are not packages, they are source folders, inside each one there's a package hierarchy, which can be just the same. 请注意,这些不是包,它们是源文件夹,每个包内都有一个包层次结构,它们可以是相同的。 Let's say if I have a class com.foo.myproject.DoSomething in the base/src/impl folder, I might have a Test called com.foo.myproject.DoSomethingTest in the base/src/Test folder, and maybe com.foo.myproject.DoSomethingUnitTest in another source folder under same package hierarchy. 假设我在base / src / impl文件夹中有一个类com.foo.myproject.DoSomething,我可能在base / src / Test文件夹中有一个名为com.foo.myproject.DoSomethingTest的Test,也许是com.foo .myproject.DoSomethingUnitTest在同一包层次结构下的另一个源文件夹中。

What I need to do, is run all tests that are under certain source folders. 我需要做的是运行某些源文件夹下的所有测试。 Maybe all that are in a junit folder, but none from the test folder. 也许所有这些都在junit文件夹中,但没有来自测试文件夹。

This works fine from Ant with the junit search patterns, for example: 这可以通过带有junit搜索模式的Ant工作得很好,例如:

<include name="**/src/junit" />

However, I want to be able to have exactly the same run configurations from Eclipse without having to call Ant. 但是,我希望能够从Eclipse获得完全相同的运行配置,而无需调用Ant。 Since there are already many projects, packages and thousands of Test Classes, I am looking for a "lazy" way to get this done, that means, without having to change project structures or having to mantain complex test suites. 由于已经有很多项目,包和数千个测试类,我正在寻找一种“懒惰”的方式来完成这项工作,这意味着,无需更改项目结构或必须保留复杂的测试套件。

One Approach I have tried is to use the org.springframework.core.io.support.PathMatchingResourcePatternResolver used by ClasspathSuite, but looking for resources in classpath always looks at the merged source folders in one single classes location, so I am not able to filter anymore at the level I intend to do it. 我尝试过的一种方法是使用ClasspathSuite使用的org.springframework.core.io.support.PathMatchingResourcePatternResolver ,但是在classpath中查找资源总是在一个单独的类位置查看合并的源文件夹,所以我无法过滤在我打算做的水平上。

I am looking for two ways of doing it: 我正在寻找两种方法:

1. Filter using some TestSuite
2. Filter using some JUNIT Runner Plugin in Eclipse (is there any that can do what I need?)

Thanks! 谢谢!

You can right-click the source folder, and select Run As> JUnit Test. 您可以右键单击源文件夹,然后选择Run As> JUnit Test。

This will run the unit tests in this source folder only. 这将仅在此源文件夹中运行单元测试。


Alternatively, when editing the Run Configuration, you can select 或者,在编辑“运行配置”时,您可以选择

运行配置

Run all tests in the selected project, package or source folder:

Press search and select the folder you wish to run the unit tests of. search并选择您要运行单元测试的文件夹。

See "Customizing a Test Configuration" on Writing and Running JUnit tests of the Eclipse Help. 请参阅编写和运行 Eclipse帮助的JUnit测试中的“自定义测试配置”。


Same page describes how to create a custom Test Suite using the New Test Suite wizard. 同一页面介绍了如何使用New Test Suite向导创建自定义Test Suite。

新测试套件向导

Here is a project that seems to make what you want: 这是一个似乎可以满足你想要的项目:

ClasspathSuite ClasspathSuite

Look for "Classpath Property Annotation" in order to set a folder from where the tests are taken. 查找“Classpath属性注释”以设置从中进行测试的文件夹。

Besides, if your test classes are really named as they seam to (all Unit Tests classes have the suffix UnitTest), then you could also use the @ClassnameFilters annotation. 此外,如果您的测试类确实被命名为接缝到(所有单元测试类都具有后缀UnitTest),那么您还可以使用@ClassnameFilters注释。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM