4

在 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 。

如何在 junit4 中实现这一点?

4

3 回答 3

2

由于 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 */ }

}

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

@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

于 2016-09-12T13:48:02.030 回答
1

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

JUnitCore junitRunner = new JUnitCore();
Request request = Request.method(Logout.class, "test_Logout_002");
Result result = junitRunner.run(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());
            }
        }
    }
}

}

于 2013-04-16T02:27:49.337 回答
1

这可能不是最巧妙的实现,但我通过创建一个新的@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 {
}
于 2017-05-24T14:16:29.450 回答