eclipse设置jvm_ant junit_ant available(2)

电脑杂谈  发布时间:2017-02-14 01:29:13  来源:网络整理

2.生成执行文件:

ant available_eclipse设置jvm_ant junit

AutoFailMethodsTest.java,实现执行结果显示和将本次失败的结果回写到元日志TEST-com.weibo.cases.suite.WangleiTestSuite.txt,实现只跑上一次失败的用例

//auto java file generated on: 2015.02.05 15:37:11,898
package com.weibo.failmethods;
import java.text.SimpleDateFormat;
import java.io.*;
import java.util.*;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import org.junit.runner.*;
import com.weibo.cases.hugang.*;
import com.weibo.cases.xiaoyu.*;
import com.weibo.cases.wanglei16.*;
import com.weibo.cases.xuelian.*;
import com.weibo.cases.lingna.*;
import com.weibo.cases.maincase.*;

@SuiteClassesMethods(className = {
com.weibo.cases.xiaoyu.StatusesLatestTimelineStatusTest.class},
methodsNumPerClass = {
1},
methodsName = {
"testLatestTimelineConsolePrivate"})
public class AutoFailMethodsTest {
	final static int THREADCOUNT = 50;
	public static void main(String[] args) throws Exception {
		String path = "/Users/hugang/workspace/testMblog/src/com/weibo/failmethods/TEST-com.weibo.cases.suite.WangleiTestSuite.txt";
		Class<AutoFailMethodsTest> clazz = AutoFailMethodsTest.class;
		SuiteClassesMethods scm = clazz.getAnnotation(SuiteClassesMethods.class);
		Class<?>[] className = scm.className();
		int[] methodsNumPerClass = scm.methodsNumPerClass();
		String[] methodsName = scm.methodsName();
		Map<Class<?>, List<String>> myClassMethodMap = new LinkedHashMap();
		List<List<String>> listMethodsName = new ArrayList<List<String>>();
		int k = 0;
		for (int i = 0; i < className.length; i++) {
			List<String> temp = new ArrayList<String>();
			for (int m = 0; m < methodsNumPerClass[i]; m++) {
		 		temp.add(methodsName[k]);
		 		k++;
 			}
		listMethodsName.add(i, temp);
	 	myClassMethodMap.put(className[i], listMethodsName.get(i));
 		}
		List<Result> methodsResult = new ArrayList<Result>();
		int failNum = 0;
		int successNum = 0;
		long runTime = 0L;
		ExecutorService executorService = Executors.newFixedThreadPool(THREADCOUNT);
		List<Future<Result>> listFr = new ArrayList<Future<Result>>();
		long startTime = System.currentTimeMillis();
		for(Map.Entry<Class<?>, List<String>> entry : myClassMethodMap.entrySet()){
			Class testClass = entry.getKey();
			List<String> failMethod = entry.getValue();
			for(int i = 0; i < failMethod.size(); i++){
				Future<Result> fr = executorService.submit(new ThreadRunTest(testClass, failMethod.get(i)));
				listFr.add(fr);
	}
}
for(Future<Result> fr : listFr){
	try{
		while(!fr.isDone());
		Result result = fr.get();
		if(result.wasSuccessful()){
			successNum++;
		}else{
			failNum++;
			methodsResult.add(result);
		}
	}catch (InterruptedException e) {
		e.printStackTrace();
	}finally{
		executorService.shutdown();
	}
}
long endTime = System.currentTimeMillis();
runTime = endTime - startTime;
		try{
			String filePath = System.getProperty("user.dir") + "/src/com/weibo/failmethods/Result.txt";
			File file = new File(filePath);
			if(!file.exists()){
				file.createNewFile();
			}
			FileOutputStream fop = new FileOutputStream(file);
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss,SSS");
			fop.write("## Second Times Result generated on: ".getBytes());
			fop.write(sdf.format(new Date()).getBytes());
			fop.write("\n".getBytes());
			StringBuffer sb = new StringBuffer();
			sb.append("===================== 结果集 =====================");
			sb.append("\n");
			sb.append("用例总数:" + methodsName.length);
			sb.append(", 成功数:" + successNum);
			sb.append(", 失败数:" + failNum);
			sb.append(", 运行时间:" + (runTime/1000)/60 + " 分钟 " + (runTime/1000)%60 + " 秒");
			sb.append("\n");
			sb.append("=================================================");
			sb.append("\n");
			sb.append("\n");
			fop.write(sb.toString().getBytes());
			for(int j = 0; j < methodsResult.size(); j++){
				byte[] fail = methodsResult.get(j).getFailures().toString().getBytes();
 				fop.write(fail);
 				fop.write("\n".getBytes());
 				fop.write("\n".getBytes());
 			}
			fop.flush();
			fop.close();
			new WriteLogTest().writeFailLog(filePath, path);
 		}catch(IOException e){
		e.printStackTrace();
		}
	}
}
由于是通过WriteFailTest类生成的文件,并没添加注释,最重要的部分就是将每个用例提交给一个线程去执行,获得每个线程执行的结果fr.get()。统计结果集:

通过WriteLogTest.java将本次失败用例的信息回写到元日志TEST-com.weibo.cases.suite.WangleiTestSuite.txt,为下次执行用。

重复AutoFailMethodsTest.java;生成只执行第一步没过的用例的结果:

FindFailTest.java(用来生成执行失败用例的class:AutoFailMethodsTest.java(生成新文件的原因:在扫日志,拿到类名是字符串类型,而在执行用例的文件中需要指定.class,字符串无法转换成.class,就直接写新文件)后,直接执行


本文来自电脑杂谈,转载请注明本文网址:
https://www.pc-fly.com/a/ruanjian/article-32285-2.html

相关阅读
    发表评论  请自觉遵守互联网相关的政策法规,严禁发布、暴力、反动的言论

    热点图片
    拼命载入中...