Search code examples
maventestngmaven-surefire-pluginmaven-shade-plugin

Not able to execute test cases via programatically suite generation


I have a Maven project for my Selenium Automation. I am programatically executing the test cases. I have built the class DynamicSuiteHelper.java for creating my suite dynamically. When I run this from my Eclipse IDE it executes as expected. Now, I want to execute this code using Maven.

So first I did mvn clean install. And then when I ran the automation.jar file it started execution, but the results show that 0 tests were ran. So I checked back in the output folder, which showed the suite file was created and it was created as I intended.

I am using three plugins:

1) maven-surefire:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.22.0</version>
</plugin>

2) maven-compiler:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.8.0</version>
    <configuration>
        <source>1.8</source>
        <target>1.8</target>
        <showDeprecation>true</showDeprecation>
        <showWarnings>true</showWarnings>
        <fork>true</fork>
        <compilerArgs>
            <arg>-verbose</arg>
            <arg>-Xlint:all,-options,-path</arg>
        </compilerArgs>
    </configuration>
</plugin>

3) maven-shade: (for generating executable jars)

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>3.2.0</version>
    <configuration>
            <createDependencyReducedPom>false</createDependencyReducedPom>
    </configuration>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>shade</goal>
            </goals>
            <configuration>
                <transformers>
                    <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                <mainClass>com.project.automation.tests.DynamicSuiteHelper</mainClass>
                    </transformer>
                </transformers>
            </configuration>
        </execution>
    </executions>
</plugin>

Also the test classes are in the same package as the DynamicSuiteHelper.java class.

Code snippet for DynamicSuiteHelper class:

    TestNG dynamicTestNG = new TestNG();
    XmlSuite dynamicSuite = new XmlSuite();
    List<XmlTest> listOfTests = new ArrayList<XmlTest>();
    XmlTest dynamicTest = new XmlTest(dynamicSuite);
    List<XmlClass> listOfClasses = new ArrayList<XmlClass>();
    listOfClasses.add(new XmlClass("com.project.automation.tests.test1.Test1Class1"));
    listOfClasses.add(new XmlClass("com.project.automation.tests.test1.Test1Class2"));
    listOfClasses.add(new XmlClass("com.project.automation.tests.test2.Test2Class1"));
    listOfClasses.add(new XmlClass("com.project.automation.tests.test2.Test2Class1"));
    dynamicTest.setClasses(listOfClasses );
    listOfTests.add(dynamicTest);
    dynamicSuite.setTests(listOfTests);
    List<XmlSuite> listOfSuites = new ArrayList<XmlSuite>();
    listOfSuites.add(dynamicSuite);
    dynamicTestNG.setXmlSuites(listOfSuites);
    dynamicTestNG.run();

Also the project structure looks like:

project
 |--automation
      |--tests
           |--test1
                |--Test1Class1.java
                |--Test1Class2.java
           |--test2
                |--Test2Class1.java
                |--Test2Class2.java
           |--BaseTest.java
           |--DynamicSuiteHelper.java

Any help is appreciated.


Solution

  • Found a solution and got the test cases up and running.

    Instead of maven-shade-plugin I used the maven-assembly-plugin. I added the test classes externally from plugin, and also some resources.

    What I observed was, maven-shade-plugin was not compiling my test-classes. So I tried out with maven-assembly-plugin.

    Thanks again everyone for giving your time.