通过TestSuite设置测试方法顺序

时间:2016-10-14 11:18:50

标签: testing junit test-suite

有几个关于如何创建测试方法顺序的主题,但我遇到了一些麻烦。

我只有Junit 4.11 TestClass:

public class ServiceTest {

    @BeforeClass
    public static void setUp() throws IllegalArgumentException, IOException {

        try {

            service = new Service("myService");

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    @Test
    public void testA {

        boolean isConnected = false;
        try {
            isConnected = service.initialService();
            assertEquals(true, isConnected);

        } catch (Exception e) {
            e.printStackTrace();
            assertEquals(true, isConnected);
        }
    }

    @Test
    public void testB {

        try {
            //exec some methods....
            assertEquals(true, isConnected);

        } catch (Exception e) {
            e.printStackTrace();
            assertEquals(true, isConnected);

        }
    }
}

testmethod的执行顺序必须是testA,testB。

正因为如此,我创造了Testsuite:

@RunWith(MySuite.class)
@SuiteClasses({ EwsServiceTest.class })

public class TestSuite {


}

接下来我定义了Testrunner,它为我提供了方法testA和testB

public class MySuite extends Suite {

    private Method[] children= null;

    protected MySuite(Class<?> klass, Class<?>[] suiteClasses) throws InitializationError {

        super(klass, suiteClasses);
        // TODO Auto-generated constructor stub
    }

    @Override
    protected List<Runner> getChildren() {
        //collect testA and testB methods
        children = super.getChildren().get(0).getClass().getMethods();

        return super.getChildren();
    }

    @Override
    protected void runChild(Runner runner, RunNotifier notifier) {
        super.runChild(runner, notifier);
    }
}

如何以及在哪个引入的类中我可以设置方法顺序。

0 个答案:

没有答案