Home > Software engineering >  How do I get a Gradle property in a Scala test?
How do I get a Gradle property in a Scala test?

Time:10-14

I have this in my ~/.gradle/gradle.properties:

com.my.path=/path/to/stuff

I have this in a Scala test:

class MyTest extends FunSuite {
    val path = sys.props.getOrElse("com.my.path", throw new RuntimeException("nope!"))

The test always throws an exception because com.my.path isn't in sys.props.

I tried prefixing the property in ~/.gradle/gradle.properties with systemProp. as suggested here, but it made no difference.

I also tried adding this to my build.gradle:

test {
    systemProperties = System.properties
}

I forget where I read that, but it causes the test runner to fail with a "No tests found" error.

I'm running the test with IntelliJ FWIW.

CodePudding user response:

Any required properties must be passed to the test explicitly. see https://docs.gradle.org/current/dsl/org.gradle.api.tasks.testing.Test.html

you pass gradle properties

test {
// set a system property for the test JVM(s)
  systemProperty 'some.prop', some.prop
}
  • Related