簡體   English   中英

TestNG Groups:我們可以包含兩個組名並創建一個組來運行測試嗎?

[英]TestNG Groups: Can we include two group names and create one group to run tests?

我有以下testng測試方法。

    @Test(groups = {"tsg1.0","smoke"})
public void testLoginWithInvalidCredentials(String usernameValue, String passwordValue){        
    /*Print something*/
}


@Test(groups = {"tsg1.0"})          
public void testLoginWithUserIdOnly(String username) {       
    /*Print something*/
}



@Test(groups = {"tsg1.0"})          
public void testLoginWithPasswordOnly(String password) {         
    /*Print something*/
}

這是用於測試上述方法的testng xml。

<suite name="Suite" thread-count="1" verbose="10"> 
  <test name="Test">
  <groups>
<run>  
 <include name="tsg1.0"/>    
</run>
</groups>
<packages>  
<package name="<test package name>"/>       
 </packages> 
</test>  
</suite>

有沒有辦法在我可以創建一個xml,其中包括組“TSG1.0”和“SMOKE”的測試。 我想只在這種情況下運行第一個測試(testLoginWithInvalidCredentials)。

請幫忙。

謝謝,邁克。

PS:以下內容不起作用,因為它將包含tsg1.0或smoke。 我想要一個和條件在這里......

<run>
<include name="tsg1.0"/>   
<include name="smoke"/>  
</run>

你實際上可以“半現成”這樣做: http//testng.org/doc/documentation-main.html#beanshell

在您的特定情況下,它將是這樣的:

<method-selectors>
  <method-selector>
    <script language="beanshell"><![CDATA[
       return groups.containsKey("tsg1.0") && groups.containsKey("smoke");
     ]]></script>
  </method-selector>
</method-selectors>

剛剛在這里更詳細地回答了一個類似的問題: 如果它是兩個組的成員,是否有可能為TestNG運行測試?

在testng中沒有現成的AFAIK。 您可以做的就是編寫您的methodinterceptor並僅返回屬於這兩個組的thsoe方法列表。

您還可以從攔截方法的testcontext參數中獲取包含的組。

您可以在此處參考更多信息: http//testng.org/doc/documentation-main.html#methodinterceptors

您可以擁有一組論壇

Groups can also include other groups. These groups are called "MetaGroups".
For example, you might want to define a group "all" that includes "checkintest" 
and "functest"."functest" itself will contain the groups "windows" and "linux" 
while "checkintest will only contain "windows". 

屬性文件示例:

<groups>
    <define name="functest">
      <include name="windows"/>
      <include name="linux"/>
    </define>

    <define name="all">
      <include name="functest"/>
      <include name="checkintest"/>
    </define>

    <run>
      <include name="all"/>
    </run>
  </groups>

對於從cmd執行的多個TestNG組,您可以在TestNG.xml文件中使用以下腳本。

<method-selectors>
        <method-selector>
            <script language="beanshell"><![CDATA[
            Boolean runTest = false;
            String groupsName = System.getProperty("GROUPS");

            if (groupsName != null && groupsName != ""){
                StringTokenizer groupsTagList = new StringTokenizer(groupsName, ",");
                while (groupsTagList.hasMoreTokens()) {
                    if(groups.containsKey(groupsTagList.nextToken().trim()){
                        runTest = true;
                        break;
                    }
                }
            }else{
                runTest = true;
            } 
            return runTest;
            ]]>
            </script>
        </method-selector>
    </method-selectors>

從Maven執行:

mvn test -DGROUPS = groups1,groups2,groups3

它會很好......

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM