Question

I have some fairly long tests in my buildSrc build which I sometimes want to skip during development, to see the effect that template changes have. It's a report generator and sometimes I just want to tweak the templates without having to update all the tests; then later I will update them when I'm happy with the output.

I was able to implement skipping tests within the buildSrc build.gradle with the following configuration:

test {
    // skip all tests if gradle run with -Pnotests
    try { if (project.ext.notests != null)
        println "skipping tests"
        exclude '**'
    } catch (MissingPropertyException e) {}
}

(and if you have a better way to detect an optional parameter I'd love to see it)

However, this doesn't work from the main gradle project. I have tried everything I can think of to try to pass the parameter through configuration, short of setting an environment variable, but I doubt that would work either.

In general I find I can't configure buildSrc whatsoever from the main project. I can't find any way to get at variables, eg project.ext.foo or ext.foo, nor a way to configure the buildSrc 'project' as there is no section in main build.gradle. It's like it exists on an island of its own. I've tried:

  • project.ext.notests = true
  • test {...} in main
  • allprojects { test {...} }
  • subprojects { test {...} }
  • dependencies { test {...} }
  • buildscript { test {...} }

Is there a way around this, or do I have to refactor my whole project to move the reporting code elsewhere and maybe then I can configure it and skip tests?

Thanks in advance.

Was it helpful?

Solution

buildSrc is a separate build (not project), and I'm not aware of a way to influence its execution when triggering the main build. (Note that building buildSrc is a prerequisite for evaluating the main build's build scripts.) Some solutions I can think of:

  • Unconditionally disable buildSrc tests for as long as required (e.g. by setting test.disabled = true in buildSrc/build.gradle)
  • Factor out buildSrc into a separate build
  • Change tests so that they are insensitive to template changes
  • Move tests that are sensitive to template changes into a separate Test task that's only triggered manually

PS: The easiest way to skip a build's tests from the command line is -x test. The existence of a project property foo can be detected with project.hasProperty("foo").

OTHER TIPS

I tried your idea of an environment variable and it does, in fact, work:

def skipTests = Boolean.parseBoolean(System.env['BUILDSRC_SKIP_TESTS'])

test {
  enabled = !skipTests
  // ... other settings here ...
}

(tested this in Gradle 4.9.)

One might also use such a variable to disable other checks, e.g.:

tasks.findAll { it.name.startsWith('findbugs') || it.name.startsWith('checkstyle') }
  .each { it.enabled = !skipTests }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top