score:3

Accepted answer

You can use --test_arg, but the test rule itself must interpret such flags.

The scala_specs2_junit_test macro wraps a scala_junit_test rule. As a test rule, the latter must write ctx.outputs.executable (see rule()#test) which is what Bazel executes upon bazel test //my:test and passes the --test_arg flags to.

I didn't find any logic in the Scala rules that would parse --jvm_flags flags, so it seems they need to be updated to handle that.


Demo

<workspace>/my_test.bzl:

def _my_test_impl(ctx):
  ctx.file_action(
    ctx.outputs.executable,
    "\n".join([
        "#!/bin/bash",
        "echo \"DEBUG: Hello from $(basename $0)\"",
        "echo \"DEBUG: Argv=($@)\"",
        # Flag parsing (e.g. --jvm_flags) would have to happen here
    ]),
    True)

my_test = rule(
    implementation = _my_test_impl,
    test = True,
)

<workspace>/BUILD:

load("//:my_test.bzl", "my_test")

my_test(
    name = "my_test",
)

Output:

  $ bazel test //:my_test --test_output=streamed -t- --test_arg=--foo=bar --test_arg=--jvm_flags=blah
(...)
INFO: (14:45:05.379) Found 1 test target...
DEBUG: Hello from my_test
DEBUG: Argv=(--foo=bar --jvm_flags=blah)
Target //:my_test up-to-date:
  bazel-bin/my_test
INFO: (14:45:05.501) Elapsed time: 0.393s, Critical Path: 0.11s
INFO: (14:45:05.501) Build completed successfully, 3 total actions
//:my_test                                                               PASSED in 0.1s

EDIT: added comment to my_test.bzl to highlight where the flag parsing would happen

score:-5

You can always set a system property in Java programatically using System.setProperty(name, value).

The drawback to doing this in a unit test is that the property will stay set after the test. Make sure you set the value to null after the test.


Related Query

More Query from same tag