Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何对大量JUnit测试进行分组/分类_Java_Junit - Fatal编程技术网

Java 如何对大量JUnit测试进行分组/分类

Java 如何对大量JUnit测试进行分组/分类,java,junit,Java,Junit,在我们的项目中,我们目前有大量(junit)测试,这些测试分为三类:单元测试、集成测试和wicket测试 我现在想对这些测试进行分组,以便只能运行其中一个(或两个)类别。我唯一发现的是junit测试套件和类别,如下所述: 我的问题是,我不想用@SuiteClasses声明测试套件中的每个测试 有没有办法添加带有通配符/模式的套件类?您可以将它们放在不同的包中。大多数IDE都有一种在给定包中运行所有测试的方法。在一个包含shell脚本的包中查找所有测试类也非常简单,可以作为构建的一部分运行测试。我

在我们的项目中,我们目前有大量(junit)测试,这些测试分为三类:单元测试、集成测试和wicket测试

我现在想对这些测试进行分组,以便只能运行其中一个(或两个)类别。我唯一发现的是junit测试套件和类别,如下所述:

我的问题是,我不想用@SuiteClasses声明测试套件中的每个测试


有没有办法添加带有通配符/模式的套件类?

您可以将它们放在不同的包中。大多数IDE都有一种在给定包中运行所有测试的方法。在一个包含shell脚本的包中查找所有测试类也非常简单,可以作为构建的一部分运行测试。我不知道是否有办法用ant来做,但我想是的

TestNG允许您将测试标记为在特定组中,然后运行这些组。这听起来正是你想要的,除了它不是JUnit


您可以滥用JUnit的假设机制来做您想做的事情:为每个测试组设置一个系统属性,然后通过假设设置了适当的属性来启动每个测试。运行所有测试将运行所有测试,但您不想要的所有测试都将被忽略。

您考虑过使用TestNG吗? 这是基于JUnit构建的,但功能更强大:请参阅

这很容易

将测试从JUnit转换为TestNG应该很简单


或者,您可以创建3个ant脚本,每个脚本都将运行它们的单元测试,但灵活性较低。

即使使用JUnit类别,您仍然无法使用通配符/模式,因为类别是注释,是Java类型

正如其他评论者所指出的,这正是TestNG使用字符串而不是注释来定义组的原因:

@Test(groups = { "database.ACCOUNTS", "fast-test" })
public void newAccountsShouldBeCreated() { ... }
以这种方式定义组后,可以使用正则表达式(例如“database.*”、“front-end.*”等)包括和排除组

TestNG确实不是基于JUnit的,但是将所有JUnit测试转换为TestNG非常容易。以下是两篇博客文章,概述了这一过程:

请参见或尝试使用


我也有同样的问题,我有超过5500个jUnit测试。然后我将其分为3个组,并使用上面的jUnit扩展创建了3个套件。很好。

假设我对这个问题的理解是正确的,实际上可以使用JUnit来完成。下面的代码用于JUnit4.11,允许我们将所有测试分为两类:“未分类”和集成

IntegrationTestSuite.java

/**
 * A custom JUnit runner that executes all tests from the classpath that
 * match the <code>ca.vtesc.portfolio.*Test</code> pattern 
 * and marked with <code>@Category(IntegrationTestCategory.class)</code>
 * annotation. 
 */
@RunWith(Categories.class)
@IncludeCategory(IntegrationTestCategory.class)
@Suite.SuiteClasses( { IntegrationTests.class })
public class IntegrationTestSuite {
}

@RunWith(ClasspathSuite.class)
@ClasspathSuite.ClassnameFilters({ "ca.vtesc.portfolio.*Test" })
class IntegrationTests {
}
/**
  * A custom JUnit runner that executes all tests from the classpath that match
  * <code>ca.vtesc.portfolio.*Test</code> pattern.
  * <p>
  * Classes and methods that are annotated with the
  * <code>@Category(IntegrationTestCategory.class)</code> category are 
  * <strong>excluded</strong>.
  */

@RunWith(Categories.class)
@ExcludeCategory(IntegrationTestCategory.class)
@Suite.SuiteClasses( { UnitTests.class })
public class UnitTestSuite {
}

@RunWith(ClasspathSuite.class)
@ClasspathSuite.ClassnameFilters({ "ca.vtesc.portfolio.*Test" })
class UnitTests {
}
/**
 * A marker interface for running integration tests.
 */
public interface IntegrationTestCategory {
}
UnitTestSuite.java

/**
 * A custom JUnit runner that executes all tests from the classpath that
 * match the <code>ca.vtesc.portfolio.*Test</code> pattern 
 * and marked with <code>@Category(IntegrationTestCategory.class)</code>
 * annotation. 
 */
@RunWith(Categories.class)
@IncludeCategory(IntegrationTestCategory.class)
@Suite.SuiteClasses( { IntegrationTests.class })
public class IntegrationTestSuite {
}

@RunWith(ClasspathSuite.class)
@ClasspathSuite.ClassnameFilters({ "ca.vtesc.portfolio.*Test" })
class IntegrationTests {
}
/**
  * A custom JUnit runner that executes all tests from the classpath that match
  * <code>ca.vtesc.portfolio.*Test</code> pattern.
  * <p>
  * Classes and methods that are annotated with the
  * <code>@Category(IntegrationTestCategory.class)</code> category are 
  * <strong>excluded</strong>.
  */

@RunWith(Categories.class)
@ExcludeCategory(IntegrationTestCategory.class)
@Suite.SuiteClasses( { UnitTests.class })
public class UnitTestSuite {
}

@RunWith(ClasspathSuite.class)
@ClasspathSuite.ClassnameFilters({ "ca.vtesc.portfolio.*Test" })
class UnitTests {
}
/**
 * A marker interface for running integration tests.
 */
public interface IntegrationTestCategory {
}
IntegrationTestCategory.java

/**
 * A custom JUnit runner that executes all tests from the classpath that
 * match the <code>ca.vtesc.portfolio.*Test</code> pattern 
 * and marked with <code>@Category(IntegrationTestCategory.class)</code>
 * annotation. 
 */
@RunWith(Categories.class)
@IncludeCategory(IntegrationTestCategory.class)
@Suite.SuiteClasses( { IntegrationTests.class })
public class IntegrationTestSuite {
}

@RunWith(ClasspathSuite.class)
@ClasspathSuite.ClassnameFilters({ "ca.vtesc.portfolio.*Test" })
class IntegrationTests {
}
/**
  * A custom JUnit runner that executes all tests from the classpath that match
  * <code>ca.vtesc.portfolio.*Test</code> pattern.
  * <p>
  * Classes and methods that are annotated with the
  * <code>@Category(IntegrationTestCategory.class)</code> category are 
  * <strong>excluded</strong>.
  */

@RunWith(Categories.class)
@ExcludeCategory(IntegrationTestCategory.class)
@Suite.SuiteClasses( { UnitTests.class })
public class UnitTestSuite {
}

@RunWith(ClasspathSuite.class)
@ClasspathSuite.ClassnameFilters({ "ca.vtesc.portfolio.*Test" })
class UnitTests {
}
/**
 * A marker interface for running integration tests.
 */
public interface IntegrationTestCategory {
}
下面的第一个示例测试未使用任何类别进行注释,因此在运行UnitTestSuite时将包括其所有测试方法,而在运行IntegrationTestSuite时将其排除

public class OptionsServiceImplTest {
    @Test
    public void testOptionAssignment() {
        // actual test code
    }
}
下一个示例在类级别上标记为Integration test,这意味着在运行UnitTestSuite并包含在IntegrationTestSuite中时,它的两个测试方法都将被排除在外:

@Category(IntegrationTestCategory.class)
public class PortfolioServiceImplTest {
    @Test
    public void testTransfer() {
        // actual test code
    }
    @Test
    public void testQuote() {
    }
}
第三个示例演示了一个测试类,其中一个方法没有注释,另一个方法用集成类别标记

public class MarginServiceImplTest {
    @Test
    public void testPayment() {
    }
    @Test
    @Category(IntegrationTestCategory.class)
    public void testCall() {
    }
}

选民们能解释为什么他们会这样做吗?使用TestNG是一个很好的建议。同意。TestNG已经足够成熟,可以作为一个相当少的替代品。感谢再次投票,我也想知道投票失败的原因。TestNG不是建立在JUnit之上的。构建工具使得只运行测试的子集变得足够简单,例如基于包/类命名约定。因此,仅此需求并不能证明将所有现有测试移植到TestNG是合理的?请看一下testng的pom中的依赖项!无论如何,我也建议使用ant解决方案,但我认为TestNG会更好。