日志记录是一种流行的解决方案,用于显示软件在运行时的运行状况。
但是,当我们使用jUnit / TestNG对应用程序进行单元测试时,日志记录会怎样?
在自动化测试执行期间,我们通常不希望看到日志记录消息,因为我们的主要兴趣是测试结果。
能够在标准的自动测试过程中禁用日志记录消息将是很好的。
相反,在某些情况下,日志记录消息在测试时可能有用。 一个典型的例子是,当我们为某些遗留代码编写缺少的测试代码时,在适当覆盖测试范围之前,我们不想碰。 在这种情况下,在控制台上记录消息可以帮助我们理解代码及其工作方式。
因此,我们可以确定三个用例:
- 运行模式 ,在执行应用程序时:启用日志并根据应用程序的要求进行配置
- 测试执行模式 ,当一起执行自动测试时:应禁用日志记录消息
- 测试创建模式 ,当我们创建新测试时:日志消息很有用,但最好将它们放在控制台中
让我们看一个基于Maven和SLF4J的示例, SLF4J是流行的日志记录外观。
- 完整的项目可以在这里找到。
项目pom.xml
中的典型SLF4J配置如下:
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j.version}</version>
<scope>runtime</scope>
</dependency>
所述slf4-api
依赖是主要的SLF4J库,而第二个, slf4j-log4j12
,所引用的可能记录的发动机(之一LOG4J ),其可工作SLF4J 下方 。
这是运行模式配置。 在此示例中,项目资源将包含一个LOG4J属性文件,该文件指示LOG4J应该记录什么,如何记录以及在哪里记录。
当我们使用不同的日志记录引擎(如java.util.logging(JDK)和Logback)时,也会发生相同的情况。 有关更多详细信息,请参见Slf4J手册 。
在测试执行模式下,我们不想记录日志,因此我们可以简单地添加以下范围测试依赖项
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-nop</artifactId>
<version>${slf4j.version}</version>
<scope>test</scope>
</dependency>
NOP记录器(slf4j-nop)只是丢弃所有记录。
重要提示 : pom.xml
的依赖关系顺序很重要。 将slf4j-nop
依赖项放在slf4-api
依赖项之后,这样就可以在测试期间使用它,即使该依赖项中还有另一个日志记录引擎。
当pom.xml
有多个日志记录引擎依赖项时,无论如何SLF4J都会显示如下消息:
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:.m2/repository/org/slf4j/slf4j-nop/1.7.12/slf4j-nop-1.7.12.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:.m2/repository/org/slf4j/slf4j-log4j12/1.7.12/slf4j-log4j12-1.7.12.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.slf4j.helpers.NOPLoggerFactory]
总而言之,下面是一个完整的配置示例:
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-nop</artifactId>
<version>${slf4j.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j.version}</version>
<scope>runtime</scope>
</dependency>
同样,应该调整最后一个依赖项以匹配所需的日志记录引擎。
如前所述,创建新测试时日志记录很有用。 在这种情况下( 测试创建模式 ),我们可以用slf4j-simple
依赖项临时替换slf4j-nop
,这将启用SLF4J Simple logger 。
现在,在执行测试期间,日志消息将作为System.err
消息显示在控制台窗口中。 不需要配置文件。
默认情况下,简单记录器不会记录DEBUG消息。 标准日志记录级别为INFO。
您可以使用此处记录的系统变量来自定义Simple logger行为。
定义简单记录器配置的明智方法是使用[Surefire插件配置部分]( http://maven.apache.org/surefire/maven-surefire-plugin/examples/system-properties.html ):
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<configuration>
<systemPropertyVariables>
<org.slf4j.simpleLogger.defaultLogLevel>DEBUG</org.slf4j.simpleLogger.defaultLogLevel>
<org.slf4j.simpleLogger.showDateTime>true</org.slf4j.simpleLogger.showDateTime>
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>
</build>
在systemPropertyVariables部分中,我们可以使用Simple logger变量名称创建标签。 在上面的示例中,启用了DEBUG和时间戳记录。
翻译自: https://www.javacodegeeks.com/2015/11/tutorial-logging-tests.html