【AT】TestNG的使用

一、引入使用

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 解决方式

  1. 查看测试类是否加注解@SpringBootTest,如果没有需加上
  2. 查看测试类是否继承AbstractTestNGSpringContextTests,如果没有需加上
  3. 查看要装配的类是否使用@Autowired注解

在这里插入图片描述

2.3 调试

运行测试类,运行成功

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

sysu_lluozh

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值