像Junit一样利用注解运行case
首先,我们先编写2个注解 TestSuite 和TestCase
package com.auto.annotation;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.RUNTIME)
public @interface TestSuite {
	//所属系统
	String System() default "TEST";
	//起始版本
	String FVersion() default "201502V1";
	//Suite状态
	String Status() default "using";
	//作者-Suite负责人
	String Author() default "zymaxs";
	//描述
	String Description() default "TestSuite Description";
}
package com.auto.annotation;
import java.lang.annotation.Retention; 
import java.lang.annotation.RetentionPolicy;  
import com.auto.util.StaticVar;
@Retention(RetentionPolicy.RUNTIME)
public @interface TestCase {
	//优先度
	String Level() default "L1";
	//描述
	String Description() default "TestCaseDescription";
	//case版本
	String CVserion() default "TEST_201502";
	//case状态
	String CStatus() default "using";
	//positive and negative 正与反
	String PAN() default "P";
}
很简单的定义
然后写一个Test class
package com.autocase;
import com.auto.annotation.*;
@TestSuite
public class Test {
	@TestCase(Level="L1",Description="实验", PAN="P")
	public void Test_01(){
		System.out.println("no 1 ====");
	}
	@TestCase(Level="L2",Description="实验2", PAN="N")
	public void Test_02(){
		System.out.println("no 2 ====");
	}
}
接下来编写一个工具类用来解析注解
package com.auto.annotation;
import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;
public class AnalyseAnntation {
	/**
	 * 
	 * 用于返回一个class所有的方法和其对应的注解
	 * @param ClassName
	 *            类名
	 * @return HashMap<Mothod,Annotation[]> 该map返回 Mothod 方法 Annotation[]
	 *         该方法对应的注解集合
	 */
	public static HashMap<Method, Annotation[]> getMethodsAnnotation(
			String ClassName) {
		HashMap<Method, Annotation[]> m = new HashMap<Method, Annotation[]>();
		try {
			Method[] ms = Class.forName(ClassName).getDeclaredMethods();
			for (Method m1 : ms) {
				m.put(m1, m1.getAnnotations());
			}
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		return m;
	}
	/**
	 * 
	 * 用于返回一个class的注解
	 * @param ClassName
	 *            类名
	 * @returnAnnotation[] class的注解 Annotation[]
	 *         该类对应的注解集合
	 */
	public static Annotation[] getClassAnnotation(String ClassName) {
		 Annotation[] an = null;
		try {
			an = Class.forName(ClassName).getDeclaredAnnotations();
			
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		return an;
	}
	/**
	 * 本质为invoke(class,fuction) 没有参数 返回String结果
	 * @param an
	 *            执行对象
	 * 
	 * @param MethodName
	 *            方法名
	 * 
	 * @return String值
	 */
	public static String invoke(Object an, String MethodName) {
		Method m2;
		String value = null;
			try {
				m2 = an.getClass().getDeclaredMethod(MethodName, null);
				value = (String) m2.invoke(an, null);
			} catch (SecurityException e) {
				e.printStackTrace();
			} catch (NoSuchMethodException e) {
				e.printStackTrace();
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				e.printStackTrace();
			}
		return value;
	}
	public static void main(String[] args) {
		String cname = "com.autocase.Test";
		System.out.println("Class:" + cname);
		Annotation[] an=AnalyseAnntation.getClassAnnotation(cname);
		for(Annotation a1:an){
			if(a1.annotationType().equals(TestSuite.class)){
				System.out.println("TestSuite:" + AnalyseAnntation.invoke(a1,"Description"));
			}
		}
		
		HashMap<Method, Annotation[]> m = AnalyseAnntation
				.getMethodsAnnotation(cname);
		Iterator<Entry<Method, Annotation[]>> it = m.entrySet().iterator();
		while (it.hasNext()) {
			Entry<Method, Annotation[]> itm = it.next();
			
			System.out.println("method:" + itm.getKey());
			for (Annotation n : itm.getValue()) {
				System.out.println("Annotation" + n.annotationType().getName());
				if (n.annotationType().equals(TestCase.class)) {
					System.out.println(AnalyseAnntation.invoke(n,"Description"));
					try {
						AnalyseAnntation.invoke( Class.forName(cname).newInstance(),itm.getKey().getName());
					} catch (ClassNotFoundException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (InstantiationException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (IllegalAccessException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					
				}
			}
		}
	}
}
运行一下,Test类中所有打上@TestCase标签的case都会被执行一遍
                    
                
                
            
        
浙公网安备 33010602011771号