0

在下面的代码中,ExampleTest该类包含 5 个测试。但是,我只想从ExampleTestSuite使用 JUNIT 的类中运行其中两个。

public class ExampleTest extends TestCase {
    private Example example;
    public ExampleTest(String name) {
        super(name);
    }

    protected void setUp() throws Exception {
        super.setUp();
        example= new Example();
    }

    protected void tearDown() throws Exception {
        super.tearDown();
        example= null;
    }

    public void test1() {

    }
    public void test2() {

    }
    public void test3() {

    }
    public void test4() {

    }
    public void test5() {

    }

}

这是我在 JUnit 3 中完成的下面的代码,但是如何在 JUnit 版本 4 中完成呢?

public class ExampleTestSuite {

    public static Test suite() {
        TestSuite suite = new TestSuite(ExampleTestSuite.class.getName());
        suite.addTest(new ExampleTest("test1"));
        suite.addTest(new ExampleTest("test3"));
        return (Test) suite;
    }
}
4

1 回答 1

2

您可以像这样使用JUnit 4.8Categories中引入的运行器:

/**
 * Marker class to be used with the @Category annotation of JUnit.
 */
public class SmokeTests {}


/**
 * Your original test class converted to JUnit 4.
 */
public class ExampleTest {
    private Example example;

    @Before
    public void setUp() throws Exception {
        example = new Example();
    }

    @After
    public void tearDown() throws Exception {
        example = null;
    }

    @Test
    @Category(SmokeTests.class)
    public void test1() {}

    @Test
    public void test2() {}

    @Test
    @Category(SmokeTests.class)
    public void test3() {}

    @Test
    public void test4() {}

    @Test
    public void test5() {}
}


/**
 * Your original test suite class converted to JUnit 4.
 */
@RunWith(Categories.class)
@SuiteClasses(ExampleTest.class)
@IncludeCategory(SmokeTests.class)
public class ExampleTestSuite {}
于 2013-10-09T19:25:01.653 回答