Tell Eclipse to skip tests - java

Is there a way to tell only Eclipse to skip various individual junit tests (but still run the rest of them)? I am familiar with the #Ignore annotation, but that is not what I am looking for because that will cause the tests to always be skipped.
I would like the tests to be skipped when ran by eclipse (Run As -> Junit Test) but ran in any other context, most likely during a maven build.
Any ideas?

So far the following solution has worked well. It is not a complete solution because it causes the tests to only be ran by maven when maven is invoked from the command line. For now though, it works.
When maven is invoked, the maven command line arguments are placed in the environment map (System.getenv()). When the tests are run through jUnit in eclipse, there is no entry in System.getenv() for the maven command line arguments. So, I am using jUnits Assume class to check that this property is not null for the tests that I want eclipse to skip.
Code:
Assume.assumeNotNull(System.getenv("MAVEN_CMD_LINE_ARGS"));
FYI, one downside of this solution is that eclipse marks the tests as passed instead of skipped.

Related

Skip single test with maven with command line options

I would like to skip a single test (e.g. com.example.MyTest) when building a project with Maven from the command line.
I'm aware of similar questions like this one, but they all require either modification of source code or pom.xml. I would like to do without modifications. How can I exclude a test using command line options only?
What I've tried so far after reading some documentation is
mvn clean install -Dtest="*,!com.example.MyTest"
but the test is still not skipped. I'm using surefire plugin version 2.19 and JUnit 4.11.
It turns out that (at least in Surefire 2.19), the test pattern doesn't work with fully qualified class names. So the right solution is
mvn clean install -Dtest="*,!MyTest"
i.e. without the package path.
In Surefire 2.19.1, it should be possible to use fully qualified names. In versions older than 2.19, neither seems to work.

How to rerun TestNG failed tests via Maven command

Basically the header says it all, imagine I ran a testsuite, now some of the test have failed and I want to rerun those tests. I know that there is testng-failed.xml file generated by surefire plugin but I don't know how to pass that file as a parameter to TestNG through Maven. This is what I tried but unfortunately none of these commands have worked (they run all the tests again).
mvn verify -DsuiteXmlFile=testng-failed.xml
mvn verify -DsuiteXmlFile=target/surefire-reports/testng-failed.xml
Assuming you are at the root of the project and it has a standard layout, you can run:
mvn -Dsurefire.suiteXmlFiles=target/surefire-reports/testng-failed.xml test
You should try the correct parameter which is based on the documentation
mvn -Dsurefire.suiteXmlFiles=testng-failed.xml
A little bit late but for all who also stumble upon this question, khmarbaise answer works if you add the appropriate maven lifecycle.
mvn -Dsurefire.suiteXmlFiles=<path to testng-failed.xml> test

Setting cucumber-jvm options in Maven from the command line

I am trying to set the "name" option for Cucumber to be able to run a specific feature or scenario.
I have entered this,
mvn test -DCucumber.Options--name="MyFeatureName"
but it just runs all the features and doesn't give an error.
Any ideas?
Here is a snippet from the Cucumber-JVM repo on how to run the java-helloworld example by passing cucumber options:
mvn test -Dcucumber.options="--format json-pretty --glue classpath:cucumber/examples/java/helloworld src/test/resources"
Keep in mind that it will override all the options in the #Cucumber.Options annotation you have on "RunCukesTest". I haven't got it to work for my own tests but maybe this will help.
So it looks like you need to give all the options needed to run cucumber, including the java class path and where the code is located using the "--glue" parameter.
Your tests are running in separate JVM, so you need to specify that system property in the test plugin configuration (i.e. surefire or failsafe plugin config in your pom.xml).

Why is my Mockito-based unit test unable to run in Eclipse?

I have a Maven-managed project that uses Mockito mocking in its unit tests. I can run all the tests within a Maven build, and they run without error (and pass!). However, if I right-click a single test function, and choose "Run As -> JUnit Test", I get an exception java.lang.NoSuchMethodError: org.mockito.Mockito.doAnswer(Lorg/mockito/stubbing/Answer;)Lorg/mockito/stubbing/Stubber;. Of course, the "missing" method is there if I look at the sources, and like I said the tests compile and run from the command line.
Best I can think of is if Eclipse is trying to "help" me by providing an outdated Mockito artifact (I'm using 1.8.5 in my Maven dependencies) for the JUnit plugin, akin to how the Maven plugin can stick you with an oddball version of the Maven runtime for certain tasks.
Is this the problem? Is it something else? Can I fix this?
ETA: Apparently this may relate to a known issue. There's a good chance that it stems from having multiple versions of Mockito in my classpath (thanks, Maven :-/ ). I seem to have my house in order -- Eclipse can run the tests now -- but unfortunately the bug has bitten my Hudson. I have to track down how to remove the old JAR from the classpath there as well.
Make sure the unit-test classpath has the correct mockito. You can check this from the run dialog. Btw, Eclipse does not ship with mockito, so perhaps you are having two versions of it. Take a look at your maven dependency graph and search for duplicates.
I had the similar problem and I found that I had both "mockito-all 1.8.x" and "mockito-core 1.9.5" in my classpath. I was supposed to use only 1.9 but somehow eclipse was putting 1.8 before 1.9.5 in the classpath. I removed 1.8.x and it worked ;)

Run all the junit tests that are in a given package in Netbeans?

We have a bunch junit tests in our current project. Essentially we are looking for a way to run all the test in a given package. Currently in Netbeans I see you can run all the tests or a single test, but no way to run a a sub-set of tests.
Is this built into Netbeans? Or is there another way we can do this?
In JUnit this is achieved through TestSuite. You can check for more information here (look at the code, not at the pictures).
At least in the Eclipse IDE there is a functionality that lets you add a new TestSuite, select which tests it is to include and then have the IDE generate it for you.
I haven't seen such thing in Netbeans, but you should check for any additional JUnit plugins.
Just set up junit test groups like documented e.g. on this question and then run the test group via your ant or maven build.

Categories

Resources