简体   繁体   English

如何使用 jUnit 将测试用例添加到套件中?

[英]How to add test cases to a suite using jUnit?

I have 2 test classes, both extend TestCase .我有 2 个测试类,都扩展了TestCase Each class contains a bunch of individual tests which run against my program.每个 class 都包含一堆针对我的程序运行的单独测试。

How can I execute both classes (and all tests they have) as part of the same suite?我怎样才能将这两个类(以及它们拥有的所有测试)作为同一个套件的一部分执行?

I am using jUnit 4.8.我正在使用 jUnit 4.8。

In jUnit4 you have something like this: 在jUnit4中,您将具有以下内容:

@RunWith(Suite.class)
@SuiteClasses({
    SomeTest.class,
    SomeOtherTest.class,
    ...
    })
public class AllTests {}

If you want the Eclipse GUI suite builder (New > JUnit Test suite), you have to add 如果要使用Eclipse GUI套件构建器(“新建”>“ JUnit测试套件”),则必须添加

public static junit.framework.Test suite() {
   return new JUnit4TestAdapter(SomeTest.class);
}

to each of your test classes st the GUI test suite builder recognizes your test. GUI测试套件生成器可以识别您的每个测试类。

Create TestClass and override suite() method and run newly created TestClass. 创建TestClass并重写suite()方法并运行新创建的TestClass。

 public static Test suite()
    {
        TestSuite suite = new TestSuite("Test ExpenseTest");
        suite.add(TestCase1.class);
        suite.add(TestCase2.class);
        return suite;
    }

JUnit 3 supported TestSuite and public static Test suite() . JUnit 3支持的TestSuitepublic static Test suite() JUnit 4 doesn't support it (it creates added test classes, but doesn't launch test methods). JUnit 4不支持它(它创建添加的测试类,但不启动测试方法)。

But you can launch a subsuite of tests inside a suite.但是您可以在套件内启动测试子套件。

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

@RunWith(Suite::class)
@Suite.SuiteClasses(
    Test1::class,
    TestSuite1::class
)
class StartTest

Then inside StartTest you can add test classes and test suites.然后在StartTest ,您可以添加测试类和测试套件。

@RunWith(Suite::class)
@Suite.SuiteClasses(
    Test2::class,
    Test3::class,
    TestSuite2::class
)
class TestSuite1

@RunWith(Suite::class)
@Suite.SuiteClasses(
    Test4::class,
    Test5::class
)
class TestSuite2

This way you can join test classes into groups (to have a list of 10 items instead of 100).通过这种方式,您可以将测试类加入组中(拥有 10 个项目而不是 100 个项目的列表)。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM