问题
**FIRST: I have read literally every post in here on how to do this, I am following all instructions to the letter including those from Maven itself.
I have converted an Eclipse Selenium project using TestNG over to a Maven project. I cannot get Maven to run my tests from the Test.XML file instead of running in the default mode where it just picks up anything with the word "Test" in the file name... I can execute the test.xml directly and everything works, so I think I got the conversion right. Since I am converting from an existing framework the effort required to switch everything to work with the default "just grab anything called 'test'" is prohibitive, I want my Test.XML file to be used by SureFire.
I am new to Maven so forgive my not using right terms but: when I 'run' the POM file with the 'goal' of TEST it simply ignores my test.xml file and runs anything with the word "test" in it, i verified this by changing names etc... I am running from Eclipse mostly, same results the few times I tried command line.
Here is my POM:
<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>test</groupId>
<artifactId>TestSuite</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>TestSuite</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugin</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.16</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>src/test/resources/test.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.8.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>net.sourceforge.jexcelapi</groupId>
<artifactId>jxl</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.35.0</version>
</dependency>
</dependencies>
</project>
If you need more information let me know I will amend but my TestNG file literally just calls one test class no frills and it works if I execute it directly as a testNG test. Rest of the code works so I am assuming it's not that. Maybe a setting in Eclipse?
回答1:
I cannot get Maven to run my tests from the Test.XML file instead of running in the default mode where it just picks up anything with the word "Test" in the file name.
That is probably because of the Maven Surefire Plugin's default inclusion patterns:
By default, the Surefire Plugin will automatically include all test classes with the following wildcard patterns:
"**/Test*.java" - includes all of its subdirectories and all java filenames that start with "Test". "**/*Test.java" - includes all of its subdirectories and all java filenames that end with "Test". "**/*TestCase.java" - includes all of its subdirectories and all java filenames that end with "TestCase".
And, as you know, "Suite XML Files configuration will override the includes and excludes patterns and run all tests in the suite files."
I cannot tell for sure, but it looks like there are some troubles with reading the test.xml file (file is not found, parsing, encoding issue, etc) and Surefire falls back to the inclusion/exclusion policies.
You may do a little test to prove this. Add 'includes' and/or 'excludes' sections inside 'configuration' and exclude all classes except random one. If that random class is run, then the 'suiteXmlFiles' is simply ignored.
Hope this will narrow down the scope.
回答2:
The groupid is incorrect. You are missing an s in the plugin. It should be <groupId>org.apache.maven.plugins</groupId>
I think what is happening because of this is maven is defaulting to it's default test phase config and ignoring yours.
Hope it helps.
来源:https://stackoverflow.com/questions/19715760/how-to-get-maven-to-use-my-testng-xml-file