简体   繁体   English

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

[英]junit4 creating test suite with specific test methods

In junit4 I want to execute specific test methods from different classes ie want create a test suite with specific test methods from different classes. 在junit4中,我想从不同的类中执行特定的测试方法,即希望使用来自不同类的特定测试方法创建测试套件。

Lets say I have 2 classes: 可以说我有两节课:

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");
    }
}

From the above classes I want to execute test methods test_Login_001 , test_Login_003 , test_Logout_002 only. 从上面的类我想要执行测试方法test_Login_001,test_Login_003,test_Logout_002。

How this can be achieved in junit4 ? 如何在junit4中实现这一目标?

You need to create an org.junit.runner.Request and pass it to the JunitCore runner, or actually to any Runner. 您需要创建一个org.junit.runner.Request并将其传递给JunitCore运行器,或实际传递给任何Runner。

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

I actually created an Annotation and can search for methods with those annotations and dynamically create Request and run them 我实际创建了一个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());
            }
        }
    }
}

} }

Since JUnit 4.8 introduced Categories there exists a clean solution, create a TestSuite: 由于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 */ }

}

And add the @Category(MustHaveTests.class) above every test you would like to run with the TestSuite, eg: 并在要使用TestSuite运行的每个测试上方添加@Category(MustHaveTests.class),例如:

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

When running the TestSuite only the MustHaveTests-"tagged" tests will be executed. 运行TestSuite时,只会执行MustHaveTests-“标记”测试。 More Details on @Category: https://github.com/junit-team/junit4/wiki/categories 有关@Category的更多详情: https//github.com/junit-team/junit4/wiki/categories

This might not be the slickest implementation, but I solved a similar problem by created a new @SuiteMethods annotation as follows: 这可能不是最简单的实现,但我通过创建一个新的@SuiteMethods注释解决了类似的问题,如下所示:

SuiteMethods.java SuiteMethods.java

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

FilteredSuite.java 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());
    }
}

A Usage Example 用法示例

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM