Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven-surefire-plugin with TestNg : how to specify the directory where test suites xml files are stored?

I'm currently working on a Maven-backed project. I've chosen TestNg to implement my unitary tests. In order to run my unitary tests at each Maven build, I have added the maven-surefire-plugin to my pom.xml :

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.12</version>
            <configuration>
            <!-- Configuring the test suites to execute -->
                <suiteXmlFiles>
                     <suiteXmlFile>testsuite-persistence-layer.xml</suiteXmlFile>
                </suiteXmlFiles>
            </configuration>
        </plugin>

Moreover, I want to specify the tests to be executed using TestNg's TestSuiteXmlFile. For instance, in my pom.xml, I've configured the surefire plugin so that it will execute the tests defined in the xml file named "testsuite-persistence-layer.xml".

The problem is that by default, the surefire plugin seems to be looking for this xml file at the root of my project. How can I specify the directory in which the surefire plugin should look for the TestSuite xml files?

According to the TestNg documenation, this could be specified through the "maven.testng.suitexml.dir" property but the Surefire plugin does not seem to take it into account.

like image 741
kyiu Avatar asked May 21 '12 15:05

kyiu


1 Answers

I am not sure if I understand your problem. You can easily specify the exact location of xml file, both as relative and fully qualified path.

<suiteXmlFile>c:/some/dir/testsuite-persistence-layer.xml</suiteXmlFile>

or

<suiteXmlFile>src/test/java/com/something/project/testsuite-persistence-layer.xml</suiteXmlFile>

But that's too easy, so I am guessing you are looking for a way to parametrize the directory where xmls are located. The quick solution that comes to my mind would be

<suiteXmlFile>${xmlPath}/testSuite.xml</suiteXmlFile>

Now you can run

mvn test -DxmlPath=c:/some/path

Of course xmlPath is just made-up name, you can use any other variable name you wish.

If you don't want to pass the path as an argument from command line you can specify the value of xmlPath variable in properties section of your POM. Properties is one of the main sections located just under < project > branch.

<project ... >
    ...

    <properties>
        <xmlPath>c:/some/path</xmlPath>
    </properties>
        ...

    <build>
        ...
        <plugins>
        ...
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.9</version>
                <configuration>
                    <suiteXmlFiles>
                        <suiteXmlFile>${xmlPath}/testSuite.xml</suiteXmlFile>
                    </suiteXmlFiles>                                        
                    ...
                </configuration>
            </plugin>
        ...
        </plugins>
        ...
    </build>
    ...

</project>
like image 108
JacekM Avatar answered Sep 21 '22 08:09

JacekM