无法从测试组中读取TestNG类组
我有以下代码来阅读测试用例组,以便进行报告:无法从测试组中读取TestNG类组,testng,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])
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()
{}
但当我在类级别定义组并抛出null指针时,它就不起作用了
@Test(Groups="G1")
public class SampleClassTest
{
@Test(Groups = "G3")
public void testCase1()
{
}
}
我试图搜索谷歌,但找不到任何解决办法。有人能帮我解决这个问题吗。在那个测试类中有没有其他没有@Test注释的方法?
如果是这样,那么当您用@Test annotation标记类时,只有该类有注释,而不是它的方法
如果您有这样的测试类
@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(){...}
}
然后,当传递testMethod1
时,您的MethodSetup
将引发异常
编辑:在获取没有
@Test
注释的方法时,可以检索类级@Test
注释并获取其组。这可以在基类或testcase类中使用。我将基类扩展到了所有的测试用例类,这很有效。我在基类中使用了这个方法
@BeforeClass(alwaysRun=true)
public void findClassgoup()
{
Test classTest = this.getClass().getAnnotation(Test.class);
if(null != classTest)
{
classLevelGroups= classTest.groups();
log.info("Classlevel group size : " + classLevelGroups.length);
}
if(null == classTest)
{
log.info("Test annotation on class retunred null");
}
}
感谢您对此进行研究,但我认为@test可以同时在类和测试级别()。若在两个地方都提到了组,那个么测试用例也将是这两个组的一部分。我已经找到了这个问题的答案。。。当然,
@Test
可以在类和方法级别,但是当你在类中添加@Test
时,这并不意味着方法也会有@Test
注释。另外,你的帖子就是我所说的:D抱歉,这并没有解决问题,因为我想得到的是——课堂上的小组名称。在你的例子中是“G1”。实际上,它需要另一个使用类级注释的@beforeClass反射方法。我也发布了这个。