2012-11-06 21 views
15

ho bisogno di trasmettere valori seguenti ...Come passare il codice Java un parametro da Maven per la prova

exeEvironment (ambiente di prova), TestGroup (Gruppo in TestNG)

dal Comando Linea -> POM -> TestNG -> Casi di test.

Sulla base di questi due messaggi ....

pass a java parameter from maven

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

ho fatto la seguente configurazione ..

In plug infallibile, ho provato a seguito di due opzioni, nessuno sembra funzionare.

=====

(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> 

In testNG.xml, posso usare il la variabile testGroup li ke ...

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

Anche questo non funziona, devo definire un parametro.


Nei casi di test , ho cercato di ottenere lui variabili nei seguenti due modi .... (1)

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

(2)

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

risposta

33

Questa è la cosa esatta che stavo cercando per il mio test di automazione e l'ho fatto funzionare.

riga di comando argomento

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

mio Pom 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 prova

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); 
    } 
} 

ben Le opere di cui sopra. Inoltre, se avete bisogno di usare testng.xml, è possibile specificare il suiteXmlFile come ...

 <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> 

Inoltre, io preferisco usare @Parameters invece di parameters nel @Test() come in seguito è deprecato.

+0

Sembra promettente, lasciami provare e tornare indietro. Grazie per la condivisione. – Girish

+0

A proposito, hai mangiato usando testng.xml qui? – Girish

+4

Non sto usando testng.xml, Tra l'altro non è necessario aggiungere il parametro nel file POM. mvn prova pulita -Denvironment = QA -Dgroups = Regressione se si utilizza questo 1. unico gruppo di regressione sarà eseguito (@Test (gruppi = { "Sniff", "regressione"}) [email protected] ("environment") - il valore "QA" sarà passato direttamente al tuo test – KingArasan

2

Non è necessario definire nulla per gruppi in xml TestNG o pom, il supporto viene integrato. Si può semplicemente indicare i gruppi sulla linea di cmd http://maven.apache.org/plugins/maven-surefire-plugin/test-mojo.html#groups

Speranza che aiuta ..

Edit 2:

Ok..so ecco un'altra opzione ... Implementare IMethodInterceptor

Definite il vostro proprietà personalizzata. Utilizzare -Dcustomproperty = groupthatneedstoberun nella chiamata della riga di comando.

Nella chiamata di intercettazione, eseguire la scansione di tutti i metodi .. qualcosa in base all'effetto ..

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

Aggiungi questo all'elenco degli ascoltatori nel tuo xml.

+0

Grazie per la risposta e sei corretto, i gruppi sono inbuild nel plugin pom surefire, ma saranno ignorati se specifico suiteXMLFile, che sto specificando. SO fondamentalmente voglio fornire i gruppi dinamicamente sulla riga di comando al POM e dovrebbe essere in grado di eseguire quei test utilizzando testNG. Qualche altra idea per favore? – Girish

2

Perfetto.

Il modo più semplice per passare la variabile da pom.xml a ABC.java

pom.xml

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

E nel ABC.java possiamo chiamarla dalle proprietà di sistema come questo

System.getProperty("hostName") 
+1

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

+1

Anche questo non ha per me un "null" – JohnP2

0

Non è necessario utilizzare variabili di ambiente o modificare pom.xml per utilizzarli.

Gli obiettivi e le opzioni per Invoke Maven 3 nella sezione Crea prendono il parametro. Prova questo (supponendo parametrizzato build):

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

Passando parametro come browser e altro può essere fatto come di seguito:

<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> 

e per gestire questa situazione in uso codice Java questo:

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; 
    }