2016-06-21 171 views
0

我正在爲我的android應用程序投擲jenkins運行我的junit測試。如果測試失敗,我只能看到堆棧跟蹤,這有時無法幫助我理解爲什麼我的測試失敗。我在代碼中添加了一些日誌,所以我可以在我的本地機器上運行它們時調試我的測試,但我希望能夠在Jenkins控制檯輸出中查看這些日誌,以便我可以瞭解當我的測試失敗時出了什麼問題。如何在jenkins的控制檯日誌中看到我的測試日誌

+0

當您在本地環境中運行測試時,您是否看到日誌?(您的IDE控制檯或終端) –

+0

是的。我可以在Android工作室的Android Monitor控制檯上看到它。 – Benjo

+0

很難說出什麼問題,但作爲一項解決方案,您可以將日誌打印到日誌文件中,然後只需「cat/type」該文件。 –

回答

0

這是一個老問題,但我找到了解決方案。 我將Log類包裝成自定義類(Logger)。

然後我用像登錄方法:

public static void d(String tag, String message) { 
    log(LOG.DEBUG, tag, message); 
} 

然後登錄的樣子:

private static void log(LOG logLevel, Object message) { 
     LOG_LINES.add(new LogLine(logLevel, tag, content)); 

     switch (logLevel) { 
      case ERROR: 
       Log.e(tag, content); 
       break; 
      case WARNING: 
       Log.w(tag, content); 
       break; 
      case INFO: 
       Log.i(tag, content); 
       break; 
      case DEBUG: 
       Log.d(tag, content); 
       break; 
      case VERBOSE: 
       Log.v(tag, content); 
       break; 
     } 
    } 

LOG_LINES一個的CopyOnWriteArrayList 和LogLine日誌數據的持有人。

最後,通過使用自定義測試Runner,日誌顯示在Jenkins(在HTML報告中)中。只需將註釋@RunWith(MyLoggingTestRunner.class)與添加到測試類:

public class MyLoggingTestRunner extends BlockJUnit4ClassRunner { 

    public MyLoggingTestRunner(Class<?> klass) throws InitializationError { 
     super(klass); 
    } 


    @Override 
    protected void runChild(final FrameworkMethod method, RunNotifier notifier) { 
     //override to override errors with logs 
     Description description = describeChild(method); 
     if (method.getAnnotation(Ignore.class) != null) { 
      notifier.fireTestIgnored(description); 
     } else { 
      run(methodBlock(method), description, notifier); 
     } 
    } 

    protected void run(Statement statement, Description description, RunNotifier notifier) { 
     EachTestNotifier eachNotifier = new EachTestNotifier(notifier, description); 
     eachNotifier.fireTestStarted(); 
     try { 
      statement.evaluate(); 
     } catch (AssumptionViolatedException e) { 
      eachNotifier.addFailedAssumption(e); 
     } catch (Throwable e) { 
      String throwableMessage = e.getMessage(); 
      String message = "\nLast logs :\n" + logsToString() + "\n\nStackTrace :\n" + e.getClass() + (TextUtils.isEmpty(throwableMessage) ? "" : ":" + throwableMessage); 

      try { 
       Field detailMessage = Throwable.class.getDeclaredField("detailMessage"); 
       detailMessage.setAccessible(true); 
       detailMessage.set(e, message); 
      } catch (Exception e1) { 
      } 
      eachNotifier.addFailure(e); 
     } finally { 
      eachNotifier.fireTestFinished(); 
     } 
    } 


    private static String logsToString() { 
     List<LogLine> logs = Logger.getLogs(); 
     StringBuilder logBuilder = new StringBuilder(); 
     String sep = ""; 
     for (LogLine logLine : logs) { 
      logBuilder.append(sep).append(logLine.toString()); 
      sep = "\n"; 
     } 
     return logBuilder.toString(); 
    } 
} 

我希望它的作品,因爲我適應我的代碼試圖解釋它的簡單方法。

相關問題