Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding a testset source-set to the compile path of another testset #53

Open
MartinAhrer opened this issue Oct 12, 2018 · 8 comments
Open

Comments

@MartinAhrer
Copy link

MartinAhrer commented Oct 12, 2018

I have a scenario where I want to re-use code from a testset in another testset. The setup is as follows

plugins {
    id 'groovy'
    id 'idea'
    id 'org.unbroken-dome.test-sets' version '1.5.1'
}

group 'testset'
version '1.0-SNAPSHOT'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

testSets {
    sharedTest
    integrationTest
}

dependencies {
    compile 'org.codehaus.groovy:groovy-all:2.3.11'
    testCompile group: 'junit', name: 'junit', version: '4.12'

    integrationTestCompile 'org.spockframework:spock-core:1.1-groovy-2.4'

    integrationTestCompile sourceSets.sharedTest.output
}

This compiles and runs fine when running the gradle CLI to build and test. However the IDEA integration seems to fail as the code does not compile in the IDE. Running this with JDK 8 + IDEA 2018.2.4.
See https://github.com/MartinAhrer/gradle-testset-plugin-ideabug.

It looks like this has worked with earlier versions of IDEA + the testset plugin.

@tkrullmann
Copy link
Member

Hi,
I've got your sample project to compile and run the test with two small modifications:

  • make sure that the groovy-all dependency version matches the one used by Spock, i.e. either upgrade groovy-all to 2.4.x or downgrade Spock to 1.1-groovy-2.3
  • remove the idea plugin (not sure why this fixes the problem, but I'll look into it). The idea plugin built into Gradle was used by early versions of the IDEA Gradle support, right now I think it's only used to generate IDEA project files initially, IDEA now seems to use a different mechanism under the hood (which is neither documented nor open-sourced). That's why unfortunately the possibilities for a plugin to influence IDEA's behavior are now very limited.

@MartinAhrer
Copy link
Author

MartinAhrer commented Oct 26, 2018

Agree, removing the idea plugin makes the project work. However, I think the idea plugin still has relevance. It allows to customise the workspace (e.g. by manipulating the XML based configuration of IDEA projects).
For example in my project where I originally hit the problem I'm also using the https://github.com/tbroyer/gradle-apt-plugin. See https://github.com/SoftwareCraftsmen/continuousdelivery/blob/master/build.gradle

It has a plugin for managing apt configuration in IDEA. As soon as I add the plugin (id "net.ltgt.apt-idea" version "0.19"), the same symptom appears (as the apt plugin pulls in the idea plugin again).

@dje1990
Copy link

dje1990 commented Nov 26, 2018

Suggestion doesn't work, ignore.

Hi MartinAhrer tkrullmann

At a guess, could you use something similar to the below? (untested)

sourceSets {
integrationTest {
compileClasspath += sourceSets.sharedTest.runtimeClasspath
}
}

@MartinAhrer
Copy link
Author

After investigating a bit further: Could it be that this is a bug in IDEA? IDEA is able to run the code referencing the shared test code from the integration test. But the editor is showing the referenced class from sharedTest as unavailable (red in my color-scheme).

I have also tried replacing my setup by your concept of TestLibrary.

testSets {
sharedTest(TestLibrary)
integrationTest { imports sharedTest }
}

The effect is the same!

@tkrullmann
Copy link
Member

Strange, I'm experiencing the same.

I also noticed that the problem goes away when I mark testCommon/java as "sources root" instead of "test sources root".

So maybe the IDEA editor has a problem with one test source importing another (even though the runner seems to take it).

The only "fix" I can imagine is to exempt test libraries from being marked as "test sources" by the plugin.

@MartinAhrer
Copy link
Author

I have upgraded to 2.1.1 (with Gradle 5.1.1) and added a test library and trying to use it with a out-of-the-box source set test provided by the java/groovy plugin.

testSets {
    createLibrary ('sharedTest')
    unitTest { imports sharedTest }
    integrationTest { imports sharedTest }
}

The IDE is showing the same symptom but also now the build fails the compileTestGroovy task when running ./gradlew test.

This is the commit showing the bug: MartinAhrer/gradle-testset-plugin-ideabug@cb5f1ad

@gokhanoner
Copy link

gokhanoner commented Jul 14, 2019

Using 2.1.1 with Gradle 5.5.1

trying to add a dependency from a module to integration tests:

dependencies {
    implementation project(":core")
    implementation project(":xxx")
    
    e2eTestImplementation project(":xx").sourceSets.test.output
}

testSets {
    e2eTest
}

This also fails with

Could not find method e2eTestImplementation() for arguments [test classes] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.

Both IDEA & CLI compile fails with the same error

If I remove the testSet definition & try same with like below, it works:

testImplementation project(":xx").sourceSets.test.output

@MartinAhrer
Copy link
Author

After upgrading to version 2.2.1 of the plugin and Gradle 6.0.1 this issues still exists (with current IDEA 2019.1.3).
But a workaround is to use the new testFixtures feature introduced with Gradle 5.6.x. The IDE will then properly recognise the classes from the fixture.

testSets {
    integrationTest {
    }
}
sourceSets {
    testFixtures {
        java {
            srcDirs = ['src/sharedTest/java']
        }
        groovy {
            srcDirs = ['src/sharedTest/groovy']
        }
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants