Java JUnit 4 测试套件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/457276/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 14:51:06  来源:igfitidea点击:

JUnit 4 Test Suites

javajunittest-suite

提问by Adam Taylor

How do I create test suites with JUnit 4?

如何使用 JUnit 4 创建测试套件?

All the documentation I've seen doesn't seem to be working for me. And if I use the Eclipse wizard it doesn't give me an option to select any of the test classes I have created.

我看到的所有文档似乎都不适合我。如果我使用 Eclipse 向导,它不会让我选择我创建的任何测试类。

采纳答案by Joachim Sauer

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

@RunWith(Suite.class)
@Suite.SuiteClasses({TestClass1.class, TestClass2.class})
public class TestSuite {
  //nothing
}

回答by duffymo

I think TestSuite has fallen out of favor. That might have been the style before 4.x, but it's not now as far as I know.

我认为 TestSuite 已经失宠了。这可能是 4.x 之前的风格,但据我所知,现在不是。

I just annotate the tests I want and then run the class. All the annotated tests are run. I might use Ant, but most of the time I have IntelliJ run them for me.

我只是注释我想要的测试,然后运行课程。运行所有带注释的测试。我可能会使用 Ant,但大多数时候我让 IntelliJ 为我运行它们。

回答by mP.

Of the top of my head create a TestSuite and the invoke addTests. If you want somesource to look at try any opensource lib like hibernate or something from apache and take a look under the test directory of the source for a Tests suite ...

在我的头顶创建一个 TestSuite 并调用 addTests。如果您想要一些源代码查看尝试任何开源库,例如 hibernate 或来自 apache 的东西,并查看测试套件源代码的测试目录下...

回答by cmcginty

You can create a suite like so. For example an AllTestsuite would look something like this.

您可以像这样创建套件。例如,AllTest套房看起来像这样。

package my.package.tests;

@RunWith(Suite.class)
@SuiteClasses({
    testMyService.class,
    testMyBackend.class,
    ...
})

public class AllTests {}

Now you can run this in a couple different ways:

现在您可以通过几种不同的方式运行它:

  1. right-click and run in Eclipse as Junit test
  2. create a runable Java Application; Main class='org.junit.runner.JUnitCore' and Args='my.package.tests.AllTests'
  3. run from the command line:

    $ java -cp build/classes/:/usr/share/java/junit4.jar:/usr/share/java/hamcrest-core.jar org.junit.runner.JUnitCore my.package.tests.AllTests
    
  1. 右键单击并在 Eclipse 中作为 Junit 测试运行
  2. 创建一个可运行的 Java 应用程序;主类='org.junit.runner.JUnitCore' 和Args='my.package.tests.AllTests'
  3. 从命令行运行:

    $ java -cp build/classes/:/usr/share/java/junit4.jar:/usr/share/java/hamcrest-core.jar org.junit.runner.JUnitCore my.package.tests.AllTests
    

回答by Red Rooster

Here are the steps to create a JUnit suite in eclipse:

以下是在 Eclipse 中创建 JUnit 套件的步骤:

  1. In the 'Package Explorer' view of the eclipse 'Java' perspective, select your unit test(s) in their package, inside the eclipse java project.
  2. Right-click on any one of the selected tests.
  3. In the pop-up menu, select New, Other…
  4. Open the ‘Java' folder, then open the ‘JUnit' folder
  5. Select ‘JUnit Test Suite' and then select the ‘Next' button
  6. Select button ‘Finish'
  7. Result: ‘AllTests.java' suite file is created, with tests automatically included
  8. Select the Run button in eclipse
  9. Result: all tests in suite run
  10. You can now point to this suite file with ANT, Jenkins or other build configuration continuous integration tool.
  1. 在 eclipse 'Java' 透视图的 'Package Explorer' 视图中,在 eclipse java 项目内的包中选择您的单元测试。
  2. 右键单击任一选定的测试。
  3. 在弹出菜单中,选择新建、其他...
  4. 打开“Java”文件夹,然后打开“JUnit”文件夹
  5. 选择“JUnit 测试套件”,然后选择“下一步”按钮
  6. 选择按钮“完成”
  7. 结果:创建了“AllTests.java”套件文件,并自动包含测试
  8. 在eclipse中选择Run按钮
  9. 结果:套件中的所有测试都运行
  10. 您现在可以使用 ANT、Jenkins 或其他构建配置持续集成工具指向此套件文件。

Version info: this is for eclipse Neon and JUnit 4. You can also select JUnit 3 before selecting 'Finish' in step 6.

版本信息:这适用于 eclipse Neon 和 JUnit 4。您也可以在步骤 6 中选择“完成”之前选择 JUnit 3。