重新设置testNG里面的annotation

package testng;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;

import org.testng.annotations.ITestAnnotation;
import org.testng.internal.annotations.IAnnotationTransformer;

public class InvocationTransformer implements IAnnotationTransformer {

	@Override
	public void transform(ITestAnnotation test, Class clazz, Constructor constructor, Method method) {
		if("two".equals(method.getName())) {
			test.setInvocationCount(2);
		} else if("three".equals(method.getName())) {
			test.setInvocationCount(3);
		}
	}
}

package testng;

import org.testng.TestListenerAdapter;
import org.testng.TestNG;
import org.testng.annotations.Test;

public class A {
	@Test
	public void one() {
		System.out.println("one");
	}
	
	@Test
	public void two() {
		System.out.println("two");
	}
	
	@Test
	public void three() {
		System.out.println("three");
	}
	
	public static void main(String[] args) {
		TestNG tng = new TestNG();
		tng.setTestClasses(new Class[] {A.class});
		TestListenerAdapter listener = new TestListenerAdapter();
		tng.addListener(listener);
		
		tng.setAnnotationTransformer(new InvocationTransformer());
		
		tng.run();
		System.out.println("Passed: " + listener.getPassedTests().size());
	}
}

你可能感兴趣的:(testNG)