简体   繁体   English

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

[英]How can I run all JUnit unit tests except those ending in “IntegrationTest” in my IntelliJ IDEA project using the integrated test runner?

I basically want to run all JUnit unit tests in my IntelliJ IDEA project (excluding JUnit integration tests), using the static suite() method of JUnit. 我基本上想要使用JUnit的static suite()方法在IntelliJ IDEA项目中运行所有JUnit 单元测试(不包括JUnit集成测试)。 Why use the static suite() method? 为什么要使用static suite()方法? Because I can then use IntelliJ IDEA's JUnit test runner to run all unit tests in my application (and easily exclude all integration tests by naming convention). 因为我可以使用IntelliJ IDEA的JUnit测试运行器在我的应用程序中运行所有单元测试(并通过命名约定轻松排除所有集成测试)。 The code so far looks like this: 到目前为止,代码如下所示:

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;
    }

}

The method getUnitTestClasses() should be rewritten to add all project classes extending TestCase, except if the class name ends in "IntegrationTest". 应该重写方法getUnitTestClasses()以添加扩展TestCase的所有项目类,除非类名以“IntegrationTest”结尾。

I know I can do this easily in Maven for example, but I need to do it in IntelliJ IDEA so I can use the integrated test runner - I like the green bar :) 我知道我可以在Maven中轻松地做到这一点,但是我需要在IntelliJ IDEA中这样做,所以我可以使用集成测试运行器 - 我喜欢绿色条:)

How about putting each major group of junit tests into their own root package. 如何将每个主要的junit测试组放入他们自己的root包中。 I use this package structure in my project: 我在我的项目中使用这个包结构:

test.
  quick.
    com.acme
  slow.
    com.acme

Without any coding, you can set up IntelliJ to run all tests, just the quick ones or just the slow ones. 如果没有任何编码,您可以设置IntelliJ来运行所有测试,只需快速测试或慢速测试。

I've written some code to do most of the work. 我写了一些代码来完成大部分工作。 It works only if your files are on the local disk instead of in a JAR. 仅当您的文件位于本地磁盘而不是JAR中时,它才有效。 All you need is one class in the package. 你需要的只是包中的一个类。 You could, for this purpose, create a Locator.java class, just to be able to find the package. 为此,您可以创建一个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;
    }
}

What about using JUnit4 and the Suite-Runner? 那么使用JUnit4和Suite-Runner呢?

Example: 例:

@RunWith(Suite.class)
@Suite.SuiteClasses({
UserUnitTest.class,
AnotherUnitTest.class
})
public class UnitTestSuite {}

I made a small Shell-Script to find all Unit-Tests and another one to find my Integration-Tests. 我制作了一个小的Shell脚本来查找所有单元测试,另一个用于查找我的集成测试。 Have a look at my blog entry: http://blog.timomeinen.de/2010/02/find-all-junit-tests-in-a-project/ 看看我的博客文章: http//blog.timomeinen.de/2010/02/find-all-junit-tests-in-a-project/

If you use Spring TestContext you can use the @IfProfile Annotation to declare different tests. 如果使用Spring TestContext,则可以使用@IfProfile Annotation声明不同的测试。

Kind regards, Timo Meinen 亲切的问候,Timo Meinen

Spring has implemented an excellent classpath search function in the PathMatchingResourcePatternResolver. Spring在PathMatchingResourcePatternResolver中实现了一个出色的类路径搜索功能。 If you use the classpath*: prefix, you can find all the resources, including classes in a given hierarchy, and even filter them if you want. 如果使用classpath *:前缀,则可以找到所有资源,包括给定层次结构中的类,甚至可以根据需要过滤它们。 Then you can use the children of AbstractTypeHierarchyTraversingFilter, AnnotationTypeFilter and AssignableTypeFilter to filter those resources either on class level annotations or on interfaces they implement. 然后,您可以使用AbstractTypeHierarchyTraversingFilter,AnnotationTypeFilter和AssignableTypeFilter的子项在类级别注释或它们实现的接口上过滤这些资源。

http://static.springsource.org/spring/docs/2.0.x/api/org/springframework/core/io/support/PathMatchingResourcePatternResolver.html http://static.springsource.org/spring/docs/2.0.x/api/org/springframework/core/io/support/PathMatchingResourcePatternResolver.html

http://static.springsource.org/spring/docs/2.5.x/api/org/springframework/core/type/filter/AbstractTypeHierarchyTraversingFilter.html http://static.springsource.org/spring/docs/2.5.x/api/org/springframework/core/type/filter/AbstractTypeHierarchyTraversingFilter.html

Solution: https://github.com/MichaelTamm/junit-toolbox 解决方案: https//github.com/MichaelTamm/junit-toolbox
Use the following features 使用以下功能

@RunWith(WildcardPatternSuite.class)
@SuiteClasses({"**/*.class", "!**/*IntegrationTest.class"})
public class AllTestsExceptionIntegrationSuit {
}

assuming you following a naming pattern where you integration tests end in ...IntegrationTest and you place the file in the top-most package (so the **/*.class search will have the opportunity to pick up all your tests) 假设您遵循命名模式,其中集成测试结束于... IntegrationTest并将文件放在最顶层的包中(因此** / * .class搜索将有机会获取所有测试)

暂无
暂无

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

相关问题 如何在 Maven 项目中的单元测试上运行 IntelliJ 调试器? - How can I run the IntelliJ debugger on unit tests in a Maven project? 无法在 IntelliJ IDEA 的 Maven 项目中运行 JUnit 测试 - Unable to run JUnit tests inside a Maven project in IntelliJ IDEA 有没有办法用我的Cucumber测试运行程序运行常规的jUnit测试? - Is there a way to run regular jUnit tests with my Cucumber test runner? 如何使用不同的JUnit Runner运行Spring单元测试? - How to run Spring unit tests with a different JUnit Runner? 是否可以在Intelij IDEA中创建顶级测试运行配置,以便我的所有JUnit测试都可以运行? - Is it possible to create a top-level test run configuration in Intelij IDEA so that all my JUnit tests run with it? 如何从命令行而不是 Intellij IDEA 运行 JUnit 测试? - How to run JUnit tests from the command line instead of Intellij IDEA? 在Intellij Idea上为Gradle Runner和JUnit测试设置环境变量 - Set up environment variables for Gradle Runner and JUnit tests on Intellij Idea 如何通过右键单击eclipse上的包并选择“Run as JUnit test”,在包中及下面(递归地)运行所有junit测试 - How can I run all junit tests in a package and below (recursively) by right clicking the package on eclipse and selecting “Run as JUnit test” 如何在Intellij Idea中运行Maven项目(并下载所有依赖项)? - How can I run a Maven project in Intellij Idea (and have all dependencies downloaded)? 如何在intellij IDEA中为Java项目添加单元测试? - How to add unit tests to Java project in intellij IDEA?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM