Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass java code a parameter from maven for testing

I need to pass on following values …

exeEvironment (Test environment) , testGroup (Group in testNG)

from Command-Line -> POM -> TestNG -> Test cases.

Based on these two posts ....

pass a java parameter from maven

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

I did the following configuration ..

In surefire plugin, I tried following two options, none seem to work.

=====

(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 , can I use the the variable testGroup like …

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

This doesn't seem to work as well, do I need to define a parameter.


In the test cases , I tried to get he variables in following two ways …. (1)

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

(2)

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

like image 854
Girish Avatar asked Nov 06 '12 06:11

Girish


People also ask

How do I pass a command line argument in Maven?

Passing an Argument to MavenMaven will use the value (2.5) passed as an argument to replace the COMMON_VERSION_CMD property set in our pom. xml. This is not limited to the package command — we can pass arguments together with any Maven command, such as install, test, or build.

Can Maven be used for testing?

maven-verifier-plugin simply verifies the existence/absence of files on the filesystem. You could use it for functional testing, but you may need more features than maven-verifier-plugin provides.

How do I run a test case using Maven?

The Maven surefire plugin provides a test parameter that we can use to specify test classes or methods we want to execute. If we want to execute a single test class, we can execute the command mvn test -Dtest=”TestClassName”.

How to use JUnit 5 Parameterized Tests with Maven?

In order to use JUnit 5 parameterized tests, we need to import the junit-jupiter-params artifact from JUnit Platform. That means, when using Maven, we'll add the following to our pom.xml: Also, when using Gradle, we'll specify it a little differently: 3. First Impression

How do I pass an argument to Maven?

Passing an Argument to Maven Now, let's run Maven from our terminal as we usually do, with the package command, for example. But in this case, let's also add the notation -D followed by a property name:

How to provide parameter values to TestNG tests in Java?

There are mainly two ways through which we can provide parameter values to testng tests. The @Parameters annotation can be used for any of the @Before, @After, @Factory, and @Test annotated methods. It can be used to initialize variables and use them in a class, test, or may be for the whole test execution.

How do I run Maven from the terminal?

Now, let's run Maven from our terminal as we usually do, with the package command, for example. But in this case, let's also add the notation -D followed by a property name: Maven will use the value (2.5) passed as an argument to replace the COMMON_VERSION_CMD property set in our pom.xml.


2 Answers

This is the exact thing I was looking for my automation test and I got it working.

Command Line argument

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> 

TestNG test

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

The above works well. Additionally, if you need to use testng.xml, you can specify the suiteXmlFile like ...

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

Also, I prefer using @Parameters instead of parameters in @Test() as the later is deprecated.

like image 150
KingArasan Avatar answered Oct 07 '22 23:10

KingArasan


You need not define anything for groups in testng xml or the pom, the support comes inbuilt. You can simply specify the groups on the cmd line http://maven.apache.org/plugins/maven-surefire-plugin/test-mojo.html#groups

Hope it helps..

Edit 2:

Ok..so here's another option...Implement IMethodInterceptor

Define your custom property. Use -Dcustomproperty=groupthatneedstoberun in your command line call.

In the intercept call, scan through all methods ..something to the effect..

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

Add this to the listeners list in your xml.

like image 36
niharika_neo Avatar answered Oct 07 '22 22:10

niharika_neo