首页 > 解决方案 > 无法从测试组中读取 TestNG 类组

问题描述

我有以下代码来读取测试用例组以进行报告:

public void MethodSetup(ITestContext context, Method testMethod) {
        log.info("CLEAR_OUTPUT");
        Test t = testMethod.getAnnotation(Test.class);
        testCaseGroups = t.groups();
        // log.info(t.groups()[0]);//or however you want to use it.
}

当我对测试用例进行分组时,这非常有效,例如

@Test(Groups = "G1")
public void testCase1()
{}

但是当我在类级别定义组并抛出空指针时它不起作用。

@Test(Groups="G1")
public class SampleClassTest
{

@Test(Groups = "G3")
public void testCase1()
{
}
}

我试图搜索谷歌,但找不到任何解决这个问题的方法。谁能帮我解决这个问题。

标签: testng

解决方案


您在该测试类中是否还有其他没有 @Test 注释的方法?
如果是这样,那么当您使用 @Test 注释标记类时,只有该类具有注释,而不是方法
所以如果你有这样的测试课

@Test(groups="G1")
class TestClass {

     // this test method has no annotation, 
     // but it will run by TestNG because it is public
     // and the class has @Test annotation
     public void testMethod1(){...}

     // this test method would have its own @Test annotation
     @Test(groups="G2")
     public void testMethod2(){...}
}

然后你会在通过MethodSetup时抛出异常。testMethod1

编辑:
如果您获得没有@Test注释的方法,您可以检索类级@Test注释并获取它的组。


推荐阅读