TestKit: How to turn off daemon?

On Travis my tests repeatedly fail because of org.gradle.launcher.daemon.client.DaemonDisappearedExceptions.
I tried using GradleRunner.withArguments('--no-daemon'), but that’s not allowed. How can I prevent TestKit from using daemon mode?

Example build: https://travis-ci.org/sherter/google-java-format-gradle-plugin/jobs/134162967
(It’s strange that only the oraclejdk8 build always fails…)
Related: Travis CI: org.gradle.launcher.daemon.client.DaemonDisappearedException: Gradle build daemon disappeared unexpectedly (it may have been killed or may have crashed)

You can use withDebug(true), which will cause TestKit to execute in “embedded” mode, executing in-process rather than using a daemon.

Thanks for the tip.
There is one problem though: When I turn debug mode on, I can’t access GradleRunner.build().output anymore (throws org.gradle.testkit.runner.UnsupportedFeatureException). Is this supposed to be like that? Then this workaround doesn’t help me very much…

You will need to use Gradle version >= 2.9 for testing your code to be able to inspect build output while running in debug mode. Please see the TestKit documentation for potential limitations.

Unfortunately the debug mode is not an option then. I want to test against Gradle versions < 2.9.

See Travis CI: org.gradle.launcher.daemon.client.DaemonDisappearedException: Gradle build daemon disappeared unexpectedly (it may have been killed or may have crashed) for what I did to make Gradle work on Travis

I would have expected that the GradleRunner respects an environment setting like GRADLE_OPTS="-Dorg.gradle.daemon=false", but apparently that’s not the case :frowning:

I wanted to turn off the Gradle daemon, because it keeps running out of Metaspace when running multiple tests in one class (probably a leak in one of the plugins I use). I found a hacky workaround to make sure each test runs in it’s own Daemon. Not exactly what OP was looking for, but might help someone.

	static int counter = 0
	@Before void setUp() {
		File props = new File(gradleRunner.getProjectDir, "gradle.properties")
		// roughly 256M
		writeText(props, "org.gradle.jvmargs=-Xmx" + (256 * 1024 * 1024 + counter++))
	}

This is based on what makes Gradle create a new daemon: The Gradle Daemon