2012-11-06 39 views
15

我需要通過對以下值...如何通過java代碼參數從行家測試

exeEvironment(測試環境), testGroup(集團TestNG中)

從命令 - Line - > POM - > TestNG - >測試用例。

基於這兩個職位....

pass a java parameter from maven

How to pass parameters to guicified TestNG test from Surefire Maven plugin?

我做了以下配置..

萬無一失插件,我嘗試以下兩個選項,似乎沒有任何工作。

=====

(1)

<execution> 
<id>default-test</id> 
    <goals> 
     <goal>test</goal> 
    </goals> 
    <configuration> 
     <properties> 
      <exeEnvironment>${exeEnvironment}</exeEnvironment> 
      <testGroup>${testGroup}</testGroup> 
     </properties> 
     <suiteXmlFiles> 
      <suiteXmlFile>testng.xml</suiteXmlFile> 
     </suiteXmlFiles> 
    </configuration> 
</execution> 

(2)

<execution> 
<id>default-test</id> 
<goals> 
    <goal>test</goal> 
</goals> 
<configuration> 
    <systemPropertyVariables> <exeEnvironment>${exeEnvironment}</exeEnvironment> 
     <testGroup>${testGroup}</testGroup> </systemPropertyVariables> 
    <suiteXmlFiles> 
     <suiteXmlFile>testng.xml</suiteXmlFile> 
    </suiteXmlFiles> 
</configuration> 
</execution> 

的testng.xml,我可以使用變量testGroup李ke ...

<test name="Web Build Acceptance"> 
    <groups> 
     <run> 
      <include name="${testGroup} /> 
     </run> 
    </groups> 
    <classes> 
     <class name="com.abc.pqr" /> 
    </classes> 
</test> 

這似乎不工作,我需要定義一個參數。


測試用例,我試圖讓他變量以下兩種方式... (1)

testEnv = testContext.getSuite().getParameter("exeEnvironment"); 
testGroup = testContext.getSuite().getParameter("testGroup"); 

(2)

testEnv = System.getProperty("exeEnvironment"); 
testGroup = System.getProperty("testGroup"); 

回答

33

這是我正在尋找我的自動化測試的確切的事情,我得到它的工作。

命令行參數

mvn clean test -Denv.USER=UAT -Dgroups=Sniff 

我出海的Xml

<?xml version="1.0" encoding="UTF-8"?> 
<project xmlns="http://maven.apache.org/POM/4.0.0" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 
    <groupId>TestNg</groupId> 
    <artifactId>TestNg</artifactId> 
    <version>1.0</version> 

    <dependencies> 
     <dependency> 
      <groupId>org.testng</groupId> 
      <artifactId>testng</artifactId> 
      <version>6.8</version> 
      <scope>test</scope> 
     </dependency> 
    </dependencies> 
    <build> 
     <plugins> 
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-surefire-plugin</artifactId> 
       <version>2.12.4</version> 
       <configuration> 
        <systemPropertyVariables> 
         <environment>${env.USER}</environment> 
        </systemPropertyVariables> 
       </configuration> 
      </plugin> 
     </plugins> 
    </build> 
</project> 

TestNG的測試

import org.testng.annotations.Parameters; 
import org.testng.annotations.Test; 


public class TestAuthentication { 

    @Test (groups = { "Sniff", "Regression" }) 
    public void validAuthenticationTest(){ 
     System.out.println(" Sniff + Regression" + System.getProperty("environment")); 
    } 

    @Test (groups = { "Regression" },parameters = {"environment"}) 
    public void failedAuthenticationTest(String environment){ 
     System.out.println("Regression-"+environment); 
    } 

    @Parameters("environment") 
    @Test (groups = { "Sniff"}) 
    public void newUserAuthenticationTest(String environment){ 
     System.out.println("Sniff-"+environment); 
    } 
} 

上述效果很好。此外,如果你需要使用testng.xml,您可以在@Test()指定suiteXmlFile像...

 <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-surefire-plugin</artifactId> 
      <version>2.12.4</version> 
      <configuration> 
       <systemPropertyVariables> 
        <environment>${env.USER}</environment> 
       </systemPropertyVariables> 
       <suiteXmlFiles> 
        <suiteXmlFile>testng.xml</suiteXmlFile> 
       </suiteXmlFiles> 
      </configuration> 
     </plugin> 

而且,我更喜歡使用@Parameters代替parameters作爲後來被棄用。

+0

看起來很有希望,讓我試着回去。感謝分享。 – Girish

+0

順便說一下,在這裏吃了testng.xml嗎? – Girish

+4

我沒有使用testng.xml,因爲您不需要在POM文件中添加參數。 MVN乾淨的測試-Denvironment = QA -Dgroups =迴歸 如果使用此 1.僅迴歸組將被執行(@Test(組= { 「嗅」, 「迴歸」}) 2。@Parameters(「environment」) - 值「QA」將直接傳遞給您的測試 – KingArasan

2

你不需要定義在TestNG的XML或POM組東西中,支持來自內置。你可以簡單地指定組的CMD線 http://maven.apache.org/plugins/maven-surefire-plugin/test-mojo.html#groups

希望它可以幫助..

編輯2:

Ok..so這裏的另一個選項...實現IMethodInterceptor

定義你的自定義屬性。 在您的命令行調用中使用-Dcustomproperty = groupthatneedstoberun。

在攔截調用中,掃描所有方法..某些事情的效果..

System.getProperty("customproperty"); 
for(IMethodInstance ins : methods) { 
    if(ins.getMethod().getGroups()) contains group) 
     Add to returnedVal; 
    } 
return returnedVal; 

將此項添加到xml的聽衆列表中。

+0

感謝您的回答,並且您是正確的,組織在surefire pom插件中生成,但如果我指定了suiteXMLFile,它將被忽略。所以基本上我想在命令行上動態地將組提供給POM,並且它應該能夠使用testNG運行這些測試。請任何其他想法? – Girish

2

完美。

從pom.xml中變量傳遞給ABC.java

的pom.xml

<properties> 
    <hostName>myhostname.com</hostName> 
</properties> 

而在ABC.java我們可以從這樣

System.getProperty("hostName") 
系統屬性調用它的最簡單方法
+1

'16:20:15,676 DEBUG TestApp:21 - hostname:null' – Wingie

+1

對於我來說,這也失敗了一個「null」 – JohnP2

0

你不需要使用環境變量或編輯pom.xml來使用它們。

在構建部分下調用Maven 3的目標和選項採用參數。試試這個(假設你參數生成):

Invoke Maven 3 
    Goals and options = test -Denv=$PARAM_ENV -Dgroup=$PARAM_GROUP 
1

傳遞參數,像瀏覽器等,可以按以下步驟進行:

<properties>  
    <BrowserName></BrowserName> 
    <TestRunID></TestRunID> 
</properties> 



     <!-- Below plug-in is used to execute tests --> 
     <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-surefire-plugin</artifactId> 
      <version>2.18.1</version> 
      <configuration> 
       <suiteXmlFiles> 
        <suiteXmlFile>src/test/resources/${testXml}</suiteXmlFile> 
       </suiteXmlFiles> 
       <systemPropertyVariables> 
        <browserName>${BrowserName}</browserName> 
        <testRunID>${TestRunID}</testRunID> 
       </systemPropertyVariables> 
      </configuration> 
      <executions> 
       <execution> 
        <id>surefire-it</id> 
        <phase>integration-test</phase> 
        <goals> 
         <goal>test</goal> 
        </goals> 
        <configuration> 
         <skip>false</skip> 
         <testFailureIgnore>true</testFailureIgnore> 
        </configuration> 
       </execution> 
      </executions> 
     </plugin> 

,並在Java代碼中使用這個來處理這個問題:

public static final String Browser_Jenkin=System.getProperty("BrowserName"); 
    public static final String TestRunID=System.getProperty("TestRunID"); 

    public static String browser_Setter() 
    { 
     String value=null; 
     try { 
      if(!Browser_Jenkin.isEmpty()) 
      { 
       value = Browser_Jenkin; 
      } 
     } catch (Exception e) { 
      value =propObj.getProperty("BROWSER"); 
     } 
     return value; 
    } 

    public static String testRunID_Setter() 
    { 
     String value=null; 
     try { 
      if(!TestRunID.isEmpty()) 
      { 
       value = TestRunID; 
      } 
     } catch (Exception e) { 
      value =propObj.getProperty("TEST_RUN_ID"); 
     } 
     return value; 
    }