maven surfire testng execution not working - java

I have problems setting up my project for testng usage.
Background is that I want to execute my test.xml with maven command line.
I use this plugin within the build section of my pom.xml:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>test.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
However... when I call mvn clean test -DxmlSuiteFile=test.xml
all my unit tests are executed but the test.xml is untouched.
when I change the version of surefire to 2.22.2 everything works fine.
BUT I get the message: testSuiteXmlFiles0 has null value
when running only mvn test
I am confused how to get that to run with the surefire 3.0.0-M5 ?

-DxmlSuiteFile=test.xml is not required if it already defined in pom.xml.
Try to define surefire testNG provider explicitly:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>test.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
<dependencies>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-testng</artifactId>
<version>3.0.0-M5</version>
</dependency>
</dependencies>
</plugin>
test.xml file should be in the project root.
See also:
https://maven.apache.org/surefire/maven-surefire-plugin/examples/providers.html

Related

How to add maven-checkstyle to existing project?

I tried adding the maven-checkstyle-plugin to my project, but it does not produce any results:
<project>
....
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<configLocation>google_checks.xml</configLocation>
</configuration>
</plugin>
</plugins>
</build>
</project>
mvn checkstyle:checkstyle or mvn checkstyle:check results in:
[INFO] You have 0 Checkstyle violations.
Which is essentially not true, because I added violations explicit in my code.
So my guess is that checkstyle is not running the analyser properly over my code (that resides just normal in /src/main/resources).
What could I do?
Would I maybe have to add the google_checks.xml file explicit to my classpath??

Executing single TestNG test case with using Maven/Jenkins

Can anyone tell me how to execute single testcase with maven or jenkins.
mvn -Dtest=smoke tests seem to be not working for me.
I have tried other options like mvn clean install -Dtest=smoketest test that time i got below error if is set to true, and no tests execute if set to false`
Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.19.1:test (default-test) on project ABC: No tests were executed! (Set -DfailIfNoTests=false to ignore this error.)
POM USED
<project>
<groupId>com.testng.smoke</groupId>
<artifactId>ABC</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<build> <!-- Source directory configuration -->
<sourceDirectory>src</sourceDirectory>
<plugins> <!-- plugins executes the testng tests -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<configuration>
<systemPropertyVariables>
<AppName>XYX</AppName>
<browserName>chrome</browserName>
<isLocal>true</isLocal>
</systemPropertyVariables>
<!-- Suite testng xml file to consider for test execution -->
<suiteXmlFiles>
<suiteXmlFile>${suiteFile}</suiteXmlFile>
</suiteXmlFiles>
<groups>${chooseGroup}</groups>
<includes>
<include>**.java</include>
</includes>
<testFailureIgnore>true</testFailureIgnore>
<argLine>
-javaagent:${settings.localRepository}/org/aspectj/aspectjweaver/${aspectj.version}/aspectjweaver-${aspectj.version}.jar
</argLine>
</configuration>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${aspectj.version}</version>
</dependency>
</dependencies>
</plugin>
<!-- Compiler plugin configures the java version to be usedfor compiling the code -->
</plugins>
</build>
</project>
It doesnt matter if its jenkins or local build.
You have to specify you test in the following way:
mvn -Dtest=TestClass#testmethod test
You forgot about that #.

Cannot run multiple executions in maven surefire?

I want to run test classes whose name end with ResourceTest.java, so I defined following execution.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</configuration>
<version>2.12.2</version>
<executions>
<execution>
<id>resource-tests</id>
<phase>resource-tests</phase>
<goals>
<goal>resource-tests</goal>
</goals>
<configuration>
<includes>**/*ResourceTest.java</includes>
<!-- <exludes>**/*.java</exludes> -->
</configuration>
</execution>
</executions>
</plugin>
But I'm not sure how to run this, I've searched a lot and I'm missing something.
I tried surefire:test, it skipped all the test cases as defined in above configuration. So, I tried surefire:resource-tests, maven is saying no goal is not defined.
I'm using eclipse to run my maven build, by passing these parameters. How can I run by the execution id?
How to select a specific execution when running with surefire:test when I've mulltiple executions defined in my pom?
What am I missing? Any help would be appreciated.
There are several problems with your current configuration :
you are forcing the maven-surefire-plugin to be executed in the resource-tests phase but this phase does not exist. You should delete that declaration to keep the default plugin binding phase, which is test.
you are invoking the goal resource-tests but maven-surefire-plugin does not define such a goal.
the <includes> element is ill-defined. There should be a <include> tag under it.
you are excluding all Java files from the plugin configuration so no test will be run
the configuration of the plugin should be done under the <configuration> element not for each <executions>.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12.2</version>
<configuration>
<includes>
<include>**/*ResourceTest.java</include>
</includes>
</configuration>
</plugin>
When you have multiple executions and you want "select" one of them, you can use a profile:
<profiles>
<profile>
<id>resource-tests</id>
<properties>
<test-classes>**/*ResourceTest.java</test-classes>
</properties>
</profile>
<profile>
<id>task-tests</id>
<properties>
<test-classes>**/*TaskTest.java</test-classes>
</properties>
</profile>
</profiles>
with the following plugin configuration:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12.2</version>
<configuration>
<includes>
<include>${test-classes}</include>
</includes>
</configuration>
</plugin>
With such a configuration:
when you run mvn clean test -Presource-tests, only the classes matching **/*ResourceTest.java will be tested
when you run mvn clean test -Ptask-tests, only the classes matching **/*TaskTest.java will be tested

Unable to generate Allure reports using allure-maven plugin

I am unable to generate Allure test html reports using allure-maven plugin.
I am using the same version of testNG-adapter and allure maven plugin (1.4.0.RC8). But I am able to generate the allure html reports using Allure CLI.
My pom.xml excluding dependencies is
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.14</version>
<configuration>
<testFailureIgnore>false</testFailureIgnore>
<argLine>
-javaagent:${settings.localRepository}/org/aspectj/aspectjweaver/${aspectj.version}/aspectjweaver-${aspectj.version}.jar
</argLine>
</configuration>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.1</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
<reporting>
<excludeDefaults>true</excludeDefaults>
<plugins>
<plugin>
<groupId>ru.yandex.qatools.allure</groupId>
<artifactId>allure-maven-plugin</artifactId>
<version>1.4.0.RC8</version>
<configuration>
<outputDirectory>${basedir}/target/allure-reports/</outputDirectory>
<allureResultsDirectory>${basedir}/target/allure-results</allureResultsDirectory>
</configuration>
</plugin>
</plugins>
</reporting>
Add property allure.version to your pom.xml
See https://github.com/allure-framework/allure-core/wiki/Allure-Maven-Plugin
As you are not showing full POM, this example you can follow to make sure all plugin & property is correct.
https://github.com/sarkershantonu/Automation-Getting-Started/blob/master/AllureJunit/pom.xml
And, for command..
Testing => mvn clean test
Generating report => mvn site
Now, if you are running in CI servers, that need allure plugins to show report but if you are running locally, use jetter server (see my POM)
To see report via jettey : mvn jetty:run

maven surefire plugin does not inject system properties

This is how I set the values
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.17</version>
<configuration>
<systemProperties>
<property>spring.active.profiles</property>
<value>Test</value>
</systemProperties>
<systemPropertyVariables>
<spring.active.profiles>development</spring.active.profiles>
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>
</build>
The test fails
#Test
public void testGetProfile() throws Exception {
assertEquals("development", System.getProperty("spring.active.profiles"));
}
I see
java.lang.AssertionError:
Expected :development
Actual :null
What am I missing here?
Nothing wrong!
Run it in a console mvn test
OR
Eclipse --> [project] --> Run as --> Maven test
This test maybe fail if you run it via Eclipse --> Run as --> JUnit, sometimes this action ignores the pom.xml variables.
My bad, plugin was in a different module that test

Categories

Resources