Junit4/Uiautomator循环执行所有测试

1. 测试类如下:

import android.os.RemoteException;
import android.support.test.filters.SdkSuppress;
import android.support.test.runner.AndroidJUnit4;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

@RunWith(AndroidJUnit4.class)
@SdkSuppress(minSdkVersion = 18)
public class MyTest extends common{

    @Before
    public void Init() throws RemoteException {
        System.out.println("init");
        /*-----------Script----------*/
    }

    @Test
    public void A_Test() throws Exception {
        System.out.println("StartTest A");
        /*-----------Script----------*/
    }

    @Test
    public void B_Test() throws Exception {
        System.out.println("StartTest B");
        /*-----------Script----------*/
    }
}

2. 通过TestSuite实现循环

(1)如果循环次数较少,可以直接在Suite中多次添加同一测试类 ,如下会执行3次

import org.junit.runner.RunWith;
import org.junit.runners.Suite;

@RunWith(Suite.class)
@Suite.SuiteClasses({
        MyTest.class,
        MyTest.class,
        MyTest.class
})
public class TestSuit {

}

(2).通过自定义testRunner

import org.junit.runner.RunWith;
import org.junit.runners.Suite;

@RunWith(RepeatSuite.class)
@Suite.SuiteClasses({
        MyTest.class
})
public class TestSuit {

}

其中@RunWith指定自定义的testRunner类为RepeatSuite.class,新建RepeatSuite类如下,继承Suite类

import org.junit.runner.Runner;
import org.junit.runner.notification.RunNotifier;
import org.junit.runners.Suite;
import org.junit.runners.model.InitializationError;
import org.junit.runners.model.RunnerBuilder;

import java.io.IOException;
import java.util.List;
import java.util.Properties;

public class RepeatSuite extends Suite {

    private int times = Integer.parseInt(this.getProperties("looptimes"));

    public RepeatSuite(Class<?> klass, RunnerBuilder builder) throws InitializationError {
        super(klass, builder);
    }

    public RepeatSuite(RunnerBuilder builder, Class<?>[] classes) throws InitializationError {
        super(builder, classes);
    }

    protected RepeatSuite(Class<?> klass, Class<?>[] suiteClasses) throws InitializationError {
        super(klass, suiteClasses);
    }

    protected RepeatSuite(RunnerBuilder builder, Class<?> klass, Class<?>[] suiteClasses) throws InitializationError {
        super(builder, klass, suiteClasses);
    }

    protected RepeatSuite(Class<?> klass, List<Runner> runners) throws InitializationError {
        super(klass, runners);
    }

    @Override
    protected void runChild(Runner runner, RunNotifier notifier) {
        System.out.println("All Loop is " + Integer.toString(this.times) );
        for(int i = 0; i<this.times; i++) {
            System.out.println("Loop " + Integer.toString(i) + "------------------");
            runner.run(notifier);
        }
    }

    public String getProperties(String s) {
        Properties prop = new Properties();

        try {
            prop.load(RepeatSuite.class.getResourceAsStream("/assets/test.prop"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        String getValue =  prop.getProperty(s);
        return  getValue;
    }
}

其中的循环次数使用方法getProperties()从配置文件中读取, 重写了方法 runChild(),添加循环

参考:https://www.mscharhag.com/java/understanding-junits-runner-architecture 

猜你喜欢

转载自blog.csdn.net/jianiao/article/details/86689662
今日推荐