简体   繁体   English

有没有办法判断是否正在从Ant运行JUnit测试?

[英]Is there a way to tell whether a JUnit test is being run from Ant?

I want to recognize at runtime whether my JUnit tests are being run from ant or not. 我想在运行时识别我的JUnit测试是否从ant运行。 The reason for this is, if it's being run from ant , I want to log information to a log file, else I just want to write to standard out. 原因是,如果它是从ant运行的,我想将信息记录到日志文件中,否则,我只想写标准输出。

Ant's built in JUnit Task has a showoutput attribute which will send any output produced from the tests to ant's logging system. Ant的内置JUnit Task具有showoutput属性,该属性会将测试产生的所有输出发送到ant的日志系统。

See also this SO question ant junit task does not report detail about reporting options. 另请参见此 SO问题ant junit任务不报告有关报告选项的详细信息。

One way to accomplish this would be to include a property in the junit task in the Ant script: 一种实现方法是在Ant脚本的junit任务中包含一个属性:

<junit fork="yes">
  <jvmarg value="-Druntime.agent=ANT"/>
  ...
</junit>

Then, in your JUnit test harness inquire about the existence and/or property for that property: 然后,在您的JUnit测试工具中,查询该属性的存在和/或属性:

String agent = System.getProperty("runtime.agent");
if ("ANT".equals(agent)) {
    // set up file logging
} else {
    // set up STDOUT logging
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM