TestNg自定義註解


1.源碼地址 java

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

2.在org.testng.annotations.Test中添加須要增長的註解 github


/**
   * 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的方法,可是不用去實現,報錯的地方一一添加便可 ide



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報告中使用咱們新增長的字段 ui

org.testng.reporters.XMLReporterConfig中增長字段 spa


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!成功看到新增長的字段了,折騰了兩個週末也算沒有白費。接下來就是自定義報告監聽器,將結果存入對應的數據庫表中 code

相關文章
相關標籤/搜索