创建maven项目
输入GroupId:com.qzcsbj
输入ArtifactId:apiJavaAutoTest
输入Project name:apiJavaAutoTest
配置maven
修改Maven home directory和User settings file(settings.xml文件配置了阿里云镜像地址)
添加pom依赖
testng
<!-- https://mvnrepository.com/artifact/org.testng/testng -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.8.8</version>
<scope>test</scope>
</dependency>
log4j
<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
创建目录结构
创建配置文件
resources下创建log4j.properties
### 根logger 设置###
log4j.rootLogger = INFO,console,file
### 输出信息到控制台 ###
log4j.appender.console = org.apache.log4j.ConsoleAppender
log4j.appender.console.Target = System.out
log4j.appender.console.layout = org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern = [%p] %d{HH:mm:ss} method: %l----%m%n
### 输出INFO级别以上的日志文件设置(因为根配置的是INFO) ###
log4j.appender.file = org.apache.log4j.DailyRollingFileAppender
log4j.appender.file.File = logs/qzcsbj.log
log4j.appender.file.Append = true
log4j.appender.file.layout = org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern = %d{HH:mm:ss} method: %l - [ %p ] ----%m%n
创建测试类
testcase下创建BaseCase.java,测试方法是test
package com.qzcsbj.autotest.testcase;
import org.apache.log4j.Logger;
import org.testng.annotations.*;
/**
* @描述 : <BaseCase>
*/
public class BaseCase{
public static Logger logger = Logger.getLogger(BaseCase.class);
@Test
public void test(){
System.out.println("hello, autotest");
logger.info("finish");
}
}
创建testng的xml文件
在testngXML目录下创建testng.xml文件,指定测试类
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="apiAutoTest" verbose="1">
<test name="test" enabled="true"> <!--test必须有name属性-->
<classes>
<class name="com.qzcsbj.autotest.testcase.BaseCase"/>
</classes>
</test>
</suite>
运行testng的xml
结果
__EOF__
本文作者:持之以恒(韧)
关于博主:擅长性能、全链路、自动化、企业级自动化持续集成(DevTestOps)、测开等
标签:03,java,log4j,file,自动化,org,testng,appender,### From: https://blog.51cto.com/qzcsbj/9084446