java +testNG 接口测试框架搭建(一)

一.创建maven工程

1.打开IDEA,选择file–>new project,选择maven,选择自己本地的JDK,点击next:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

建好以后,默认有src->main->java、resourcel两个文件夹,以及pom.xml文件

二、在pom.xml文件中导入依赖包

<groupId>com.iflytek.test</groupId>
<artifactId>QM-QQZW20-TEST</artifactId>
<version>1.0-SNAPSHOT</version>
<!-- 私有仓库依赖:maven仓库-->
<repositories>
    <repository>
        <id>repo</id>
        <name>repo</name>
        <url>http://maven.ifly.com:8888/nexus/content/groups/public</url>
    </repository>
    <repository>
        <id>iflytek-public</id>
        <name>iflytek-public</name>
        <url>http://maven.ifly.com:8888/nexus/content/groups/public/</url>
    </repository>
</repositories>


<dependencies>
        <!-- Spring框架依赖:用于构建Spring容器 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.3.16.RELEASE</version>
        </dependency>

        <!-- Spring单元测试依赖:用于测试类启动spring容器 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>3.2.6.RELEASE</version>
            <!--注意去掉scode-
        </dependency>


        <!-- Spring其他依赖-->
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-mongodb</artifactId>
            <version>1.6.2.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>4.3.8.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>4.3.16.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>4.3.16.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>4.3.16.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-expression</artifactId>
            <version>4.3.16.RELEASE</version>
        </dependency>

 <!-- dubbo依赖--> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.5.3</version> <exclusions> <exclusion> <artifactId>spring</artifactId> <groupId>org.springframework</groupId> </exclusion> </exclusions> </dependency>

<!--EPSA DUBBO依赖,和dubbo二选一,根据项目来-->
<dependency>
         <groupId>com.iflytek.edu</groupId>
         <artifactId>epas-dubbo</artifactId>
         <version>1.0.4.1001</version>
         <exclusions>
             <exclusion>
                 <groupId>org.springframework</groupId>
                 <artifactId>spring</artifactId>
             </exclusion>
         </exclusions>
     </dependency>
<!-- 引入相应工程的jar包:*根据实际项目去引入-->
 <dependency> <groupId>xxxxx(e.g:com.qiming.qqzw)</groupId> 
 <artifactId>xxxxxx(e.g:resource-center-service-api)</artifactId> 
 <version>1.0.0.1001-SNAPSHOT</version>
 </dependency> 
<!-- testng依赖:测试框架--> 
<dependency> 
<groupId>org.testng</groupId>
 <artifactId>testng</artifactId> 
<version>6.14.3</version> 
<!--注意去掉scode--> 
</dependency> 
<!-- fastjson依赖:主要用于接口返回结果中解析json对象 -->
 <dependency>
 <groupId>com.alibaba</groupId> 
<artifactId>fastjson</artifactId> 
<version>1.2.54</version> 
</dependency> 

<!-- 流式断言器依赖:要比于junit和testng中的断言,assertj的api更强大--> 
<dependency> 
<groupId>org.assertj</groupId> 
<artifactId>assertj-core</artifactId> 
<version>3.11.1</version>
 </dependency> 

<!--引入@case注解--> 
<dependency>
 <groupId>org.uncommons</groupId> 
<artifactId>reportng</artifactId>
 <version>1.2.3-SNAPSHOT</version>
 </dependency> 

<!--引入AbstractTestNGSpringContextTest-->
 <dependency> 
<groupId>com.github.yongchristophertang</groupId> 
<artifactId>rest-connector</artifactId> 
<version>0.5.17</version> 
</dependency> 

<!--引入其他依赖包-->
<!--引入git关联依赖-->
 
<dependency>
    <groupId>com.github.yongchristophertang</groupId>
    <artifactId>rest-connector</artifactId>
    <version>0.5.17</version>
</dependency>

 <dependency> 
<groupId>org.dom4j</groupId> 
<artifactId>dom4j</artifactId> 
<version>2.0.0</version> 
</dependency> 
<dependency>
 <groupId>org.slf4j</groupId> 
<artifactId>slf4j-simple</artifactId>
 <version>1.7.25</version> 
</dependency> 
<dependency> 
<groupId>com.101tec</groupId>
 <artifactId>zkclient</artifactId>
 <version>0.2</version> 
</dependency> 
<!--其他依赖包结束-->
 </dependencies>
<!--自动编译依赖-->
<build>
    <sourceDirectory>src/main/java</sourceDirectory>
    <testSourceDirectory>src/main/java</testSourceDirectory>
    <finalName>${project.artifactId}</finalName>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>7</source>
                <target>7</target>
                <encoding>UTF-8</encoding>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.6</version>
            <configuration>
                <testFailureIgnore>true</testFailureIgnore>
                <!-- <testNGArtifactName>org.testng:testng</testNGArtifactName>-->
                <forkMode>once</forkMode>
                <!--<skipTests>true</skipTests>-->
            </configuration>
        </plugin>
    </plugins>
</build>

三、配置xml配置文件,在resources文件下建bean.xml:

在这里插入图片描述

(1).dubbo配置bean.xml格式数据:

xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://code.alibabatech.com/schema/dubbo
    http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <dubbo:application name="qzw20-test" owner="x2_consumer"/>
    <!--填写提供者地址以及端口-->
 <dubbo:registry address="zookeeper://110.1.1.1:8888" register="true"/>
  <!--填写dubbo类,id值可以任意取,与interface没有联系-->
<dubbo:reference id="hanyuOverSeaSceneService" interface="com.qiming.qqzw.resource.api.oversea.IHanyuOverSeaSceneService" c

(2)epas dubbo 配置bean.xml格式数据:

<?xml version="1.0" encoding="utf-8"?>
<beans
        xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
    http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd  http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- epas配置 客户端使用EpasConfig配置 -->
    <bean id="epasConfig" class="com.iflytek.edu.epas.dubbo.config.EpasConfig">
        <!-- 在平台申请的appKey -->
        <property name="appKey" value="qqzw-code-test" />
        <property name="appSecret" value="test" />
        <!-- 地址服务url  -->
        <property name="addrServerUrl" value="http://xxx.xxx.com/xxx" />
        <!-- 是否默认使用dubbo代理服务(外网客户端模式) 默认值为false -->
        <!--<property name="proxy" value="false" />-->
    </bean>
    <dubbo:application name="qqzw20-test"/>
<!--group需要与开发确认,id随意填写,registry要与id对应-->
<dubbo:registry id="resourceConsumer" protocol="epas" address="epasConfig" group="qqzw-resource-service-test"/>
<dubbo:reference registry="resourceConsumer" id="hanyuBaseResourceService" interface="com.qiming.qqzw.resource.api.baseResour

四、编写接口测试脚本,新建一个文件TestApply,file->new ->java class:

import com.alibaba.fastjson.JSONObject;
import com.github.yongchristophertang.database.testng.TestNGDBInjectionModuleFactory;
import com.github.yongchristophertang.engine.java.ProxyFactories;
import com.qiming.qqzw.resource.api.oversea.IHanyuOverSeaSceneService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Guice;
import org.testng.annotations.Test;
import org.uncommons.reportng.annotation.Case;

import javax.annotation.Resource;
import java.util.*;
import java.util.Map;
@Guice(moduleFactory = TestNGDBInjectionModuleFactory.class)
@ContextConfiguration(locations = "classpath:bean*.xml")

//要注入Service
@Service
public class TestApply extends AbstractTestNGSpringContextTests{
    @Autowired
    private IHanyuOverSeaSceneService hanyuOverSeaSceneServiceProxy;
    private IHanyuOverSeaSceneService hanyuOverSeaSceneService;
    
    @BeforeClass
    public void init() {
        hanyuOverSeaSceneService = ProxyFactories.createLoggerProxy(hanyuOverSeaSceneServiceProxy);
    }

    @Test(priority=1)
    @Case("BVT test")
    public void pageOverseaScene_BVT_Test() {
        Map<String, Object> listOverseaSceneDialogueBySceneId_param = new HashMap<String, Object>();
        listOverseaSceneDialogueBySceneId_param.put("pageNum",2);
        listOverseaSceneDialogueBySceneId_param.put("pageSize",6);
        JSONObject listResourceById_result= hanyuOverSeaSceneService.pageOverseaScene(listOverseaSceneDialogueBySceneId_param);
    }

}
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我知道了。下面是搭建接口自动化测试框架的步骤: 1. 安装 Eclipse 首先,你需要安装 Eclipse,可以在官网上下载最新版本的 Eclipse IDE。 2. 安装 TestNG 插件 打开 Eclipse 后,进入 Help -> Eclipse Marketplace,在搜索框中输入 TestNG,然后点击 Install 安装 TestNG 插件。 3. 创建 Maven 项目 在 Eclipse 中创建一个 Maven 项目,选择 Create a simple project,然后勾选 Create a simple project (skip archetype selection),接下来输入项目名称和项目路径,最后点击 Finish。 4. 配置 pom.xml 文件 在项目中的 pom.xml 文件中添加以下依赖: ``` <dependencies> <dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>7.4.0</version> <scope>test</scope> </dependency> <dependency> <groupId>io.rest-assured</groupId> <artifactId>rest-assured</artifactId> <version>4.4.0</version> </dependency> </dependencies> ``` 这里我们使用了 TestNG 和 Rest Assured 两个依赖。 5. 创建测试类 在 src/test/java 目录下创建一个测试类,例如 TestDemo。在测试类中编写测试方法,例如: ``` import org.testng.annotations.Test; import io.restassured.RestAssured; import static io.restassured.RestAssured.*; public class TestDemo { @Test public void testDemo() { RestAssured.baseURI = "https://jsonplaceholder.typicode.com"; given().log().all() .when().get("/posts/1") .then().log().all().statusCode(200); } } ``` 这里的测试方法使用了 Rest Assured 库来发送 HTTP 请求,并使用 TestNG 的断言来验证响应状态码是否为 200。 6. 运行测试 在 Eclipse 中右键点击测试类或测试方法,选择 Run As -> TestNG Test 来运行测试。 这就是使用 Eclipse 和 TestNG 搭建接口自动化测试框架的步骤。希望能对你有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值