如何在 JUnit 4 中运行属于某个类别的所有测试 [英] How to run all tests belonging to a certain Category in JUnit 4

查看:33
本文介绍了如何在 JUnit 4 中运行属于某个类别的所有测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JUnit 4.8 包含一个不错的新功能,称为类别",允许您将某些类型的测试组合在一起.这非常有用,例如对慢速和快速测试进行单独的测试运行.我知道 JUnit 4.8 发行说明,但想知道我如何实际运行所有带有特定类别注释的测试.

JUnit 4.8 contains a nice new feature called "Categories" that allows you to group certain kinds of tests together. This is very useful, e.g. to have separate test runs for slow and fast tests. I know the stuff mentioned in JUnit 4.8 release notes, but would like to know how I can actually run all the tests annotated with certain category.

JUnit 4.8 发行说明显示了一个示例套件定义,其中 SuiteClasses 注释从特定类别中选择要运行的测试,如下所示:

The JUnit 4.8 release notes show an example suite definition, where SuiteClasses annotation selects the tests from certain category to run, like this:

@RunWith(Categories.class)
@IncludeCategory(SlowTests.class)
@SuiteClasses( { A.class, B.class }) // Note that Categories is a kind of Suite
public class SlowTestSuite {
  // Will run A.b and B.c, but not A.a
}

有谁知道我如何运行 SlowTests 类别中的所有测试?看来你必须有SuiteClasses注解...

Does anyone know how I could run all the tests in SlowTests category? It seems that you must have the SuiteClasses annotation...

推荐答案

我找到了一种可能的方法来实现我想要的,但我不认为这是最好的解决方案,因为它依赖于 ClassPathSuite 库不是 JUnit 的一部分.

I found out one possible way to achieve what I want, but I don't consider this to be the best possible solution as it relies on ClassPathSuite library that is not part of JUnit.

我为慢速测试定义了测试套件,如下所示:

I define the test suite for slow tests like this:

@RunWith(Categories.class)
@Categories.IncludeCategory(SlowTests.class)
@Suite.SuiteClasses( { AllTests.class })
public class SlowTestSuite {
}

AllTests 类定义如下:

AllTests class is defined like this:

@RunWith(ClasspathSuite.class)
public class AllTests {
}

我不得不在这里使用 ClassPathSuite 项目中的 ClassPathSuite 类.它将找到所有带有测试的类.

I had to use ClassPathSuite class from ClassPathSuite project here. It will find all the classes with tests.

这篇关于如何在 JUnit 4 中运行属于某个类别的所有测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆