Does eclipse has a feature or plugin that shows the used code branch for an execution?
I known about coverage plugins for junit test, but I would like one for normal runtime execution instead of unit tests.
The EclEmma code coverage plug-in is not specific to JUnit executions. Measuring code coverage is an added launch mode, like Run, or Debug.
https://www.eclemma.org/userdoc/launching.html
Related
I would like to create an automated junit test that passes if my code meets the requirements specified by PMD, and fails if it does not. Is this possible?
It is possible tu run a PMD analysis in a JUnit test. However, there exist PMD plugins for every Java IDE, build and CI tool out there. Are you sure they don't already do what you want to achieve?
If you still want to do it yourself, take a look at the class PMD. It has the static method PMD.doPMD(configuration) which starts the analysis.
The same class also contains the main method in case you want to start it like from the command line.
I'm working on a project that has a lot of legacy code that is not covered with tests.
Is there any way that I could set up the integration server to check that all new commits have a minimum amount of tests (say, coverage is >70%)?
Essentially, I see two options:
Somehow set up the CI server to fail the build when the committed changes are not covered with unit tests. This will ensure that every piece of new code will have tests and that tests for the legacy code will increase with each change.
Set a coverage threshold for the whole project and fail the build if the coverage percentage decreases after a commit. The problem with this is that if I delete a class containing 100 instructions and add a new class with 50 instructions the coverage percentage will go up without me writing any tests.
I like option 1 more because it forces changes in legacy code to be unit tested. This should increase the overall test coverage.
Right now we're using Jenkins as our CI server and JaCoCo for test coverage. Maven is used for building the project and SVN is our main source control.
I know you can configure Jenkins to verify that there is at least one test file as part of the commit. That would not assure good test coverage, but at least you would know there was some kind of test related changes.
Some coverage tools (like cobertura) support excluding packages. This way, you can exclude all the old code (assuming it can be pattern matched) and have cobertura check only new code (which covers new commits).
I hope this helps.
For option 2, you can use the Jenkins JaCoCo plugin to track the code coverage for each build and set the build result to passed or failed depending on the coverage metrics.
I like option 1 better, too, but I don't know of a built-in way for Jenkins to do this. It should be fairly easy (at least at the class level) to post-process the coverage data and combine it with the SVN revision info, something like:
Parse the JaCoCo output files and find classes that have 0% coverage
Get the file(s) that changed for this build from the SVN revision details (Jenkins makes the revision numbers available in environment variables, SVN_REVISION if there is only one for this build or SVN_REVISION_1, SVN_REVISION_2, ... for multiple)
Print an error message if any of the changed classes have 0% coverage
Use the Jenkins Text Finder plugin to fail the build if the error message is printed.
This isn't a full solution, it gets trickier for new methods or lines that aren't covered by tests. Gives me an idea for a new Jenkins plugin ;-)
I have built a tool which does exactly this
https://github.com/exussum12/coverageChecker
You pass in the diff of the branch and the cover test output from the tests. The tool works out which lines in the diff are also in the clover file. and fails the build if less than a certain percentage
to use
bin/diffFilter --phpunit diff.txt clover.xml 70
to fail builds when less than 70% of the diff is covered by a test
I can add other formats if needed
Edit
I have added jacoco
Bin/diffFilter --jacoco diff.txt jacoco.xml
I am measuring the code coverage in my project using the EclEmma plugin for Eclipse. This involves running the coverage for the whole project. But due to some dependency issues tests in some packages are failing altogether. When the coverage for these package is taken individually, the tests run properly and the package is showing the coverage correctly.
Is it possible to get a Coverage report, by running the coverage for each package separately and then merging these reports into one.
Or Alternatively, are there any other free plugins which offer the above capability.
Note: Modifying the test methods to remove the dependency may not be possible due to logical and size constraints.
It's been a while since I shifted to IDEA, but I seem to recall that there was an option (as in "button in the EclEMMA view") to merge several coverage runs.
A visit to http://www.eclemma.org/ confirms this - look for "Merge Sessions". Also:
http://www.eclemma.org/userdoc/sessions.html and
http://www.eclemma.org/userdoc/coverageview.html
It is the button to the right of the "double-X" Remove all sessions button.
Cheers,
I have set up eclipse to work just as I want with my java web app using the following instructions : https://stackoverflow.com/a/6189031/106261.
Is it also possible to get unit tests to be run as part of the auto build, without running a maven install (or test). So I make a change to a class, the tests get run, and if a fail occurs I get a some sort of indicator. Without needing to manually run maven test.
There are several ways to achieve this, all with their own limitations:
You could set up a CI server which builds your project every time you commit a new version. Very reliable but not really "real time"
You can add your own builder to the list of builders (project properties -> Builders), for example an Ant builder which runs "ant test" or something. This builder gets invoked every time you save. Every time. That means Eclipse will become a total slug unless running your unit tests takes less than a few milliseconds.
You can use one of the plugins mentioned here: Is it possible to run incremental/automated JUnit testing in Eclipse?
I've a small project where the main code is wrote in java but the tests are mostly groovy code. If i use JaCoCo for code coverage (called by Sonar), the code coverage is not done on my java code, as it seems JaCoCo does not use its special classloader when loading the java classes with groovy. Strangely, I remember it worked before (before sonar version 2.12 which embed JaCoCo).
Does anyone know how to fix it ?
My mistake: forgot to add the integration-test maven profile to the sonar configuration in jenkins.