一、引入使用
1.1 导入TestNG依赖包
修改pom.xml文件,添加以下依赖
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.8.3</version>
<scope>compile</scope>
</dependency>
1.2 添加编译插件和执行测试插件
修改pom.xml文件,添加以下插件
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.15</version>
<configuration>
<!--<testFailureIgnore>true</testFailureIgnore>-->
<forkMode>never</forkMode>
<argLine>-Dfile.encoding=UTF-8</argLine>
<suiteXmlFiles>
<suiteXmlFile>xml/testNG.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
</plugins>
</build>
1.3 创建test目录文件夹
1.4 创建测试类,进行编译调试
在Java文件夹下new -> Java Class
,输入如下代码,并编译
import org.testng.Assert;
import org.testng.annotations.Test;
public class TestDemo {
@Test
public void testcase1(){
Assert.assertTrue(false);
System.out.println("testcase1");
}
}
1.5 编写testNG.xml
xml文件用于按照需要批量执行用例,右键选择运行可独立执行
<?xml version="1.0" encoding="utf-8" ?>
<suite name="testproj" parallel="false">
<test name="testDemo1">
<classes>
<class name="TestDemo"></class>
</classes>
</test>
</suite>
若要在最后一步生成测试报告,需修改pom文件中修改build->plugins->plugin->configuration
中配置文件地址为xml文件所在地址
<suiteXmlFiles>
<suiteXmlFile>xml/testNG.xml</suiteXmlFile>
</suiteXmlFiles>
二、装配@Autowired bean为null
在使用TestNG搭建接口自动化框架时,发现bean装配不成功,装载的对象为null
2.1 报错信息
java.lang.NullPointerException
at com.lluozh.util.InfluxDbUtilTest.testQueryGroup(InfluxDbUtilTest.java:38)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84)
at com.intellij.rt.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:66)
at com.intellij.rt.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:110)
===============================================
Default Suite
Total tests run: 1, Failures: 1, Skips: 0
===============================================
2.2 解决方式
- 查看测试类是否加注解@SpringBootTest,如果没有需加上
- 查看测试类是否继承AbstractTestNGSpringContextTests,如果没有需加上
- 查看要装配的类是否使用@Autowired注解
2.3 调试
运行测试类,运行成功