首页 > 数据库 > 数据库开发技术 > 如何遍历执行一个包里面每个类的用例方法
本人在使用 httpclient 做接口测试的过程中,用例是以代码形式写在一个用例包里面的,包里的每个类表示的一类用例,大致是按照接口所在模块划分。这样就导致了一个问题,执行用例必须得把用例包里面所以类的用例方法都执行一边。之前使用过java 的反射来根据类名创建类对象,然后根据方法名执行相应的方法。根据这个思路,加之上网查找了一些相关资料参考了一些其他人的代码,自己封装了一个执行用例包里面所有类的用例方法的用例执行类,分享出来,供大家参考。
1package source;
2
3import java.io.File;
4import java.lang.reflect.Method;
5import java.net.URL;
6import java.util.ArrayList;
7import java.util.List;
8
9public class excuteSource extends SourceCode {
10
11 public static void main(String[] args) {
12 excuteAllMethodInPackage("pie.normal");
13 }
14
15 /**
16 * 执行包内所有类的非 main 方法
17 *
18 * @param packageName
19 */
20 public static void excuteAllMethodInPackage(String packageName) {
21 // String packageName = "najm.base";
22 List<String> classNames = getClassName(packageName);
23 if (classNames != null) {
24 for (String className : classNames) {
25 String path = packageName + "." + className;
26 excuteAllMethod(path);// 执行所有方法
27 }
28 }
29 }
30
31 /**
32 * 获取实例对象的所有 public 方法
33 *
34 * @param object
35 * @return
36 */
37 public static Method[] getAllMethod(Object object) {
38 Class<?> class1 = object.getClass();
39 class1.getName();
40 Method[] methods = class1.getDeclaredMethods();
41 // Method[] methods = class1.getMethods();//此处获取的所有方法,包括继承来的
42 return methods;
43 }
44
45 /**
46 * 获取实例对象所有 public 方法,并且执行
47 *
48 * @param object
49 */
50 public static void excuteAllMethod(Object object) {
51 Class<?> class1 = object.getClass();
52 Method[] methods = class1.getDeclaredMethods();
53 for (Method method : methods) {
54 if (method.getName().equals("main")) {
55 continue;
56 }
57 executeMethodByName(method.getName(), class1.getName());
58 }
59 }
60
61 /**
62 * 执行一个类的方法内所有的方法,非 main
63 *
64 * @param path
65 * 类名
66 */
67 public static void excuteAllMethod(String path) {
68 Class<?> c = null;
69 Object object = null;
70 try {
71 c = Class.forName(path);
72 object = c.newInstance();
73 } catch (Exception e) {
74 e.printStackTrace();
75 }
76 Class<?> class1 = object.getClass();
77 Method[] methods = class1.getDeclaredMethods();
78 for (Method method : methods) {
79 if (method.getName().equals("main")) {// 排除 mian 方法
80 continue;
81 }
82 executeMethodByName(method.getName(), class1.getName());
83 }
84 }
85
86 /**
87 * 根据方法名执行相应方法,利用反射,适用于带参方法
88 *
89 * @param api_name
90 * 接口名称
91 * @param use
92 * 传入参数,此处为适配新的请求方法
93 * @return 返回请求json数据
94 */
95 public static void executeMethodByName(String mehtod, String path) {
96 Object obj = null;
97 Method method = null;
98 String className = null;
99 try {
100 // 里面写自己的类名及路径
101 Class<?> c = Class.forName(path);
102 obj = c.newInstance();
103 className = c.getCanonicalName();
104 // 第一个参数写的是方法名,第二个\第三个\...写的是方法参数列表中参数的类型
105 method = c.getMethod(mehtod);
106 // invoke是执行该方法,并携带参数值
107 } catch (Exception e) {
108 output("反射执行出错!", e);
109 }
110 try {
111 output("执行" + className + "类的" + method.getName() + "方法");
112 method.invoke(obj);
113 } catch (Exception e) {
114 output("反射运行方法异常!", e);
115 }
116 }
117
118 /**
119 * 获取某包下所有类
120 *
121 * @param packageName
122 * 包名
123 * @param childPackage
124 * 是否遍历子包
125 * @return 类的完整名称
126 */
127 public static List<String> getClassName(String packageName) {
128 List<String> fileNames = new ArrayList<>();
129 ClassLoader loader = Thread.currentThread().getContextClassLoader();// 获取当前位置
130 String packagePath = packageName.replace(".", "/");// 转化路径,Linux 系统
131 URL url = loader.getResource(packagePath);// 具体路径
132 if (url == null || !"file".equals(url.getProtocol())) {
133 output("获取类名失败!");
134 return fileNames;
135 }
136 File file = new File(url.getPath());
137 File[] childFiles = file.listFiles();
138 for (File childFile : childFiles) {
139 String path = childFile.getPath();
140 if (path.endsWith(".class")) {
141 path = path.substring(path.lastIndexOf("/") + 1, path.lastIndexOf("."));
142 fileNames.add(path);
143 }
144 }
145 return fileNames;
146 }
147}
main 方法里面写的就是使用方法,这里需要提醒一点,一定要对方法名进行过滤,不然可能会把其他类的 main 方法也执行了。
原文作者:FunTester;如有侵权,请联系管理员删除。