1.源码地址 https://github.com/cbeust/testng/
2.在org.testng.annotations.Test中添加需要增加的注解 - /**
- * case author
- * @return
- */
- public String author() default "";
-
- /**
- * case level
- * @return
- */
- public String level() default "";
复制代码 添加完之后,引用@Test注解,即可使用author、level了。但是在运行过程中,我们还需要在报告中体现author和level的值
3.在org.testng.annotations.Configuration中增加新添加的注解 - public String author() default "";
-
- public String level() default "";
复制代码
4.在org.testng.internal.annotations.TestOrConfiguration中增加author和level的getter和setter方法
- @Override
- public String getAuthor() {
- return m_author;
- }
- @Override
- public void setAuthor(String author) {
- m_author = author;
- }
- @Override
- public String getLevel() {
- return m_level;
- }
- @Override
- public void setLevel(String level) {
- m_level = level;
- }
复制代码
5.在org.testng.internal.ITestNGMethod增加author和level的get、set方法,并且在org.testng.internal.BaseTestMethod类中实现方法,由于继承关系很多,有部分类中需要定义author和level的方法,但是不用去实现,报错的地方一一添加即可
6.修改org.testng.internal.annotations.JDK15TagFactory的重定义一个createConfigurationTag方法,增加author和level字段 - private IAnnotation createConfigurationTag(Class<?> cls, Annotation a,
- boolean beforeSuite, boolean afterSuite,
- boolean beforeTest, boolean afterTest,
- String[] beforeGroups, String[] afterGroups,
- boolean beforeClass, boolean afterClass,
- boolean beforeMethod, boolean afterMethod,
- boolean alwaysRun,
- String[] dependsOnGroups, String[] dependsOnMethods,
- String description, String author, String level, boolean enabled, String[] groups,
- boolean inheritGroups, String[] parameters,
- boolean firstTimeOnly, boolean lastTimeOnly,
- long timeOut)
- {
- ConfigurationAnnotation result = new ConfigurationAnnotation();
- result.setFakeConfiguration(true);
- result.setBeforeSuite(beforeSuite);
- result.setAfterSuite(afterSuite);
- result.setBeforeTest(beforeTest);
- result.setAfterTest(afterTest);
- result.setBeforeTestClass(beforeClass);
- result.setAfterTestClass(afterClass);
- result.setBeforeGroups(beforeGroups);
- result.setAfterGroups(afterGroups);
- result.setBeforeTestMethod(beforeMethod);
- result.setAfterTestMethod(afterMethod);
- result.setAlwaysRun(alwaysRun);
- result.setDependsOnGroups(dependsOnGroups);
- result.setDependsOnMethods(dependsOnMethods);
- result.setDescription(description);
- result.setAuthor(author);
- result.setLevel(level);
- result.setEnabled(enabled);
- result.setGroups(groups);
- result.setInheritGroups(inheritGroups);
- result.setParameters(parameters);
- result.setFirstTimeOnly(firstTimeOnly);
- result.setLastTimeOnly(lastTimeOnly);
- result.setTimeOut(timeOut);
- return result;
- }
复制代码修改本类中的createTestTag方法,将author和level的值放入result中 - result.setAuthor(findInherited(test.author(), cls, Test.class, "author", DEFAULT_STRING));
- result.setLevel(findInherited(test.level(), cls, Test.class, "level", DEFAULT_STRING));
复制代码
7.在xml报告中使用我们新增加的字段 org.testng.reporters.XMLReporterConfig中增加字段 - public static final String ATTR_AUTHOR = "author";
- public static final String ATTR_LEVEL = "level";
复制代码org.testng.reporters.XMLSuiteResultWriter的getTestResultAttributes方法中增加author和level - String author = testResult.getMethod().getAuthor();
- if(!Utils.isStringEmpty(author)) {
- attributes.setProperty(XMLReporterConfig.ATTR_AUTHOR, author);
- }
- String level = testResult.getMethod().getLevel();
- if(!Utils.isStringEmpty(level)) {
- attributes.setProperty(XMLReporterConfig.ATTR_LEVEL, level);
- }
复制代码
8.测试一下 - public class Demo1 {
- @Test(testName="ddd", groups={"a"},description="这是用例描述",author="我是作者啊",level="11")
- public void f() {
- System.out.println("实例:testng注解");
- }
- }
复制代码右键运行下,查看testng-results.xml
ok!成功看到新增加的字段了,折腾了两个周末也算没有白费。接下来就是自定义报告监听器,将结果存入对应的数据库表中
|