我想在不同的线程上运行testng测试,以便该测试中的所有方法在同一个线程上顺序运行。 如何通过在xml文件或我执行testng测试的类中进行更改来实现这一点?
这是我的测试类:
import java.util.*;
import org.testng.annotations.Test;
@Test(singleThreaded=true)
public class Calculator2 {
Calendar calendar=Calendar.getInstance();
Date starttime;
Date endtime;
long stime;
long etime;
public void add() {
starttime=calendar.getTime();
stime=System.nanoTime();
System.out.println
(new Exception().getStackTrace()[0].getMethodName().toUpperCase()); // output :doit
System.out.println("start time: "+starttime);
Random r=new Random();
double inputA=r.nextDouble();
double inputB=r.nextDouble();
double answer = inputA+inputB;
System.out.println(answer+" "+Thread.currentThread().getId());
endtime=calendar.getTime();
etime=System.nanoTime();
System.out.println("end time: "+endtime);
long duration=etime-stime;
// System.out.println("start time add:"+starttime+" "+"end time:"+endtime+" "+Thread.currentThread().getId());
System.out.println("duration:"+duration/100000.000);
}
public void subtract() {
starttime=calendar.getTime();
stime=System.nanoTime();
System.out.println
(new Exception().getStackTrace()[0].getMethodName().toUpperCase()); // output :doit
System.out.println("start time: "+starttime);
Random r=new Random();
double inputA=r.nextDouble();
double inputB=r.nextDouble();
double answer = inputA-inputB;
System.out.println(answer+" "+Thread.currentThread().getId());
endtime=calendar.getTime();
System.out.println("end time: "+endtime);
etime=System.nanoTime();
long duration=etime-stime;
System.out.println("duration:"+duration/1000000.000);
}
public void multiply() {
starttime=calendar.getTime();
stime=System.nanoTime();
System.out.println
(new Exception().getStackTrace()[0].getMethodName().toUpperCase()); // output :doit
System.out.println("start time: "+starttime);
Random r=new Random();
double inputA=r.nextDouble();
double inputB=r.nextDouble();
double answer = inputA*inputB;
System.out.println(answer+" "+Thread.currentThread().getId());
endtime=calendar.getTime();
etime=System.nanoTime();
System.out.println("end time: "+endtime);
long duration=etime-stime;
System.out.println("duration:"+duration/1000000.000);
}
public void divide() {
starttime=calendar.getTime();
stime=System.nanoTime();
System.out.println
(new Exception().getStackTrace()[0].getMethodName().toUpperCase()); // output :doit
System.out.println("start time: "+starttime);
Random r=new Random();
double inputA=r.nextDouble();
double inputB=r.nextDouble();
double answer = inputA/inputB;
System.out.println(answer+" "+Thread.currentThread().getId());;
endtime=calendar.getTime();
etime=System.nanoTime();
System.out.println("end time: "+endtime);
long duration=etime-stime;
// System.out.println("start time div:"+starttime+" "+"end time:"+endtime);
System.out.println("duration:"+duration/1000000.000);
}
public void power() throws InterruptedException{
starttime=calendar.getTime();
stime=System.nanoTime();
System.out.println
(new Exception().getStackTrace()[0].getMethodName().toUpperCase()); // output :doit
System.out.println("start time: "+starttime);
Random r=new Random();
double inputA=r.nextDouble();
double inputB=r.nextDouble();
double answer =inputA;
for (int x=2; x<=inputB; x++){
answer *= inputA;
}
System.out.println(answer+" "+Thread.currentThread().getId());
endtime=calendar.getTime();
etime=System.nanoTime();
System.out.println("end time: "+endtime);
long duration=etime-stime;
// System.out.println("start time pow:"+starttime+" "+"end time:"+endtime);
System.out.println("duration"+duration/1000000.000);
}
}
这是我运行测试的类:
package TestNg.src;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.Properties;
import org.testng.IAnnotationTransformer;
import org.testng.TestListenerAdapter;
import org.testng.TestNG;
import org.testng.annotations.ITestAnnotation;
public class Test1 {
public static void main(String args[]) throws IOException,
ClassNotFoundException, InterruptedException {
File configFile = new File("name.properties");
Properties prope = new Properties();
FileReader reader = new FileReader(configFile);
prope.load(reader);
String s = prope.getProperty("class-name");
final Class<?> cl = Class.forName(s);
TestListenerAdapter tla = new TestListenerAdapter();
TestNG testng = new TestNG();
testng.setTestClasses(new Class[] { cl });
Transformer myTransformer = new Transformer();
testng.setThreadCount(20);
testng.setParallel("tests");
//testng.setDataProviderThreadCount(20);
testng.setAnnotationTransformer(myTransformer);
testng.addListener(tla);
testng.run();
}
}
class Transformer implements IAnnotationTransformer {
public void transform(ITestAnnotation annotation, Class testClass,
Constructor testConstructor, Method testMethod) {
annotation.setInvocationCount(5);
}
}