How to set priority for test methods execution using TestNG + JAVA Reflection -
i have trying execute test script of reflections has annotated @test following way:
class<?> classname = class.forname(format); //load class name @ runtime constructor<?> customconstructor = classname.getconstructor(webdriver.class); //create customized constructor , initalize driver testbase method[] method = classname.getmethods(); //call list of methods in current class file (method me : method) { if (me.getname().startswith("test")) { //check wheather class prefix test method getmethods = class.forname(format).getdeclaredmethod(me.getname()); //loading methods @ runtime. if(getmethods.isannotationpresent(test.class)) { //the method annotated @test execute here, using invoke() method of reflection. } }
but, problem not able run @test methods per priority value. executing randomly. can tell me how run @test methods based on priority value.
also, tried same dependsonmethods. still randomly executing.
sample code:
package com.test.build; import com.test.build.classa; import com.test.build.classb; import java.lang.reflect.*; import java.util.scanner; import org.testng.annotations.test; public class parentclass { @test public void executetestmetods() throws exception { scanner scan = new scanner(system.in); system.out.println("type package name"); string name = scan.next(); class<?> class1 = class.forname(name); method[] method = class1.getmethods(); (method me : method) { if (me.isannotationpresent(test.class)) { if (me.getname().startswith("test")) { system.out.println(me.getname()); } } } scan.close(); } }
classa
package com.test.build; import org.testng.annotations.test; @test(singlethreaded = true) public class classa { @test(priority=0) public void test1() { system.out.println("class a"); } @test(priority=1) public void test2() { system.out.println("class second method"); } @test(priority=2) public void test3() { system.out.println("class a"); } @test(priority=3) public void test4() { system.out.println("class second method"); } @test(priority=4) public void test5() { system.out.println("class a"); } @test(priority=5) public void test6() { system.out.println("class second method"); } }
output:
type package name com.test.build.classa test3 test4 test5 test6 test1 test2 passed: executetestmetods
=============================================== default test
tests run: 1, failures: 0, skips: 0
the output not executed per priority, , shows randomly invoked. how make sequence execution?
testng provides inbox dedicated feature want: annotation transformers.
Comments
Post a Comment