2012-11-06 16 views
15

muszę zdać się na następujących wartościach ...Jak przekazać kod Java parametr z Maven do testowania

exeEvironment (środowisko testowe), testGroup (Grupa w TestNG)

z Command Linia -> POM -> TestNG -> Przypadki testowe.

Na podstawie tych dwóch stanowisk ....

pass a java parameter from maven

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

zrobiłem następującą konfigurację ..

W murowany wtyczki, próbowałem następujących dwóch opcji, żaden nie wydaje się działać.

=====

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

W testNG.xml, można używać zmienna testGroup li ke ...

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

To nie wydaje się działać tak dobrze, czy muszę zdefiniować parametr.


W przypadków testowych, starałem się dostać he zmienne w następujące dwa sposoby .... (1)

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

(2)

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

Odpowiedz

33

Dokładnie to właśnie szukałem testu automatyzacji i udało mi się go uruchomić.

argumentem wiersza poleceń

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

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

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

Powyższe działa dobrze. Dodatkowo, jeśli trzeba użyć testng.xml można określić suiteXmlFile jak ...

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

Również wolę korzystania @Parameters zamiast parameters w @Test() jak później jest przestarzała.

+0

Wygląda obiecująco, pozwól mi spróbować i wrócić. Dziękuję za udostępnienie. – Girish

+0

A tak przy okazji, zjadłem cię tutaj używając testng.xml? – Girish

+4

Nie używam testng.xml, Przy okazji nie trzeba dodawać parametru do pliku POM. mvn czyste testy -Denvironment = QA -Dgroups = regresji jeśli używasz tego 1. jedyną grupą regresji zostaną wykonane (@Test (groups = { "wąchać", "regresja"}) [email protected] ("environment") - wartość "QA" zostanie przekazana bezpośrednio do twojego testu – KingArasan

2

Nie musisz niczego dla grup w XML TestNG lub pom określić wsparcie przychodzi wbudowana. można po prostu określić grupy na linii cmd http://maven.apache.org/plugins/maven-surefire-plugin/test-mojo.html#groups

nadzieję, że to pomaga ..

Edit 2:

Ok..so oto inna opcja ... Wdrożenie IMethodInterceptor

Zdefiniuj swoje własność niestandardowa. Użyj -Dcustomproperty = groupthatneedstoberun w linii komend.

Podczas rozmowy przechwytującej, przejrzyj wszystkie metody ... coś w tym stylu.

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

Dodaj to do listy słuchaczy w twoim xml.

+0

Dzięki za odpowiedź i masz rację, grupy są wbudowane w plugin surefire pom, ale zostanie to zignorowane, jeśli podam parametr suiteXMLFile, który podaję. Zasadniczo chcę dynamicznie udostępniać grupy w wierszu poleceń do POM i powinien on być w stanie uruchomić te testy za pomocą testNG. Jakieś inne pomysły, proszę? – Girish

2

Idealny.

Najprostszym sposobem, aby przekazać zmienną z pom.xml do ABC.java

pom.xml

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

A w ABC.java możemy nazwać to z właściwości systemu, jak to

System.getProperty("hostName") 
+1

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

+1

To również nie dla mnie z "zerową" – JohnP2

0

Nie trzeba używać zmiennych środowiskowych ani edytować pliku pom.xml, aby z nich korzystać.

Cele i opcje dla Invoke Maven 3 w sekcji Build ma parametr. Spróbuj to (zakładając, że parametryzowane kompilacji):

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

Passing parametr jak przeglądarki, a inne można zrobić jak poniżej:

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

i sobie z tym poradzić w użyciu kodu Java to:

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