我使用的是scala_specs2_junit_test测试规则,我希望通过测试--一个系统属性(您通常可以使用-DmyProp=myValue进行的操作)。
我发现这个线程听起来是相关的,但是它谈到了java (可以访问命令行参数的原生规则)和run (vs test)。
这个是可能的吗?
发布于 2017-06-16 13:02:42
您可以使用arg,但是测试规则本身必须解释这些标志。
scala_specs2_junit_test宏包装了一个scala_junit_test规则。作为测试规则,Bazel必须编写ctx.outputs.executable (参见规则()#测试),这是Bazel在bazel test //my:test上执行的,并将--test_arg标志传递给它。
我没有在Scala规则中找到任何解析--jvm_flags标志的逻辑,因此似乎需要对它们进行更新以处理这些问题。
演示
/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,
)/BUILD:
load("//:my_test.bzl", "my_test")
my_test(
name = "my_test",
)输出:
$ 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编辑:将注释添加到my_test.bzl,以突出标记解析将在何处进行
发布于 2017-06-15 16:03:25
您可以使用System.setProperty(名称、值)编程地在Java中设置系统属性。
在单元测试中这样做的缺点是,属性将在测试之后保持设置。确保在测试后将值设置为null。
https://stackoverflow.com/questions/44571338
复制相似问题