Java - Logback 显示 运行 的状态(通过/失败)
Java - Logback to display status of run (passed / failed)
我在我的 Selenium Webdriver 项目中使用 Logback,我正试图找到一种方法来在测试结束时记录状态(无论测试通过还是失败),因此在查看日志时你知道哪个测试失败了。
我有一个 afterMethod
,我目前在其中记录“testGoogleWebsite 已完成”,但想要获取“testGoogleWebsite 已失败”或“testGoogleWebsite 已通过”。
我的测试:
@Test
public void testGoogleWebsite() {
openGoogleWebsite();
searchForWhosebug();
clickOnWhosebugLink();
}
@AfterMethod
public void afterMethod(Method method){
LOG.debug(method.getName() + " has finished");
}
我的登录文件:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<property name="DEV_HOME" value="target/Logs" />
<appender name="FILE-AUDIT" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${DEV_HOME}/debug.log</file>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n</Pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- rollover daily -->
<fileNamePattern>${DEV_HOME}/archived/debug.%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>10MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
</appender>
<logger name="com.test" level="debug" additivity="false">
<appender-ref ref="FILE-AUDIT" />
</logger>
<root level="debug">
<appender-ref ref="FILE-AUDIT" />
</root>
</configuration>
因为我看过 @AfterMethod
,所以我想您使用的是 TestNG。
那么,你可以试试:
@AfterMethod
public void tearDown(ITestResult result) {
String methodName = result.getMethod().getMethodName();
if (result.getStatus() == ITestResult.FAILURE) {
LOG.debug(methodName + " has failed");
} else if (result.getStatus() == ITestResult.SUCCESS) {
LOG.debug(methodName + " has passed");
}
}
@AfterMethod
public void afterMethod(ITestResult result)
{
String tcName = result.getName();
if(result.getStatus() == ITestResult.SUCCESS)
{
//Do something here
LOG.debug(tcName + " has passed");
}
else if(result.getStatus() == ITestResult.FAILURE)
{
//Do something here
LOG.debug(tcName + " has failed");
}
else if(result.getStatus() == ITestResult.SKIP ){
LOG.debug(tcName + " has skipped");
}
}
我在我的 Selenium Webdriver 项目中使用 Logback,我正试图找到一种方法来在测试结束时记录状态(无论测试通过还是失败),因此在查看日志时你知道哪个测试失败了。
我有一个 afterMethod
,我目前在其中记录“testGoogleWebsite 已完成”,但想要获取“testGoogleWebsite 已失败”或“testGoogleWebsite 已通过”。
我的测试:
@Test
public void testGoogleWebsite() {
openGoogleWebsite();
searchForWhosebug();
clickOnWhosebugLink();
}
@AfterMethod
public void afterMethod(Method method){
LOG.debug(method.getName() + " has finished");
}
我的登录文件:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<property name="DEV_HOME" value="target/Logs" />
<appender name="FILE-AUDIT" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${DEV_HOME}/debug.log</file>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n</Pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- rollover daily -->
<fileNamePattern>${DEV_HOME}/archived/debug.%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>10MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
</appender>
<logger name="com.test" level="debug" additivity="false">
<appender-ref ref="FILE-AUDIT" />
</logger>
<root level="debug">
<appender-ref ref="FILE-AUDIT" />
</root>
</configuration>
因为我看过 @AfterMethod
,所以我想您使用的是 TestNG。
那么,你可以试试:
@AfterMethod
public void tearDown(ITestResult result) {
String methodName = result.getMethod().getMethodName();
if (result.getStatus() == ITestResult.FAILURE) {
LOG.debug(methodName + " has failed");
} else if (result.getStatus() == ITestResult.SUCCESS) {
LOG.debug(methodName + " has passed");
}
}
@AfterMethod
public void afterMethod(ITestResult result)
{
String tcName = result.getName();
if(result.getStatus() == ITestResult.SUCCESS)
{
//Do something here
LOG.debug(tcName + " has passed");
}
else if(result.getStatus() == ITestResult.FAILURE)
{
//Do something here
LOG.debug(tcName + " has failed");
}
else if(result.getStatus() == ITestResult.SKIP ){
LOG.debug(tcName + " has skipped");
}
}