Selecting specific tests to run in gradle - junit

I'm trying to fix our messy failing test runs, and, unfortunately, I'm very new to gradle. We currently have testng, junit, and I'd like to add some spock tests to the mix as well. I'm not quite sure how gradle determines which tests to run when I type "gradle test". How can I prevent the testng &/or junit tests from running? How can I get gradle to start running my spock tests?

By default, the test task runs all JUnit tests it can find, which includes any Spock tests. To make it run TestNG tests instead, configure the task as follows:
test {
useTestNG()
}
If you have both JUnit and TestNG tests, you need two test tasks, one for each test framework.
To run a subset of tests, use the -Dtest.single system property. For more information, see the corresponding section in the Gradle User Guide.

You may provide using the command line:
$> gradle test --tests org.somewhere.MyTestClass
Or even
$> gradle test --tests org.somewhere.MyTestClass.my_test_case

$> gradle test -Dtest.single=YourTestClass

Related

Run integration test suite with maven

I'm trying to find a way to run a single integration test suit with maven.
I have a suite defined like that:
#RunWith(Suite.class)
#Suite.SuiteClasses({
ITest1.class,
ITest2.class,
})
public class TestSuitIT {}
I try to tun it with the command
mvn -Dskip.unit.tests -Dit.test=TestSuitIT integration-test
But it doesn't run the suit. I also tried with the full package.
It does work if I run a single test, but not with the suite. Is it even supported?

2 variants for JUnit execution: TestRunner & JUnitCore

There seem to be two approaches for invoking JUnit tests from the OS command shell:
java junit.textui.TestRunner <class-name>
and
java org.junit.runner.JUnitCore <class-name>
When do we use one versus the other?
Also, are there other ways to invoke Junit tests from the OS command shell?
JUnitCore is an entry point of JUnit - so if you want to run a test programmatically or of from some non-java script, I think, its the way to go for JUnit 4.
TestRunner is something a very old junit 3.x
Notice, that nowadays JUnit 5 is the latest available major release and it has yet another way to run the tests.
The question about different ways of running the tests from command line has been already answered Here so I can't add much to this.
However, I can comment on:
Also, are there other ways to invoke Junit tests from the OS command shell?
Nowadays in regular projects people do not run tests like this, instead they use one of build tools (Maven, Gradle for example) that among other things take care of tests.
So for example if you use maven, you can run mvn test and it will compile everything you need, including source code of tests, will take care about all test dependencies and will run all the tests with the help of build-in surefire plugin.
If you don't want to compile anything (assuming that all the code has been already compiled and all is set, you can use mvn surefire:test)
These build tools are also integrated with CI tools (like Jenkins, etc.) So this is considered to be a solved problem.
So unless you're doing something really different (like writing the IDE UI that should run test selected by user on demand or something) there is no really need to run tests with the options you've mentioned.

Forcing Eclipse to run JUnit tests in the same order that Maven runs them

I’m using Maven 3.2.3, SureFire 2.17, JUnit 4.11 and Eclipse Juno on Mac 10.9.5. I notice that when I run my JUnit tests via the command line
mvn test -Dtest=MyTest
the individual tests within the file “MyTest.java” run in a different order than when I run them in Eclipse (by right clicking the class name and selecting “Run As -> JUnit Test”). How do I get Eclipse to run the tests in the same order in which they are run on the command line?
Thanks, - Dave
The order of JUnit test runs are not guaranteed by design, as is mentioned in the JUnit FAQ page.This is done to promote the concept of test Independence, this will make sure that the tests will test their cases clearly and independently, and also ease maintainability.
This means that when you are running the tests in Eclipse, the orders are not guaranteed and keep changing. It's explained in "Can I change JUnit execution order?" on how you can possibly fix the order, even though its not a good practice.

Intellij 13.0.2 JUnit test do not run in Debug

Intellij 13.0.2 JUnit test do not run in Debug
I have created several JUnit tests that run fine in Intellij, but when I try to Debug the same test, the files build and the test is not executed. I look in the event log and all it says is All Files are up to date. It does run NMake, but nothing more.
I don't know if this is related, but the View | Tool WIndows | Debug is disabled.
What do I need to do to Debug JUnit tests?
Thanks
Select Run --> Run Configurations... from the IntelliJ window
Select JUnit on the left side.
On the right side there is a "Before Launch:" section for configuring the behavior before launching JUnit tests.
See also the help page for this.

Jacoco Sling Junit Integration-Test Execution

One of our test classes extends RemoteBaseTest but Jacoco ignores it completely.
How can I make Jacoco work with Sling Integration Testing?
For Unit Tests everything works as expected.
We are using Adobe CQ 5.6.1.
I see that this issue has been resolved: sling-issue-tracker-2810
but unsure how to implement it - is it even included in the latest CQ-Version yet?
If not how do I manually add it?
I don't know what RemoteBaseTest is but I assume you are running a JUnit "proxy" test which talks to the Sling JUnit server-side tests subsystem and causes the actual tests to run on your CQ server.
If that's correct, the actual test code doesn't run in the client JVM that's running RemoteBaseTest, it runs in the server JVM that's running CQ. So it's on the server JVM that you need to setup Jacoco to collect coverage data.
If you're running some tests on the client JVM (like common JUnit tests) and some on the server JVM via the Sling testing tools, Jacoco has functions to merge coverage data coming from different JVMs. We have this as a work in progress in https://issues.apache.org/jira/browse/SLING-1803 , which is not fully integrated in Sling yet but should be adaptable to any version of CQ.