Android中动态加载运行测试代码

package com.automation.testlibrary;
import android.os.AsyncTask;
import java.lang.reflect.Method;

public class MethodRunTask extends AsyncTask {
    String packageName;
    String className;
    String methodName;
    String[] classNames=new String[]{};
public MethodRunTask(String packagename, String[] classnames) {
        packageName = packagename;
        className = "";
        methodName = "";
        classNames = classnames;
    }
    public MethodRunTask(){

    }
    @Override
    protected Object doInBackground(Object[] objects) {
        if (classNames.length > 0) {
            runClassMethods(packageName, classNames);
        } else {
            runClassMethods(packageName, className, methodName);
        }
        return null;
    }
public void runClassMethods(String packageName, String[] classNames) {//        TestCaseUtils.TESTCASE_CATEGORY="/Priority/P1";
        try {
            TaskStatus.getInstance().SetTestCaseRunning();
            for (String classNameInPackage : classNames) {
                //执行className类下所有test_前缀的方法
                Class<?> clazz = Class.forName(packageName+"." + classNameInPackage);
                Method[] methods = clazz.getMethods();
                Object obj = clazz.newInstance();
                for (Method method : methods) {
                    String name = method.getName();
                    if (name.startsWith("test_")) {
                        try {
                            ReportUtil.new_log();
                            method.invoke(obj);
                        } catch (Exception e) {
                            e.printStackTrace();
                        } finally {
                            try {
                                ReportUtil.close_log();
                            } catch (Exception ex) {
                                ex.printStackTrace();
                            }
                        }
                        continue;
                    }
                }
            }
        } catch (Throwable e) {
            e.printStackTrace();
        }finally {
            TaskStatus.getInstance().SetTestCaseFinished();
        }
    }
}

 

posted @ 2018-02-01 17:36  GavinLJC  阅读(270)  评论(0编辑  收藏  举报