除了那些使用集成测试运行器在IntelliJ IDEA项目中以"IntegrationTest"结尾的JUnit单元测试之外,我该如何运行?

thv*_*hvo 12 java reflection junit unit-testing intellij-idea

我基本上想要使用JUnit的static suite()方法在IntelliJ IDEA项目中运行所有JUnit 单元测试(不包括JUnit集成测试).为什么要使用static suite()方法?因为我可以使用IntelliJ IDEA的JUnit测试运行器在我的应用程序中运行所有单元测试(并通过命名约定轻松排除所有集成测试).到目前为止,代码如下所示:

package com.acme;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class AllUnitTests extends TestCase {

    public static Test suite() {
        List classes = getUnitTestClasses();
        return createTestSuite(classes);
    }

    private static List getUnitTestClasses() {
        List classes = new ArrayList();
        classes.add(CalculatorTest.class);
        return classes;
    }

    private static TestSuite createTestSuite(List allClasses) {
        TestSuite suite = new TestSuite("All Unit Tests");
        for (Iterator i = allClasses.iterator(); i.hasNext();) {
            suite.addTestSuite((Class<? extends TestCase>) i.next());
        }
        return suite;
    }

}
Run Code Online (Sandbox Code Playgroud)

应该重写方法getUnitTestClasses()以添加扩展TestCase的所有项目类,除非类名以"IntegrationTest"结尾.

我知道我可以在Maven中轻松地做到这一点,但是我需要在IntelliJ IDEA中这样做,所以我可以使用集成测试运行器 - 我喜欢绿色条:)

Arn*_*son 8

如何将每个主要的junit测试组放入他们自己的root包中.我在我的项目中使用这个包结构:

test.
  quick.
    com.acme
  slow.
    com.acme
Run Code Online (Sandbox Code Playgroud)

如果没有任何编码,您可以设置IntelliJ来运行所有测试,只需快速测试或慢速测试.

  • 这个答案仍然有效.test/quick和test/slow是测试源根,其中任何一个中的测试仍然在com.acme包中. (2认同)

Roe*_*ker 7

我写了一些代码来完成大部分工作.仅当您的文件位于本地磁盘而不是JAR中时,它才有效.你需要的只是包中的一个类.为此,您可以创建一个Locator.java类,以便能够找到该包.

public class ClassEnumerator {
    public static void main(String[] args) throws ClassNotFoundException {
        List<Class<?>> list = listClassesInSamePackage(Locator.class, true);

        System.out.println(list);
    }

    private static List<Class<?>> listClassesInSamePackage(Class<?> locator, boolean includeLocator) 
                                                                      throws ClassNotFoundException {

        File packageFile = getPackageFile(locator);

        String ignore = includeLocator ? null : locator.getSimpleName() + ".class";

        return toClassList(locator.getPackage().getName(), listClassNames(packageFile, ignore));
    }

    private static File getPackageFile(Class<?> locator) {
        URL url = locator.getClassLoader().getResource(locator.getName().replace(".", "/") + ".class");
        if (url == null) {
            throw new RuntimeException("Cannot locate " + Locator.class.getName());
        }

        try {
        return new File(url.toURI()).getParentFile();
        }
        catch (URISyntaxException e) {
            throw new RuntimeException(e);
        }
    }

    private static String[] listClassNames(File packageFile, final String ignore) {
        return packageFile.list(new FilenameFilter(){
            @Override
            public boolean accept(File dir, String name) {
                if (name.equals(ignore)) {
                    return false;
                }
                return name.endsWith(".class");
            }
        });
    }

    private static List<Class<?>> toClassList(String packageName, String[] classNames)
                                                             throws ClassNotFoundException {

        List<Class<?>> result = new ArrayList<Class<?>>(classNames.length);
        for (String className : classNames) {
            // Strip the .class
            String simpleName = className.substring(0, className.length() - 6);

            result.add(Class.forName(packageName + "." + simpleName));
        }
        return result;
    }
}
Run Code Online (Sandbox Code Playgroud)


tim*_*nen 5

那么使用JUnit4和Suite-Runner呢?

例:

@RunWith(Suite.class)
@Suite.SuiteClasses({
UserUnitTest.class,
AnotherUnitTest.class
})
public class UnitTestSuite {}
Run Code Online (Sandbox Code Playgroud)

我制作了一个小的Shell脚本来查找所有单元测试,另一个用于查找我的集成测试.看看我的博客文章:http: //blog.timomeinen.de/2010/02/find-all-junit-tests-in-a-project/

如果使用Spring TestContext,则可以使用@IfProfile Annotation声明不同的测试.

亲切的问候,Timo Meinen