2017-07-24 105 views
0

TestNG的組名稱經常被硬編碼並可能降低軟件質量。我想知道如何防止硬編碼組名稱。防止TestNG硬編碼組名稱

在TestCase.java中,是的,我可以使用靜態最終字符串,而不是像@JavaSoftwareTester在this post中詢問的那樣。

public class TestType { 
    private static final String GROUP_A = "GroupA"; 
    private static final String GROUP_B = "GroupB" 

    /** 
    * Avoid instantiation 
    */ 
    private TestType() { 
    } 
} 

但是,看來這最後的字符串不能在testng.xml文件中使用,如:

<groups> 
    <run> 
     <!-- I don't want hard coded --> 
     <include name = "GroupA" /> 
     <!-- but this is incorrect --> 
     <include name = TestType.GROUP_A /> 
    </run> 
</groups> 

總結:我想,以防止硬編碼的組名,但我無法找到合適的機制。請善意表明如何解決這個問題。

+0

XML是文本,而不是代碼,因此它無法引用一個XML描述Java代碼。 – juherr

回答

1

您可以使用TestNG提供的Beanshell支持來執行此操作。

以下是你如何去做的。

  • 首先添加一個依賴於BeanShell的庫通過從here添加內容
  • 您然後定義一個BeanShell的如下所示是(因爲TestNG的在BeanShell的庫帶來僅作爲可選的依賴,這是必需)你的方法選擇

<method-selector> <script language="beanshell"> <![CDATA[ import org.rationale.emotions.stackoverflow.qn45272885.TestClass; groups.containsKey(TestClass.GROUP_B); ]]> </method-selector>

不要忘了讓常量,公衆在TestType類。

要了解更多關於BeanShell的整合使用TestNG你可以參考我的博客文章here也正式TestNG的文檔here