2011-11-18 225 views
2

有没有办法记录testNG测试案例,以便文档可以自动生成?我想像这样:记录TestNG测试案例

@Description("This testcase checks, whether the system is alive") 
@Step("Check whether services are running") 
@Step("Try to access the system webpage") 
... 
@Test(groups={"sanity"}) 
public void checkStatus() { 
... 
} 

我已经考虑了两种选择:自定义注释处理和编写我自己的javadoc doclet。在尝试任何这些选项之前,我想知道,是否有一种标准的方法。我肯定会重用当前的testNG注释,特别是测试的分组。

最后但并非最不重要的一点,我想提一下,我只想用这种方法进行系统级测试(不是单元测试),我相当复杂,并且不太容易说出测试的结果来自测试名称或代码。

+0

是否有使用注释或自定义格式的原因?其实你可以使用普通的HTML,比如

  • 检查服务是否正在运行
。随着一些CSS应用于此,javadoc实用程序将完美地完成这项工作。另外,您还可以在某些IDE中为您的javadoc获取HTML自动完成功能。 –

回答

5

最后,我想出了自己。我使用javadoc和一些注释处理(仅用于区分测试用例组)。我使用自定义的doclet,它首先建立测试用例的下列方式列表:

private MethodDoc[] getTestMethods(RootDoc root) { 
    List<MethodDoc> result = new ArrayList<MethodDoc>(); 
    for (ClassDoc c : root.classes()) { 
     for(MethodDoc m : c.methods()) { 
      if (isTest(m)) 
       result.add(m); 
     } 
    } 
    return result.toArray(new MethodDoc[0]); 
} 

// simplified test detection 
private static boolean isTest(MethodDoc m) { 
    for(AnnotationDesc a: m.annotations()) { 
     if (a.annotationType().name().equals("Test")) 
      return true; 
    } 
    return false; 
} 

然后,对每个测试中,我检索中集集团:

static Set<String> getMethodGroups(MethodDoc m) { 
    Set<String> result = getGroups(m); 
    result.addAll(getGroups(m.containingClass())); 
    return result; 
} 

static Set<String> getGroups(ProgramElementDoc m) { 
    Set<String> result = new HashSet<String>(); 
    for(AnnotationDesc a: m.annotations()) { 
     if (a.annotationType().name().equals("Test")) { 
      for(ElementValuePair ev : a.elementValues()) { 
       if (ev.element().name().equals("groups")) { 
        String value = ev.value().toString(); 
        StringTokenizer tokenizer = new StringTokenizer(value, "{}\", "); 
        while (tokenizer.hasMoreElements()) { 
         result.add(tokenizer.nextToken()); 
        } 
       } 
      } 
     } 
    } 
    return result; 
} 

剩下的只是标准doclet处理。另外,我发现我可以直接在javadoc中使用自定义标签,例如

/** 
* Some description 
* @Step Step 1 
* @Step Step 2 
*/ 
void someMethod() {} 
+1

你能否给我提供确切的步骤..就像我们必须保持上述方法..我是javadoc的新手,从来没有使用过它。谢谢! – OverrockSTAR