1 安装Maven
创建Maven项目,直接在pom.xml中引入下面代码,IDEA会自动下载依赖包。
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.1.0</version>
<scope>compile</scope>
</dependency>
2 编写代码
可以直接引用TestNG注解,以下是我的测试demo
package com.daojia.clean;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class TestDemo {
@BeforeClass
public void beforeClass(){
System.out.println("beforeClase");
}
@AfterClass
public void afterClass(){
System.out.println("afterClass");
}
@Test
public void testcase1(){
Assert.assertEquals(1,1);
System.out.println("test01");
}
@Test
public void testcase2(){
Assert.assertTrue(true);
System.out.println("test02");
}
@DataProvider(name="user")
public Object[][] getStr(){
return new Object[][]{
{"","","账户不能为空"},
{"admin","","密码不能为空"},
{"admin","123","密码错误"}
};
}
@Test(dataProvider = "user")
private void sout(String uname,String pword,String msg){
System.out.println(uname+"->"+pword+"->"+msg);
}
}
3 testng.xml配置和执行
项目构建需要添加如下配置,先记着,他的作用以后在整理
1.pom.xml 添加 <build></build>配置
<build>
<plugins>
<!--添加插件 关联testng-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20.1</version>
<configuration>
<!-- 解决maven test命令时console出现中文乱码乱码 -->
<argLine>-Dfile.encoding=UTF-8</argLine>
<testFailureIgnore>true</testFailureIgnore>
<suiteXmlFiles>
<file>testng.xml</file>
</suiteXmlFiles>
</configuration>
</plugin>
</plugins>
<!--配置resources 资源-->
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
<include>**/*.xls</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
创建testng.xml,文件放在项目根目录下,和pom.xml 同级。testng.xml 如下
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="suitetest">
<test name="test" preserve-order="true" verbose="3">
<packages>
<package name="com.daojia.clean.*" />
</packages>
<classes>
<class name="com.daojia.clean.TestDemo">
<methods>
<include name="test1"/>
</methods>
</class>
<class name="com.daojia.clean.TestDemo1"/>
</classes>
</test>
</suite>
扫描要执行测试的包cases
配置完成后,本地terminal中执行:mvn test ,如下显示我么们bulid 做的事情。
网友评论