簡體   English   中英

使用JUnit測試的Log4J

[英]Log4J with JUnit Tests

我想在我的Selenium Java測試中添加日志記錄。 我已經實現了log4jFramework,它可以很好地將日志放在控制台或文件中。

我正在使用JUnit測試框架,我想在日志文件的文件名中包含測試名稱和日期/時間,而不是使用標准約定,如果文件已經存在,這似乎是在文件前加上+1 。

這是我的log4j.properties文件......

log4j.appender.rollingFile=org.apache.log4j.RollingFileAppender
log4j.appender.rollingFile.File=~/Desktop/Selenium/AutomationLogs/automationLog
log4j.appender.rollingFile.MaxFileSize=2MB
log4j.appender.rollingFile.MaxBackupIndex=2
log4j.appender.rollingFile.layout = org.apache.log4j.PatternLayout
log4j.appender.rollingFile.layout.ConversionPattern=%d{dd MMM yyyy HH:mm:ss} %p %t %c - %m%n

我在網上找到的只是與每日滾動文件相關。 我想每次運行單元測試時都為日志生成一個新文件

如果我正確理解您的需求,可以通過結合以下內容來實現:

  • 使用JUnit方法規則,它將為您提供當前方法的名稱
  • 在運行時創建Log4j appender,並使用剛剛獲得的方法名稱對其進行配置

具體來說,您需要創建自定義JUnit規則。 我選擇擴展TestWatcher因為它看起來最合適

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;
import org.apache.log4j.RollingFileAppender;
import org.junit.rules.TestWatcher;
import org.junit.runner.Description;

public class TestMethodLogging extends TestWatcher {
  private static final String date = new SimpleDateFormat("y-MM-dd")
      .format(new Date());
  private Logger logger;

  @Override
  protected void starting(Description description) {
    String name = description.getMethodName();
    RollingFileAppender a = (RollingFileAppender) Logger.getRootLogger()
        .getAppender("rollingFile");
    PatternLayout layout = new PatternLayout();
    layout.setConversionPattern("%d{dd MMM yyyy HH:mm:ss} %p %t %c - %m%n");

    try {
      File logDir = new File(a.getFile()).getParentFile();
      File logFile = new File(logDir, name + "_" + date);
      logger = Logger.getLogger(name);
      logger.addAppender(new RollingFileAppender(layout, logFile
          .getAbsolutePath()));
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
  }

  public Logger getLogger() {
    return logger;
  }
}

完成此操作后,您可以將其作為規則放在測試類中。 規則只是測試中的一個字段(帶有@Rule注釋)。 在這里,我稱之為rule (不是很富有想象力,我承認)。 要從測試方法中記錄,您需要調用rule.getLogger()

import static org.junit.Assert.assertEquals;

import org.junit.Rule;
import org.junit.Test;

public class MyTest {
  @Rule
  public TestMethodLogging rule = new TestMethodLogging();

  @Test
  public void sumOfTwoInts() throws Throwable {
    rule.getLogger().error(
        "logging to a logger whose name is based on the test method's name");
    assertEquals(5, 2 + 3);
  }

  @Test
  public void productOfTwoInts() throws Throwable {
    rule.getLogger().error(
        "logging to a logger whose name is based on the test method's name");
    assertEquals(8, 2 * 4);
  }
}

當我運行此測試時,它會在~/Desktop/Selenium/AutomationLogs目錄下創建這兩個文件:

productOfTwoInts_2015-05-10  
sumOfTwoInts_2015-05-10

第一個文件的內容如下所示:

$ cat productOfTwoInts_2015-05-10 
10 May 2015 19:59:58 ERROR main productOfTwoInts - logging to a logger whose name is based on the test method's name
10 May 2015 20:01:22 ERROR main productOfTwoInts - logging to a logger whose name is based on the test method's name
10 May 2015 20:01:24 ERROR main productOfTwoInts - logging to a logger whose name is based on the test method's name

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM