2016-11-09 1 views
0

J'utilise Logback dans mon projet Selenium Webdriver et j'essaie de trouver un moyen de consigner le statut (que le test soit réussi ou non) à la fin du test, donc lorsque vous regardez à travers les journaux, vous savez quel test a échoué.Java - Logback pour afficher l'état d'exécution (réussi/échoué)

J'ai un afterMethod où je me connecte actuellement « testGoogleWebsite a terminé », mais que vous voulez obtenir « testGoogleWebsite a échoué » ou « testGoogleWebsite a passé ».

Mon test:

@Test 
public void testGoogleWebsite() { 
    openGoogleWebsite(); 
    searchForStackOverflow(); 
    clickOnStackOverflowLink(); 
} 


@AfterMethod 
public void afterMethod(Method method){ 
    LOG.debug(method.getName() + " has finished"); 
} 

Mon fichier logback:

<?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> 
+0

Donc, vous utilisez TestNG, non? Parce que cela devrait être l'étiquette la plus pertinente pour votre question. –

Répondre

1
@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"); 
    } 
} 
0

Parce que je l'ai vu @AfterMethod, je suppose que vous utilisez TestNG.

Ensuite, vous pouvez essayer:

@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"); 
    }  
}