2015-05-10 160 views
4

我想将日志记录添加到我的Selenium Java测试中。我已经实现了log4jFramework,它可以将日志放在控制台或文件中。Log4J与JUnit测试

我正在使用JUnit测试框架,我想在日志文件的文件名中包含测试名称和日期/时间,而不是使用标准惯例,这似乎是在文件前加上数字+1 if一个文件已经存在。

这是我的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 

我发现网上似乎只是相关的日常滚动文件。我想生成日志的新文件每次运行单元测试时

+0

“测试名称“是指测试方法的名称还是测试类的名称? –

+0

请关注此:http://stackoverflow.com/questions/1444520/is-it-possible-to-configure-log4j-to-create-a-new-file-with-every-run-of-the-app –

回答

5

如果我理解你的需求正确,这可以通过组合以下来实现:使用JUnit的方法规则,将

  • 给你目前的方法
  • 在运行时创建一个Log4j追加,并与方法的名称配置它的名字获得你刚刚获得

具体来说,您需要创建一个自定义的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 
当你说
+0

感谢一百万,这正是我在 –

+0

之后所欢迎的:)请注意,我只是稍微更改了TestMethodLogging类。具体来说,SimpleDateFormat现在只使用一次,从而避免其线程安全问题(http://stackoverflow.com/questions/6840803/simpledateformat-thread-safety) –

+0

cc @DanielCohen - 我也稍微改变了传递给SimpleDateFormat的模式:如果需要,日/月现在有一个前导零,以便我们得到2015-05-10(而不是2015-5-10) –