Prateek Neelgund
Prateek Neelgund

Reputation: 11

Maven Project : Able to run single test separately with TestNG test but unable to run whole project with maven test

I have Maven Project and all the configuration is correct, recently i configured the project with GIT and Jenkins,Created Jenkin job.

Earlier i was able to run the complete project by Right click on project and Run **--> **Run --> Maven test and test execution use to start but now its not throwing any error but not launching the browser for execution.

But if i run the java file [Single test case] separately using TestNG its working as expected[launching the browser and starts the execution]

As i have configured with Jenkins i cannot run every test case separately. I will have to run the project with Maven test

I have tried the possible solutions :

  1. Clean Project + delete m2 folder + Maven Run [just to make sure its not pointing to any old jar files] --> Did not work for me

Please find the code snippet that i have used in pom.xml

 

<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.19.1</version> <configuration> <testFailureIgnore>true</testFailureIgnore> </configuration> </plugin> <plugin> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.7</source> <target>1.7</target> </configuration> </plugin> <plugin> <artifactId>maven-resources-plugin</artifactId> <version>2.7</version> </plugin> </plugins> </build>

Thanks in advance for all the suggestions :)

Upvotes: 0

Views: 1648

Answers (3)

Swapnil Shrishrimal
Swapnil Shrishrimal

Reputation: 60

If your testng.xml or xml file with test cases is in root directory of project you can try below command.

  1. This will trigger all test cases present in testng.xml

         mvn clean test -Dsurefire.suiteXmlFiles=testng.xml
    
  2. This will execute single test case

         mvn clean test -Dtest=className
    

Please not that, you need to have surefire plugin into the pom.xml.

Upvotes: 0

Ishita Shah
Ishita Shah

Reputation: 4035

To trace cause, You may go through by below steps. And you will be found where is actual cause and it gets stuck.

1. Create Simple @Test with Console output:

public class NewTest {
  @Test
  public void f() {
      System.out.println("Hello World");
  }
}

2. Create TestNG.XML at root location of the project to execute above class:

<?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
    <suite name="Suite">
      <test thread-count="5" name="Test">
        <classes>
          <class name="packageName.NewTest"/>
        </classes>
      </test> <!-- Test -->
    </suite> <!-- Suite -->

3. Run and Check TESTNG.XML output

4. Run and Check output from POM.XML

POM.XML:

  <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.22.0</version>
            <configuration>
                <suiteXmlFiles> 
                    <suiteXmlFile>testng.xml</suiteXmlFile>
                </suiteXmlFiles>
            </configuration>
        </plugin>
  </plugins>

Upvotes: 1

Vladimir Efimov
Vladimir Efimov

Reputation: 799

  1. create a testNG xml and include all test classes there. This could be done by including the top level package:

<test name="sample-Test" preserve-order="true" verbose="2">
    <packages>
        <package name="org.sample.something.*"/>
    </packages>
</test>

2.1 Update your surefire plugin configuration to make it:

        <configuration>
           <suiteXmlFiles>
               <suiteXmlFile>src/test/resources/testng.xml</suiteXmlFile>
           </suiteXmlFiles>
        </configuration>

2.2 OR (more flexible as you can create different profiles pointing to different xml to run different test suites separately) Create a maven profile that will point to your testNG xml

     <profiles>
       <profile>
          <id>selenium-tests</id>
          <build>
             <plugins>
                <plugin>
                   <groupId>org.apache.maven.plugins</groupId>
                   <artifactId>maven-surefire-plugin</artifactId>
                   <version>2.19.1</version>
                   <configuration>
                      <suiteXmlFiles>
                         <suiteXmlFile>src/test/resources/testng.xml</suiteXmlFile>
                      </suiteXmlFiles>
                   </configuration>
                </plugin>     
             </plugins>
          </build>
       </profile>
    </profiles>
  1. mvn clean test -U -Pselenium-tests OR mvn clean test

source for maven

source for xml

Upvotes: 0

Related Questions