【问题标题】:How to set priority for test methods execution using TestNG + JAVA Reflection如何使用 TestNG + JAVA Reflection 设置测试方法执行的优先级
【发布时间】:2015-09-21 12:40:31
【问题描述】:

我试图在注释为@Test 的反射的帮助下执行我的测试脚本,如下所示:

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.
}
}

但是,问题是无法按照优先级值运行@Test 方法。它随机执行。谁能告诉我如何根据优先级值运行@test 方法。

另外,我对dependsOnMethods 进行了同样的尝试。但它仍然是随机执行的。

示例代码:

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();
    }
}

A类

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");
    }

}

输出:

输入包名 com.test.build.ClassA 测试3 测试4 测试5 测试6 测试1 测试2 通过:执行TestMetods

================================================ 默认测试

测试运行:1,失败:0,跳过:0

输出未按优先级正确执行,并显示为随机调用。如何让它顺序执行?

【问题讨论】:

  • 你不应该。您可能在测试中具有优先权的唯一原因是在执行中强制执行顺序,唯一原因是因为您的测试依赖于其他测试。这是 TDD 中的一个很大的 NO-NO:所有测试都应该是独立的,并且设置/清除所有需要的状态。如果是这种情况,我强烈建议您重新设计它们,或者如果不是这种情况,请考虑一下为什么需要优先级。
  • (另外,你应该正确缩进你发布的代码)

标签: java reflection selenium-webdriver testng


【解决方案1】:

TestNG 为收件箱提供了一个专门的功能来做你想做的事:Annotation transformers

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-28
    • 2020-06-29
    • 1970-01-01
    • 2012-09-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多