gpt4 book ai didi

java - 如何在 Junit 4 中自定义 TestSuite 测试?

转载 作者:行者123 更新时间:2023-11-30 04:12:50 26 4
gpt4 key购买 nike

在下面的代码中,ExampleTest类(class)包含 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;
}
}

最佳答案

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

/**
* 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 {}

关于java - 如何在 Junit 4 中自定义 TestSuite 测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19211442/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com