junit4使用特定的测试方法创建测试套件

时间:2013-03-06 06:28:08

标签: java junit4

在junit4中,我想从不同的类中执行特定的测试方法,即希望使用来自不同类的特定测试方法创建测试套件。

假设我有2个班级:

public class Test_Login {
    @Test
    public void test_Login_001(){
        System.out.println("test_Login_001");
    }

    @Test
    public void test_Login_002(){
        System.out.println("test_Login_002");
    }

    @Test
    public void test_Login_003(){
        System.out.println("test_Login_003");
    }
}


public class Logout {   

    @Test
    public void test_Logout_001(){  
        System.out.println("test_Logout_001");  
    }

    @Test
    public void test_Logout_002(){
        System.out.println("test_Logout_002");
    }

    @Test
    public void test_Logout_003(){
        System.out.println("test_Logout_003");
    }
}

从上面的类我想执行测试方法test_Login_001,test_Login_003,test_Logout_002 only。

如何在junit4中实现这一目标?

3 个答案:

答案 0 :(得分:1)

您需要创建一个org.junit.runner.Request并将其传递给JunitCore运行器,或实际传递给任何Runner。

JUnitCore junitRunner = new JUnitCore();
Request request = Request.method(Logout.class, "test_Logout_002");
Result result = junitRunner.run(request);

我实际创建了一个Annotation,可以搜索带有这些注释的方法并动态创建Request并运行它们

public class TestsSuite {

public static void main(String[] args) throws Exception {
    Class annotation = MyTestAnnotation.class;
    JUnitCore junitRunner = new JUnitCore();
    Class testClass = Test_Login.class;
    Method[] methods = testClass.getMethods();
    for (Method method : methods) {
        if (method.isAnnotationPresent(annotation)) {
            if (method.isAnnotationPresent(org.junit.Test.class)) {
                Request request = Request.method(testClass, method.getName());
                Result result = junitRunner.run(request);
                System.out.println(result.wasSuccessful());
            }
        }
    }
}

}

答案 1 :(得分:1)

由于JUnit 4.8引入了类别,因此存在一个干净的解决方案,创建一个TestSuite:

@RunWith(Categories.class)
@IncludeCategory(MustHaveTests.class)
@SuiteClasses( { Test_Login.class, Test_Logout.class }) 
public class MustHaveTestsTestSuite {
    public interface MustHaveTests { /* category marker */ }

}

将@Category(MustHaveTests.class)添加到您希望使用TestSuite运行的每个测试之上,例如:

@Category(MustHaveTests.class)
@Test
public void test_Login_001(){
    System.out.println("test_Login_001");
}

运行TestSuite时,只会执行MustHaveTests-“tagged”测试。有关@Category的更多详情:https://github.com/junit-team/junit4/wiki/categories

答案 2 :(得分:0)

这可能不是最简洁的实现,但我通过创建一个新的@SuiteMethods注释解决了类似的问题,如下所示:

SuiteMethods.java

@Retention(RUNTIME)
@Target(TYPE)
public @interface SuiteMethods {
    String[] value() default {""};
}

FilteredSuite.java

public class FilteredSuite extends Categories {

    private static String[] TEST_METHODS_TO_RUN = {""};  // default behavior is to run all methods 

    private static Class<?> extractMethodNamesFromAnnotation(Class<?> clazz) {
        SuiteMethods methodsAnnotation = clazz.getAnnotation(SuiteMethods.class);
        if (methodsAnnotation != null) {
            // if our MethodsAnnotation was specified, use it's value as our methods filter
            TEST_METHODS_TO_RUN = methodsAnnotation.value();
        }        
        return clazz;
    }

    public static Filter getCustomFilter() {
        Filter f = new Filter() {

            @Override
            public boolean shouldRun(Description desc) {
                String methodName = desc.getMethodName();
                for (String subString : TEST_METHODS_TO_RUN) {
                    if (methodName == null || methodName.contains(subString)) {
                        return true;
                    }
                }
                return false;
            }

            @Override
            public String describe() {
                return null;
            }
        };
        return f;
    }

    public FilteredSuite(Class<?> arg0, RunnerBuilder arg1) throws InitializationError {
        super(extractMethodNamesFromAnnotation(arg0), arg1);            
    }

    @Override
    public void filter(Filter arg0) throws NoTestsRemainException {
        // At test suite startup, JUnit framework calls this method to install CategoryFilter.
        // Throw away the given filter and install our own method name filter 
        super.filter(getCustomFilter());
    }
}

用法示例

@RunWith(FilteredSuite.class)
@SuiteClasses({
    GroupRestTest.class,
    ScenarioRestTest.class
})
@SuiteMethods({
    "testReadOnlyFlag",
    "testSheetWriteData",
    "testAddScenarioMeta"
})
public class SubsetTestSuite {
}