So legen Sie die Priorität für die Ausführung von Testmethoden mit TestNG + JAVA Reflection fest
Ich habe versucht, mein Testskript mithilfe von Überlegungen auszuführen, die mit @Test wie folgt kommentiert wurden:
Class<?> className = Class.forName(format); //Load the class name at runtime
Constructor<?> customConstructor = className.getConstructor(WebDriver.class); //Create customized constructor and initalize driver from testbase
Method[] method = className.getMethods(); //Call the list of methods in current class file
for (Method me : method) {
if (me.getName().startsWith("test")) { //Check wheather the class prefix as test
Method getMethods = Class.forName(format).getDeclaredMethod(me.getName()); //Loading all the methods at runtime.
if(getMethods.isAnnotationPresent(Test.class))
{
//The method which is annotated @Test will execute here, using invoke() method of reflection.
}
}
Aber das Problem kann die @ Test-Methoden nicht gemäß dem Prioritätswert ausführen. Es wird zufällig ausgeführt. Kann mir jemand sagen, wie ich die @ test-Methoden basierend auf dem Prioritätswert ausführen kann.
Auch habe ich das gleiche mit dependOnMethods versucht. Aber es wird immer noch zufällig ausgeführt.
Beispielcode
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();
for (Method me : method) {
if (me.isAnnotationPresent(Test.class)) {
if (me.getName().startsWith("test")) {
System.out.println(me.getName());
}
}
}
scan.close();
}
}
Klasse
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 A second method");
}
@Test(priority=2)
public void test3()
{
System.out.println("class A");
}
@Test(priority=3)
public void test4()
{
System.out.println("Class A second method");
}
@Test(priority=4)
public void test5()
{
System.out.println("class A");
}
@Test(priority=5)
public void test6()
{
System.out.println("Class A second method");
}
}
Ausgabe
Paketname eingeben com.test.build.ClassA test3 test4 test5 test6 test1 test2 PASSED: executeTestMetods
=========================================== Standardtest
Testlauf: 1, Fehler: 0, Übersprungen: 0Die Ausgabe wird nicht ordnungsgemäß nach Priorität ausgeführt und wird nach dem Zufallsprinzip aufgerufen. Wie mache ich es Sequenzausführung?