2013-10-06 39 views
1

在下面的代碼中,ExampleTest類包含5個測試。但是,我想使用JUNIT從ExampleTestSuite類中僅運行其中的兩個。如何在Junit 4中自定義我的TestSuite測試?

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; 
    } 
} 
+1

之間相互轉換的兩個版本的JUnit是題外話。 Stackoverflow不是一個代碼轉換服務。如果您在重寫代碼時遇到困難,請解決您的問題中的特定問題。 – toniedzwiedz

+1

我不是要求你轉換所有的類,我已經使用註釋做了。我只是不知道如何限制JUnit 4中的測試。 – GSDa

回答

2

可以使用Categories跑者JUnit 4.8介紹是這樣的:

/** 
* 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 {} 
相關問題