51Testing软件测试论坛

标题: TestNg自定义注解 [打印本页]

作者: 测试积点老人    时间: 2018-12-18 15:49
标题: TestNg自定义注解

1.源码地址

https://github.com/cbeust/testng/


2.在org.testng.annotations.Test中添加需要增加的注解

  1. /**
  2.    * case author
  3.    * @return
  4.    */
  5.   public String author() default "";
  6.   
  7.   /**
  8.    * case level
  9.    * @return
  10.    */
  11.   public String level() default "";
复制代码

添加完之后,引用@Test注解,即可使用author、level了。但是在运行过程中,我们还需要在报告中体现author和level的值


3.在org.testng.annotations.Configuration中增加新添加的注解

  1. public String author() default "";
  2.   
  3.   public String level() default "";
复制代码

4.在org.testng.internal.annotations.TestOrConfiguration中增加author和level的getter和setter方法
  1. @Override
  2.         public String getAuthor() {
  3.                 return m_author;
  4.         }

  5.         @Override
  6.         public void setAuthor(String author) {
  7.                 m_author = author;
  8.         }

  9.         @Override
  10.         public String getLevel() {
  11.                 return m_level;
  12.         }

  13.         @Override
  14.         public void setLevel(String level) {
  15.                 m_level = level;
  16.         }
复制代码

5.在org.testng.internal.ITestNGMethod增加author和level的get、set方法,并且在org.testng.internal.BaseTestMethod类中实现方法,由于继承关系很多,有部分类中需要定义author和level的方法,但是不用去实现,报错的地方一一添加即可


6.修改org.testng.internal.annotations.JDK15TagFactory的重定义一个createConfigurationTag方法,增加author和level字段

  1. private IAnnotation createConfigurationTag(Class<?> cls, Annotation a,
  2.       boolean beforeSuite, boolean afterSuite,
  3.       boolean beforeTest, boolean afterTest,
  4.       String[] beforeGroups, String[] afterGroups,
  5.       boolean beforeClass, boolean afterClass,
  6.       boolean beforeMethod, boolean afterMethod,
  7.       boolean alwaysRun,
  8.       String[] dependsOnGroups, String[] dependsOnMethods,
  9.       String description, String author, String level, boolean enabled, String[] groups,
  10.       boolean inheritGroups, String[] parameters,
  11.       boolean firstTimeOnly, boolean lastTimeOnly,
  12.       long timeOut)
  13.   {
  14.     ConfigurationAnnotation result = new ConfigurationAnnotation();
  15.     result.setFakeConfiguration(true);
  16.     result.setBeforeSuite(beforeSuite);
  17.     result.setAfterSuite(afterSuite);
  18.     result.setBeforeTest(beforeTest);
  19.     result.setAfterTest(afterTest);
  20.     result.setBeforeTestClass(beforeClass);
  21.     result.setAfterTestClass(afterClass);
  22.     result.setBeforeGroups(beforeGroups);
  23.     result.setAfterGroups(afterGroups);
  24.     result.setBeforeTestMethod(beforeMethod);
  25.     result.setAfterTestMethod(afterMethod);
  26.     result.setAlwaysRun(alwaysRun);
  27.     result.setDependsOnGroups(dependsOnGroups);
  28.     result.setDependsOnMethods(dependsOnMethods);
  29.     result.setDescription(description);
  30.     result.setAuthor(author);
  31.     result.setLevel(level);
  32.     result.setEnabled(enabled);
  33.     result.setGroups(groups);
  34.     result.setInheritGroups(inheritGroups);
  35.     result.setParameters(parameters);
  36.     result.setFirstTimeOnly(firstTimeOnly);
  37.     result.setLastTimeOnly(lastTimeOnly);
  38.     result.setTimeOut(timeOut);

  39.     return result;
  40.   }
复制代码

修改本类中的createTestTag方法,将author和level的值放入result中

  1. result.setAuthor(findInherited(test.author(), cls, Test.class, "author", DEFAULT_STRING));
  2.     result.setLevel(findInherited(test.level(), cls, Test.class, "level", DEFAULT_STRING));
复制代码

7.在xml报告中使用我们新增加的字段

org.testng.reporters.XMLReporterConfig中增加字段

  1. public static final String ATTR_AUTHOR = "author";
  2.   public static final String ATTR_LEVEL = "level";
复制代码

org.testng.reporters.XMLSuiteResultWriter的getTestResultAttributes方法中增加author和level

  1. String author = testResult.getMethod().getAuthor();
  2.     if(!Utils.isStringEmpty(author)) {
  3.             attributes.setProperty(XMLReporterConfig.ATTR_AUTHOR, author);
  4.     }
  5.     String level = testResult.getMethod().getLevel();
  6.     if(!Utils.isStringEmpty(level)) {
  7.             attributes.setProperty(XMLReporterConfig.ATTR_LEVEL, level);
  8.     }
复制代码

8.测试一下

  1. public class Demo1 {
  2.   @Test(testName="ddd", groups={"a"},description="这是用例描述",author="我是作者啊",level="11")
  3.   public void f() {
  4.           System.out.println("实例:testng注解");
  5.   }
  6. }
复制代码

右键运行下,查看testng-results.xml


[attach]120216[/attach]


ok!成功看到新增加的字段了,折腾了两个周末也算没有白费。接下来就是自定义报告监听器,将结果存入对应的数据库表中








欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/) Powered by Discuz! X3.2