JMockit generate coverage report from command line - java

I have the following command :
java -javaagent:jmockit.jar -cp ./out:junit.jar:hamcrest-core-1.3.jar org.junit.runner.JUnitCore TestCompareNumbers
where TestCompareNumbers is my test class for which i want a coverage report.
The result is :
JUnit version 4.12-beta-3
.....
Time: 0.011
OK (5 tests)
But a coverage report file hasnt been generated. I guess that Ive missed an option, I searched on google but i have no answer for that.
Thanks!

If your are looking for coverage using jmockit for a non-jmockit based code you should use javaagent= jmockit-coverage.jar
From the jmockit documentation
When not using the JMockit mocking APIs, code coverage can still be activated without adding any jar to the classpath. Instead, run with "-javaagent:/jmockit-coverage.jar" as a JVM initialization parameter.

Related

Generate Trace of JUnit test on success

I have JUnit tests that work without errors. And what I would like to do is to generate a trace file of these tests for example havinge execution time of each test class, list of methods tested ant other.
Thank you.
Your build tool should do this for you. For instance if you are using maven have a look at http://maven.apache.org/surefire/maven-surefire-report-plugin/, for ant you could use https://ant.apache.org/manual/Tasks/junit.html and https://ant.apache.org/manual/Tasks/junitreport.html.

SonarQube Multi-Module Code Coverage

I have a multi-module project that I can't seem to get an accurate unit test code coverage report on SonarQube. I use buildr and JaCoCo for the test coverage generation. The file heirarchy is similar to below.
Project
--Module1
----Reports
------Jacoco
--------jacoco.cov (jacoco execution file, previously used as .exec)
--Module2
--Reports
----Jacoco (Generated HTML, CSV, and XML report files)
----JUnit (JUnit xml report)
At this time, no unit tests exist for Module2. The problem I believe is that the overall coverage on SonarQube reflects only for Module1 and ignores Module2 completely. This makes the coverage appear higher than what it truly is for the whole project. Does anyone have any thoughts on this? Thank you for your input.
If I get your problem correctly you are looking to force coverage to 0% when there are no coverage file generated.
You have to set the correct parameter for this behaviour :
sonar.jacoco.reportMissing.force.zero=true
see documentation for more details : http://docs.sonarqube.org/display/PLUG/Usage+of+JaCoCo+with+Java+Plugin

Gradle + TestNG only runs certain tests as single tests

I've been using Gradle + TestNG + Java + Selenium for my web UI tests for quite a while now and I've only recently run in to this issue. For some reason when I try to run a single test class using -DtaskName.single=ExampleTestClass where ExampleTestClass would be ExampleTestClass.java it only works on some of my test classes.
I'm getting the error: Could not find a matching test for pattern: ExampleTestClass
I've seen this error in the past due to typos or missing #Test annotations etc, so I'm familiar with the "normal" cause, but this is quite bizarre as it appears to work on some test classes and not others. I've inspected the code and all annotations and groups are in place for the test methods, they run fine from my IDE (IntelliJ), and they are all located in the same directory / package path. Is there something I'm missing here? I don't know if I'm seeing things but I did notice that it didn't work with a test class that did not have Test as the last four characters of the Java class name but upon renaming it, still no dice. I've read the documentation and can't find anything wrong. Is there anything else that may be causing this to fail? It's quite odd since these tests are all so similar in every way. I even checked character encoding etc - no discrepancies between any of them.
Any advice or ideas on where to look next would be great.
Cheers,
Darwin
I ran into the exact same thing with gradle 1.6 (haven't had time to upgrade), and TestNG. A single test in a project with multiple tests get skipped and also gradle complains about not finding it if you try running the single test alone. Debug run shows the missing test .class file being found by gradle.
I worked around it by adding an #Test annotation to the test class in addition to the test method. That seems to make gradle find it.
I recently ran into a similar issue. I tried to run a test with -DTest.single and --tests but it wasn't found. After a bit of frustration I realized that the test was in a test group that was excluded in my test task configuration. I had incorrectly assumed that running with -Dtest.single would overrule exclusions, including that test's group allowed it to run as a single test.

maven not running all subset of tests I ask it to when using #

I am trying to run a couple of tests from different classes, my command line is:
mvn -Dtest=com.MyComp.Selenium.SelTests.SomeTests1#XTest,com.MyComp.Selenium.SelTests.SomeTests1#YTest,com.MyComp.Selenium.SelTests.SomeTests2#ZTest
When I run this I would expect it to run XTest and YTest from the class SomeTests1 and ZTest from SomeTests2 but instead it just runs XTest and ZTest and skips YTest.
If I just tell it to run YTest or if I tell it to run all tests in SomeTests1 it works. Please can somebody see what I'm doing wrong?
I don't want to run all the tests in each class I just want a subset.
If you are using Junit 4.x and surefire 2.12.1 or greater then you can use the following syntax for running multiple tests in a class
mvn -Dtest=com.MyComp.Selenium.SelTests.SomeTests1#XTest+#YTest...
Note the + symbol. Here is the documentation

Run single test from a JUnit class using command-line

I am trying to find an approach that will allow me to run a single test from a JUnit class using only command-line and java.
I can run the whole set of tests from the class using the following:
java -cp .... org.junit.runner.JUnitCore org.package.classname
What I really want to do is something like this:
java -cp .... org.junit.runner.JUnitCore org.package.classname.method
or:
java -cp .... org.junit.runner.JUnitCore org.package.classname#method
I noticed that there might be ways to do this using JUnit annotations, but I would prefer to not modify the source of my test classes by hand (attempting to automate this). I did also see that Maven might have a way to do this, but if possible I would like to avoid depending on Maven.
So I am wondering if there is any way to do this?
Key points I'm looking for:
Ability to run a single test from a JUnit test class
Command Line (using JUnit)
Avoid modifying the test source
Avoid using additional tools
You can make a custom, barebones JUnit runner fairly easily. Here's one that will run a single test method in the form com.package.TestClass#methodName:
import org.junit.runner.JUnitCore;
import org.junit.runner.Request;
import org.junit.runner.Result;
public class SingleJUnitTestRunner {
public static void main(String... args) throws ClassNotFoundException {
String[] classAndMethod = args[0].split("#");
Request request = Request.method(Class.forName(classAndMethod[0]),
classAndMethod[1]);
Result result = new JUnitCore().run(request);
System.exit(result.wasSuccessful() ? 0 : 1);
}
}
You can invoke it like this:
> java -cp path/to/testclasses:path/to/junit-4.8.2.jar SingleJUnitTestRunner
com.mycompany.product.MyTest#testB
After a quick look in the JUnit source I came to the same conclusion as you that JUnit does not support this natively. This has never been a problem for me since IDEs all have custom JUnit integrations that allow you to run the test method under the cursor, among other actions. I have never run JUnit tests from the command line directly; I have always let either the IDE or build tool (Ant, Maven) take care of it. Especially since the default CLI entry point (JUnitCore) doesn't produce any result output other than a non-zero exit code on test failure(s).
NOTE:
for JUnit version >= 4.9 you need hamcrest library in classpath
I use Maven to build my project, and use SureFire maven plugin to run junit tests.
Provided you have this setup, then you could do:
mvn -Dtest=GreatTestClass#testMethod test
In this example, we just run a test method named "testMethod" within Class "GreatTestClass".
For more details, check out http://maven.apache.org/surefire/maven-surefire-plugin/examples/single-test.html
The following command works fine.
mvn -Dtest=SqsConsumerTest -DfailIfNoTests=false test
We used IntelliJ, and spent quite a bit of time trying to figure it out too.
Basically, it involves 2 steps:
Step 1: Compile the Test Class
% javac -cp .:"/Applications/IntelliJ IDEA 13 CE.app/Contents/lib/*" SetTest.java
Step 2: Run the Test
% java -cp .:"/Applications/IntelliJ IDEA 13 CE.app/Contents/lib/*" org.junit.runner.JUnitCore SetTest

Categories