16

Provided the test classes and JUnit are both on the classpath, one can run JUnit tests from the command line as follows:

java org.junit.runner.JUnitCore TestClass1 TestClass2

Now, is there a way to run all tests in a package (and sub-packages) as well?

I'm looking for something like

java org.junit.runner.JUnitCore com.example.tests.testsIWantToRun.*

Is there an easy way of doing that (that doesn't involve maven or ant)?

Christian
  • 6,070
  • 11
  • 53
  • 103
  • I want to run all tests in a package and its sub-package on Continuous Integration servers, and I don't know what packages and tests adopters will choose to set up. But I can't run all of them, because tests in a certain package are not supposed to be kicked off on that box. Anything that requires someone manually creating a list of tests is thus not ideal (unless such a list can consist entirely of exclusions). – Christian Jul 01 '14 at 14:31
  • possible duplicate of [How do I Dynamically create a Test Suite in JUnit 4?](http://stackoverflow.com/questions/3257080/how-do-i-dynamically-create-a-test-suite-in-junit-4) – Christian Jul 10 '14 at 14:03
  • I found answers for my own question after I got the initial response; they are linked from my answer below. I've also just started a vote to close this as a duplicate. – Christian Jul 10 '14 at 14:05

3 Answers3

9

Junit lets you define suites of tests. Each suite defines a collection of tests, and running the suite causes all of the tests to be run. What I do is to define a suite for each package, listing the test classes for that package along with the suites for any sub-packages:

package com.foo.bar;

import org.junit.runner.RunWith;
import org.junit.runners.Suite;

import com.foo.bar.baz.Suite_baz;

@RunWith(Suite.class)
@Suite.SuiteClasses({
    ThisTest.class,
    ThatTest.class,
    TheOtherTest.class,
    Suite_baz.class,
})
public class Suite_bar {
}

This isn't completely effortless. You have to construct the suites and manually update them with new test classes. I suppose it wouldn't be hard to write a little java program to generate these automatically, if someone wanted to.

Kenster
  • 23,465
  • 21
  • 80
  • 106
3

I asked this question to be able to kick sets of a project's Cucumber tests on Jenkins off selectively, without really knowing what their RunTests classes would be called, what their CucumberOptions would contain, or where they would be located. I found a few helpful threads on StackOverflow in the meantime, which answer my question:

Using those, I can kick my Cucumber tests off individually as follows:

First, I used the maven assembly plugin to get the tests packaged in a jar: https://stackoverflow.com/a/574650/2018047

Then I copied the tests' dependencies to the target folder on Jenkins, as shown here: https://stackoverflow.com/a/23986765/2018047

We already have a flag that skips the execution of our tests when it's set, so I package my tests without running them: mvn clean install -DskipMyTestModule=true

And using the code from above and the invocation from below, I'll be able to make it all work...

java -Dcucumber.options="src/test/resources/features --tags @b --format pretty:STDOUT --format html:target/cucumber-b --format json:target/cucumber-b.json" -Dname=value -cp target/artifact-1.2.8-SNAPSHOT-tests.jar;target/test-classes/libs/junit-4.11.jar;target/test-classes/libs/* org.junit.runner.JUnitCore com.example.foo.bar.test.cucumber.RunTest

Hope this helps someone in the future. :)

Community
  • 1
  • 1
Christian
  • 6,070
  • 11
  • 53
  • 103
1

With JUnit 4 this is supported using an extension called cpsuite. All you need to do is add it to your test classpath (maven io.takari.junit:takari-cpsuite), create a dynamic test suite class:

package com.mycompany;
import org.junit.extensions.cpsuite.ClasspathSuite;
import org.junit.runner.RunWith;

@RunWith(ClasspathSuite.class)
@ClasspathSuite.IncludeJars(true)
public class RunAllTests {}

and run it:

java -cp ${CP} org.junit.runner.JUnitCore com.mycompany.RunAllTests

Your classpath ${CP} should include your test jar, junit, hamcrest and cpsuite.

Omri Spector
  • 2,431
  • 24
  • 22