2015-10-19 86 views
0

當測試向System.out或System.err寫入內容時,我希望CI構建失敗。最好我想列出一些產生不必要輸出的測試。寫入System.out或.err失敗測試

我試圖用內行保命插件與添加的監聽器,但也不只是伎倆的一部分,你可以從這個問題看JUnit RunListener is removed on fail()

是不是有人嘗試這樣的事情以及是否有其他方式來實現CI構建一個更清潔的控制檯?

回答

1

您可以使用Checkstyle插件,以便您使用不需要的代碼進行測試失敗,並將在surfire報告中列爲失敗。

+0

我們的項目中有部分使用System.out.print,這沒關係。這只是爲了保持'mvn測試'輸出清潔。 – globalworming

2

你可能會看看這個Maven插件https://github.com/policeman-tools/forbidden-apis,它檢查禁止的API調用。你可以定義在代碼中應該禁止的唯一呼叫。

編輯簡單示例顯示排除不允許的API調用。在這個例子中,除了一個測試類以外,測試類中不允許調用System.out.println

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使用。

[ERROR] Forbidden method invocation: java.io.PrintStream#println(java.lang.String) 
[ERROR] in sub.optimal.NotPermittedSystemOutTest (NotPermittedSystemOutTest.java:9) 
+0

我是全球變暖的同事。我們的問題是,有一些有效的情況下調用'System.out' /'.err'是一個有效的情況。 – user3001

+0

@ user3001請檢查添加的示例如何從支票中排除班級。 – SubOptimal

1

可以檢查測試是否通過使用圖書館System Rules

public class YourTest { 
    @Rule 
    public final SystemOutRule systemOutRule = new SystemOutRule().enableLog(); 

    @After 
    public void assertNoTextHasBeenWrittenToSystemOut() { 
    assertEquals("", systemOutRule.getLog()) 
    } 

    ... 
} 

有相同的可System.err使用SystemErrRule來完成寫入System.out

+0

需要您的幫助[**鏈接**](https://stackoverflow.com/questions/46712632/systemoutrule-in-junit-test-didnt-capture-standard-output):) – Woland