写入System.out或.err时失败测试 [英] Fail tests on write to System.out or .err
问题描述
当测试向System.out或System.err写入内容时,我希望我的CI构建失败。优选地,我想有一个测试列表产生不必要的输出。
I'd like my CI build to fail when the tests write something to System.out or System.err. Preferably I would like to have a list of tests which produced unwanted output.
我试图使用mavens surefire插件添加一个监听器,但这只是一部分的技巧您可以从问题中看到JUnit RunListener在fail()上删除
I tried to use mavens surefire plugin with an added listener, but that does only part of the trick as you can see from the question JUnit RunListener is removed on fail()
有没有其他人尝试这样的东西,有没有其他方法来实现一个更清洁的控制台CI构建?
Did someone else try something like this and are there other ways to achieve a cleaner console on CI builds?
推荐答案
您可能看看这个Maven插件 https://github.com/policeman-tools/forbidden-apis
禁止的API调用。您可以在代码中定义应禁止的呼叫。
You might have a look at this Maven plugin https://github.com/policeman-tools/forbidden-apis
which check for forbidden API calls. You can define wich calls should be forbidden in the code.
编辑显示不允许的api呼叫的简单示例。在示例中,除了一个测试类之外,不允许在测试类中调用 System.out.println
。
edit Simple example to show exclusion of not permitted api calls. In the example the call of System.out.println
should not be permitted in test classes with the exception of one test class.
pom.xml
<build>
<plugins>
<plugin>
<groupId>de.thetaphi</groupId>
<artifactId>forbiddenapis</artifactId>
<version>2.0</version>
<executions>
<execution>
<goals>
<goal>check</goal>
<goal>testCheck</goal>
</goals>
</execution>
</executions>
<configuration>
<signaturesFiles>
<signaturesFile>${project.build.testOutputDirectory}/signatures.txt</signaturesFile>
</signaturesFiles>
<excludes>
<!-- specify the classes which should be excluded -->
<exclude>**/Permitted*</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
符号文件 src\test\resources\signatures.txt
java.io.PrintStream#println(java.lang.String)
测试类
// the one which doesn't use System.out
package sub.optimal;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.junit.Assert;
import org.junit.Test;
public class NoSystemOutTest {
@Test
public void dummy() {
Logger logger = Logger.getGlobal();
logger.log(Level.INFO, "some info");
Assert.assertTrue(true);
}
}
。
// the one in which the use of System.out should be permitted
package sub.optimal;
import org.junit.Assert;
import org.junit.Test;
public class PermittedSystemOutTest {
@Test
public void dummy() {
System.out.println("permitted usage");
Assert.assertTrue(true);
}
}
。
// the one in which the use of System.out is not permitted
package sub.optimal;
import org.junit.Assert;
import org.junit.Test;
public class NotPermittedSystemOutTest {
@Test
public void dummy() {
System.out.println("not permitted usage");
Assert.assertTrue(true);
}
}
使用 mvn test-compile forbiddenapis:testCheck
只会报告 NotPermittedSystemOutTest
中禁止的api用法。
Running the check with mvn test-compile forbiddenapis:testCheck
will only report the forbidden api usage in NotPermittedSystemOutTest
.
[ERROR] Forbidden method invocation: java.io.PrintStream#println(java.lang.String)
[ERROR] in sub.optimal.NotPermittedSystemOutTest (NotPermittedSystemOutTest.java:9)
这篇关于写入System.out或.err时失败测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!