Defining a test module in a multi-module project

I’m having trouble defining a test sources only module in my multi-module project.
Say that I have the following structure:

gradle-example:
  app:
    src/main/kotlin (regular production code)
    src/test/kotlin <- this is supposed to use 'com.example.SomeTestUtil'
  testlib:
    src/test/kotlin/com.example.SomeTestUtil

I would like to have various test utilities in the testlib module under src/test, and to be able to use them in the app module, but only in src/test. I know i can move the SomeTestUtil from src/test to src/main and just use it in the tests, but I would like to avoid having these classes exposed to production code.

Is it possible to do that ?
Current app module build.gradle.kts has:

dependencies {
  testImplementation(project("testlib"))
}

But it does not work and I cannot import the SomeTestUtil to app src/test

You can use the Test Fixtures plugin to do this. The advantage of the test fixture plugin is that you can apply it to each project where you want to expose test utils to dependencies.

1 Like