Exclude log traces while TEST execution of maven surefire plugin - maven

My maven project executes maven-surefire-plugin v2.22.0 TEST while building the application. By default the log level of surefire execution is INFO, and this plugin uses [org.apache.logging.slf4j.Log4jLoggerFactory].
I don't want my build process to log these traces while executing : maven-surefire-plugin:2.22.0:test (default-test)
Please could anyone help how to skip logging in this plugin execution?
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<groupId>org.apache.maven.plugins</groupId>
<version>2.22.0</version>
I have tried exclusions and verbose=false, but no luck :(

Put alog4j.properties under src/test/resources with the following content:
log4j.rootLogger=OFF

Related

Maven skip specific test [duplicate]

In my maven project I have a number of modules. Is it possible to turn off running unit test for some modules via command line options?
My project takes about 15 mins to run through all unit tests. I would like to speed up the overall build by running just the unit tests in the module I am working on. I do not want to go in and edit each individual pom.xml to achieve this.
I have tried a solution outlined here: Can I run a specific testng test group via maven? However the result is a lot of test failures in modules that I want to skip. I suppose 'group' is not the same concept of module?
To toggle unit tests on and off for an entire project use Maven Surefire Plugin's capability of skipping tests. There is a drawback with using skipTests from the command line. In a multi-module build scenario, this would disable all tests across all modules.
If you need more fine grain control of running a subset of tests for a module, look into using the Maven Surefire Plugin's test inclusion and exclusion capabilities.
To allow for command-line overrides, make use of POM properties when configuring the Surefire Plugin. Take for example the following POM segment:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.9</version>
<configuration>
<excludes>
<exclude>${someModule.test.excludes}</exclude>
</excludes>
<includes>
<include>${someModule.test.includes}</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<someModule.skip.tests>false</someModule.skip.tests>
<skipTests>${someModule.skip.tests}</skipTests>
<someModule.test.includes>**/*Test.java</someModule.test.includes>
<someModule.test.excludes>**/*Test.java.bogus</someModule.test.excludes>
</properties>
With a POM like the above you can execute tests in a variety of ways.
Run all tests (the above configuration includes all **/*Test.java test source files)
mvn test
Skip all tests across all modules
mvn -DskipTests=true test
Skip all tests for a particular module
mvn -DsomeModule.skip.tests=true test
Only run certain tests for a particular module (this example includes all **/*IncludeTest.java test source files)
mvn -DsomeModule.test.includes="**/*IncludeTest.java" test
Exclude certain tests for a particular module (this example excludes all **/*ExcludeTest.java source files)
mvn -DsomeModule.test.excludes="**/*ExcludeTest.java" test
Found a way to exclude on command line:
# Exclude one test class, by using the explanation mark (!)
mvn test -Dtest=!LegacyTest
# Exclude one test method
mvn verify -Dtest=!LegacyTest#testFoo
# Exclude two test methods
mvn verify -Dtest=!LegacyTest#testFoo+testBar
# Exclude a package with a wildcard (*)
mvn test -Dtest=!com.mycompany.app.Legacy*
This is from: https://blog.jdriven.com/2017/10/run-one-or-exclude-one-test-with-maven/
…and if you like to pass the parameter to maven release plugin in Hudson/Jenkins you have to use
-Darguments=-DskipTests
to get it work.
If you want to use Maven profiles:
http://maven.apache.org/guides/introduction/introduction-to-profiles.html
you might want to make it work doing something like this:
Skipping tests in some modules in Maven
I don't know if there is a supported command line option that does the same.
You also might try using environment properties directly, something as per this doc page:
http://maven.apache.org/plugins/maven-surefire-plugin/examples/skipping-test.html
i.e. something like:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12</version>
<configuration>
<skipTests>${moduleA.skipTests}</skipTests>
</configuration>
</plugin>
then using mvn -DmoduleA.skipTests=false test to test that one module.

How to defer Maven test goal after deployment?

I'm using the JBoss and WildFly Maven plugin to deploy my applications.
<plugins>
<plugin>
<groupId>org.wildfly.plugins</groupId>
<artifactId>wildfly-maven-plugin</artifactId>
<version>1.0.0.Beta1</version>
</plugin>
</plugins>
The problem that I have found is that if I have some Junit test in my project they are executed before application deployment, leading to a test with an inconsistent application state. Is it possible to configure somehow this plugin to kick-in before the test phase ?
Thanks
This plugin by default executes alongside the package phase and you'll probably have issues attempting to run the plugin before your application is packaged. As an alternative, you could override the maven-surefire-plugin to run your tests in a later phase, such as integration-test, which would be executed after your application has been packaged.
A practical example can be found on this Maven tutorial where it's show how to include failsafe plugin and bind it to the executions of the integration-test and verify phase. This way tests which are engineered as integration test (e.g ending in *IT) will execute only during the integration-test phase.

Sonar Maven Plugin: How do I exclude test source directories?

I have a Maven project with Java sources and Scala test sources. I generate code coverage using Jacoco during the verify stage. When I try to run the sonar goal either during the verify phase by adding an execution, or by running mvn verify sonar:sonar, I end up with the test directory being added twice by Sonar:
[INFO] [11:15:34.756] Test directories:
[INFO] [11:15:34.756] /Users/xxx/Documents/workspace/misc/xxx/src/test/scala
[INFO] [11:15:34.756] /Users/xxx/Documents/workspace/misc/xxx/src/test/scala/../scala
which results in the analysis failing with the following error:
Failed to execute goal org.codehaus.mojo:sonar-maven-plugin:2.1:sonar (default-cli) on project kv-mapper: Can not execute SonarQube analysis: Unable to read and import the source file : '/Users/xxxx/Documents/workspace/misc/xxx/src/test/scala/../scala/xxx/xxxxx/xxx/xxx/xxxxx/xxxxxx.java' with the charset : 'UTF-8'. Duplicate source for resource
My pom.xml (for Sonar) looks like this.
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>sonar-maven-plugin</artifactId>
<version>${sonar.plugin.version}</version>
<!-- no default executions -->
<configuration>
<sonar.core.codeCoveragePlugin>jacoco</sonar.core.codeCoveragePlugin>
<sonar.dynamicAnalysis>reuseReports</sonar.dynamicAnalysis>
<sonar.language>java</sonar.language>
<sonar.jacoco.itReportPath>
${basedir}/target/jacoco.exec
</sonar.jacoco.itReportPath>
<sonar.exclusions>
**/test/*
</sonar.exclusions>
</configuration>
</plugin>
How do I configure Sonar to either:
exclude test/scala directory entirely?
or
remove the duplicate directory?
sonar.test.exclusions should be used instead of sonar.exclusions
<sonar.test.exclusions>
**/test/*
</sonar.test.exclusions>
Either add a step to remove the folder before running the SonarQube analysis.
Or set exclusions on test files. See http://docs.sonarqube.org/display/SONAR/Narrowing+the+Focus#NarrowingtheFocus-IgnoreFiles

Displaying custom test results in Jenkins Maven job

I just added some Python unit tests to an existing Maven POM but I can't seem to get Jenkins to report the results of the tests when it runs the build.
I'm running nose tests from Maven with the exec-maven-plugin during the "test" phase. The tests run successfully from the Jenkins job and generate an xUnit compatible test report to target/surefire-reports/TEST-nosetests.xml, but Jenkins doesn't pick up on the results.
Interestingly, Maven also reports no tests run before executing the test suite:
-------------------------------------------------------
T E S T S
-------------------------------------------------------
There are no tests to run.
Results :
Tests run: 0, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] --- exec-maven-plugin:1.1.1:exec (nosetests) # server ---
[INFO] ................
[INFO] ----------------------------------------------------------------------
[INFO] Ran 16 tests in 194.799s
[INFO]
[INFO] OK
[INFO] Registering compile source root /Volumes/Data/workspace/myProject/target
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
So is this a problem with Jenkins not seeing the results, or with Maven not treating my test suite as actual tests?
I worked through this problem by using a "free-style software project" in Jenkins rather than the "maven2/3" project.
Under Build, choose Add build step and configure the project to Invoke top-level Maven targets. I'm using the test target.
Finally, under Post-build Actions choose Add post-build action of Publish JUnit test result report and point it at the xUnit output from your tests. This option is not available for Maven 2/3 jobs for some reason.
To build upon the answer by bpanulla, if you have tests in more than one sub-directory of your project, you can use a wildcard in the "Test report XMLs" field, such as: **/target/surefire-reports/*.xml
If you have a more modular Jenkins setup using a free project will not correctly build submodules. If the maven-plugin that generates the reports execution id is e2eTests, then add the following snippet to your pom.xml and the jenkins maven plugin will take care of the rest!
<properties>
    <jenkins.e2eTests.reportsDirectory>target/protractor-reports</jenkins.e2eTests.reportsDirectory>
</properties>
See https://wiki.jenkins-ci.org/display/JENKINS/Building+a+maven2+project
Try after adding the maven compiler plugin
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<executions>
<execution>
<id>default-testCompile</id>
<phase>compile</phase>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
<configuration>
<encoding>ISO-8859-1</encoding>
<source>1.8</source>
<target>1.8</target>
<useIncrementalCompilation>false</useIncrementalCompilation>
<testSource>1.8</testSource>
<testTarget>1.8</testTarget>
</configuration>
</plugin>
As you can see in the Tests run output that Maven didn't recognized the tests as tests. Furthermore to see the results in Jenkins you need to check if nope can create junit compatible output result files (which i assume) which can be read by jenkins and of course displayed.

Test output folder not created in target/surefire reports when I run as maven Test

Test output folder not created in target/surefire reports when I run as maven Test.
When i run my suite as maven test, surefire reports gets updated but test-output folder is not created. But when I run as Testng Suite, test-output folder is created.
what should I do to have test-output folder created on Run->Maven test?
When you run as Testng Suite you would be using Testng's default report location which is
${your base dir}/test-output
When you are running from maven, you are basically using Maven's surefire plugin to execute the testng tests. The reports output directory by default for surefire is
${your build directory}/surefire-reports
That is why you see the discrepancy in both runs.
To get the output in the same folder as testng does, you can explicitly specify the reports directory for the surefire plugin under the configuration section
<reportsDirectory>${basedir}/test-output</reportsDirectory>
and then run as maven-test to see the output there.
You can refer to the below link to see how to configure your pom's surefire plugin.
http://maven.apache.org/plugins/maven-surefire-plugin/examples/testng.html
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.16</version>
<configuration>
<testFailureIgnore>true</testFailureIgnore>
</configuration>
</plugin>
</plugins>
</build>

Resources