2

Question: Is it possible to run JUnit tests located in one Gradle project, with dependencies from another project?

Background

Consider this multi-project setup:

product_1_configuration_with_tests/
feature_a_with_tests/
feature_b_with_tests/
common_integration_tests/
  • For the sake of simplicity, the product project depends on the other three projects. No other dependencies exist.
  • common_integration_tests contains integration tests common for all products, that must be run in the context (classpath/dependencies) of the product.
  • common_integration_tests can not have any dependencies to any other projects.
  • The behavior of the common integration tests differ depending on what is on the runtime classpath.

How can such common integration tests be run?

Attempts

Ideally I'd like the test task to be defined in the product project. I have tried including the integration test in the suite:

// In product_1_configuration_with_tests/build.gradle
task integrationTest(type: Test) {
  include '**/MyIntegrationTest.class'
}

Since the test task does not find the test which resides in another module, no tests are executed.

David Pärsson
  • 6,038
  • 2
  • 37
  • 52

1 Answers1

0

Either you declare the integration test sources as a separate source set of the product_1_configuration_with_tests project (giving up on the common_integration_tests project), or you declare a dependency from common_integration_tests on product_1_configuration_with_tests. In a greenfield project, the former would be the preferable approach, but the latter will work as well. Other solutions are needlessly complicated.

common_integration_tests can not have any dependencies to any other projects.

Why?

Peter Niederwieser
  • 121,412
  • 21
  • 324
  • 259
  • It can more specifically not have any dependencies on any product projects. This is because we want to be able to reuse the integration tests in many products, and we can't have dependencies between our products. – David Pärsson Mar 25 '13 at 15:42
  • That changes the game. Are the products part of the same Gradle build? – Peter Niederwieser Mar 25 '13 at 16:57
  • Nope, each product is built separately (but all are built using Gradle). – David Pärsson Mar 26 '13 at 12:51
  • In that case, you'd publish the tests as a Jar, and pull them into the products as `testCompile` dependencies. Since a `Test` task also requires a `testClassesDir`, you'd probably have to add a `Copy` task that unpacks the test Jar into some directory and configure the `Test` task accordingly. You might find further details at http://forums.gradle.org (the question has been asked before). – Peter Niederwieser Mar 26 '13 at 13:17