Unit test results not showing in Sonar when building child projects in Jenkins - unit-testing

I have some maven projects configured in Jenkins, and I execute them from a parent project (which have them as <modules> in its pom.xml, the children have this project defined as <parent>).
The unit tests execute successfully for each project in Jenkins and it shows the results, but then in Sonar all projects appear as having no unit tests.
The sonar configuration for each project is
sonar.projectKey=project:key
sonar.projectName=project_name
sonar.projectVersion=1.0
sources=src/main/java
tests=src/test/java
binaries=target/classes
Do I have to do anything else in order for Sonar to pick up the results of the unit tests being executed?
Thanks in advance.

From what I see, I guess you're trying to run Sonar analyses with the Sonar Jenkins plugin "on-the-fly" mode, where you specify properties.
You have to understand that this mode does not support running tests: it can only reuse test reports (if you specify "sonar.dynamicAnalysis=reuseReports").
But as you are using Maven, why don't you just run the Sonar post-build action? (see documentation for that)

Related

Adding test cases to TeamCity

I have written some automated test cases in java (selenium IDE)for a project.The project is using ruby on rails. The project was configured in TeamCity. Now I am planning to add these test cases as a build step. How can I achieve this. Which build step should I use.
How do you run them on your local machine?
If you run them from command line you can create build configuation with Command Line runner.If you run them by running JUnit test you can create a build configuration that runs JUnit tests (using some build tools like Ant, Maven, Gradle).

Sonarqube 4.3 dont trigger Cobertura unit test execution on Jenkins

we had SonarQube v3.4 and recently upgraded to SonarQube V4.3. Also we use Jenkins to build and trigger sonar analysis on Maven projects and Cobertura as the code coverage tool.
In the version 3.4, Sonar used to trigger (with Jenkins) the cobertura execution and collect the data, but when we upgraded to Sonar 4.3, there was no option in Sonar to select the default code-coverage tool, the Jenkins Sonar execution no longer triggers cobertura and unit test execution. Now it expects a Jacoco dump file in order to provide coverage data to sonar.
I cannot find any useful documentation in Sonarqube confluence.
How can we configure Sonar to use Cobertura (without modifying ALL the projects pom files) and trigger code coverage unit test execution from Jenkins?.
Thanks!.
Since version 2.2 of Java Plugin and 1.6 of Cobertura plugin, SonarQube does not trigger unit test execution anymore : Only reuse report mode is available.
Please note that the recommended version for both of those plugins are the fixed versions Java 2.2.1 and Cobertura 1.6.1 to prevent the collision between JaCoCo and Cobertura plugin (which will happen with 2.2).
Because of all this, the property to select the default code coverage tool is not relevant anymore and you only have to provide a coverage report (either cobertura or JaCoCo) to get your coverage information.
Please refer to these piece of documentation to get a rough idea on how to do so :
http://docs.codehaus.org/display/SONAR/Cobertura+Plugin
http://docs.codehaus.org/display/SONAR/JaCoCo+Plugin

Unable to execute unit tests in a maven project from within intellij

I have inherited a legacy project with 100s of tests, and dependencies defined within the pom.
All of the tests run when I execute a mvn clean install from the command line, but when I try to execute one of these tests in debug mode from within Intellij i get the following error.
java.lang.NoClassDefFoundError: Could not initialize class
How can I get intellij to recognise these dependencies when trying to run a test in debug mode from the ide?
I managed to solve this by simply changing the working directory location in the Run configuration to point to the correct classpath location. The default location had been taken from a parent project.
It would be very convenient if one was able to debug tests from within IntelliJ. I have run into similar issues with my massive project and have found a workaround for it.
When wishing to debug a test, I have often found it useful to use a remote debugging session like so:
mvn -Dmaven.surefire.debug="-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 -Xnoagent -Djava.compiler=NONE" -Dtest=com.autofunk.TheFunkImplTest test -DfailIfNoTests=false
You can then attach to the above using a remote debug session from within IntelliJ on port 8000.
Therefore, when debugging tests I first check to see whether a simple debug from within Intellij works. If not, I run the above and then attach to it using a debug configuration like so:
IntelliJ execute unit tests without mvn command.
a IntelliJ plugin: MvnRunner
You can run tests using the maven project window in IntelliJ
View - Tool Windows - Maven Projects
Then under the project or module you wish to test open the lifecycle goals and click test.
This will run the currently configured test goal. Now the report is logged into the target directory
I use
https://github.com/destin/maven-test-support-plugin
to view the test results.
You can access this screen once the plugin is installed from the Project window again right click on the projects root and select "Show Test Results" (should be below the maven icon)
Good luck

Why my Sonar Jenkins job never becomes unstable, even with test failures?

I have a job in Jenkins that is run every night. The tasks executed during this build are: compilation, unit tests, integration tests (which are only JUnit tests which are longer than "real unit tests" to execute), and Sonar quality analysis.
When a test fails, the job is however considered as successfull and thus, no email is sent to notify this failure.
The Maven command used is mvn clean install sonar:sonar. Removing the install goal does not change anything.
What is wrong with that?
Is there a way to get the expected behavior (i.e. having an unstable build when a test failed) with only one Jenkins job, or should I create two jobs, one for the whole "Java part" (compile, unit test and integration tests), and one for the Sonar analysis?
We are using Maven 2.0.9, Java 1.6, Sonar 2.8, Jenkins 1.413.
Jenkins seems to set that property: Hudson build successful with unit test failures
With the property (-Dmaven.test.failure.ignore=false), when there is a test failure, the build stops.
There is a jenkins plugin for sonar:
That seems to analyze even if Tests fails: http://jira.codehaus.org/browse/SONARPLUGINS-461
In my sonar installation, I run the tests seperate from sonar and reuse the junit/surefire reports. That way I can control the tests independently from sonar.

double unit test reporting with hudson and maven

I have a maven2 project in hudson and when the cobertura reporting plugin runs, it causes the unit tests to show that they have run twice. I don't mind them running multiple times, but the trend graph shows twice as many tests as we actually are running. Is there a way to make sure the graph only shows them once?
thanks,
Jeff
This is a known bug. Just wait for it to be fixed.
The workaround I use (works in Hudson 1.391) is to configure cobertura in separate Maven profile and run it in a Hudson job as a post-build step.
Mode detailed instructions:
Add cobertura to your project pom in a special profile (so it won't run while default lifecycle) and configure it to create report in xml format.
Install "Hudson M2 Extra Steps Plugin"
Configure your Hudson job as Maven 2 project
In your job configuration in the "Build" section configure usual clean/install goals
In "Build Environment" section select "Configure M2 Extra Build Steps" and add Maven post-build step. Configure it to run "cobertura:cobertura -P your_cobertura_profile_name"
In "Post-build Actions" select "Publish Cobertura Coverage Report" and configure proper xml report pattern (default should work just fine)
I had the same problem recently when I was running maven goals test and emma:emma on the same job. emma seems to have rerun all tests thus doubling the results. When I removed goal test my unit tests still got executed but test results went back to normal. Could be the same with cobertura.