是否可以从Eclipse中的多个包运行JUnit测试?

是否可以从Eclipse中的多个包运行JUnit测试?

问题描述:

可以同时运行多个程序包的JUnit测试,而无需手动创建测试套件。

Is it possible to run JUnit tests for multiple packages at the same time without manually creating test suites.

例如,如果我有层次结构:

For example if I have the hierarchy:

code.branchone

code.branchone.aaa

code.branchone.bbb

code。 branchtwo

code.branchtwo.aaa

code.branchtwo.bbb

code.branchone
code.branchone.aaa
code.branchone.bbb
code.branchtwo
code.branchtwo.aaa
code.branchtwo.bbb

是否可以:

Is it possible to:


  1. 运行code.branchone和后代包中的所有测试

  2. 运行所有测试code.branchone.aaa和code.branchtwo.bbb

我用手动创建测试套件的问题是,当新的测试来了你可能会忘记添加它们。

The problem I see with manually creating test suites is that when new tests come along you may forget to add them.

是的,这是可能的。至少我最简单的方法是添加一个测试套件类。它可以这样:

Yes, it is possible. The easiest way for me at least is to add a test suite class. It can look like this:

package tests;

import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;

import tests.message.ATest;
import tests.validator.BTest;
import tests.validator.CTest;
import tests.validator.DTest;

@RunWith(Suite.class)
@SuiteClasses({ ATest.class, 
        BTest.class, 
        CTest.class, 
        DTest.class })
public class AllTests {

}

允许您测试您导入的任何类,无论它是什么包。要在eclipse中运行,只需右键单击AllTests类并将其作为JUnit测试运行。然后,它将运行您在 @SuiteClasses 中定义的所有测试。

This will allow you to test any class that you import no matter what package it is in. To run this in eclipse you just right click the AllTests class and run it as JUnit test. It will then run all the tests you define in @SuiteClasses.

这将与链接源一起工作,我一直使用它。

This will work with linked sources as well, I use it all the time.