使用java如何进行接口测试

关于接口测试, 我们之前介绍过很多方法了, 有postman, soapUI, Jmeter等, 他们各有优势和劣势, 今天和大家分享的是如何用java TestNG来实现接口测试.

 

开始测试之前需要如下准备工作:

1. IDE环境, Intellij 或者eclipse, 个人推荐使用Intellij.

2. JKD1.8 以上

3. Maven 环境 Maven环境配置

4. 被测接口(京东web版登录API)

 

首先来看下jd 登录接口,chrome浏览器打开www.jd.com, 使用chrome开发者工具, Windows用户按键盘F12, 将tab切换到Network, 使用密码登录方式, 登录成功后, 我们就可以获取到登录接口了,如果无法获取,使用charles第三方工具也是妥妥的.

 

看下我登录后开发者工具:

接着在ide中创建项目, 我们选择maven来创建项目
是不是非常的简单, 一步步都是next下去即可, Intellij就会帮你把maven项目创建好. 
如果你的IDE里没有maven,也不用着急, 在菜单的Preference->plugs里可以添加所需插件


在创建好的项目中, 我们首先进入pom.xml文件, 这是maven的配置文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.api.test</groupId>
    <artifactId>test</artifactId>
    <version>1.0-SNAPSHOT</version>

</project >
 

这边需要添加相应的项目管理依赖包, 如testng等, 适个人情况添加所需的依赖包.

<properties>
        <!-- 指明编译源代码时使用的字符编码,maven编译的时候默认使用的GBK编码,
         通过project.build.sourceEncoding属性设置字符编码,告诉maven这个项目使用UTF-8来编译 -->
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

        <!--这里可以设置需要运行group-->
        <groupsTest>execShell</groupsTest>

    </properties>

    <dependencies>

        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>7.0.0</version>
        </dependency>

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.10-FINAL</version>
        </dependency>

        <dependency>
            <groupId>org.jsoup</groupId>
            <artifactId>jsoup</artifactId>
            <version>1.9.1</version>
        </dependency>

        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.2</version>
        </dependency>

        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpmime</artifactId>
            <version>4.5.2</version>
        </dependency>
        <dependency>
            <groupId>com.jcraft</groupId>
            <artifactId>jsch</artifactId>
            <version>0.1.54</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.38</version>
        </dependency>


        <dependency>
            <groupId>commons-beanutils</groupId>
            <artifactId>commons-beanutils</artifactId>
            <version>1.6</version>
        </dependency>

        <dependency>
            <groupId>commons-collections</groupId>
            <artifactId>commons-collections</artifactId>
            <version>3.1</version>
        </dependency>


        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.1</version>
        </dependency>

        <dependency>
            <groupId>net.sf.ezmorph</groupId>
            <artifactId>ezmorph</artifactId>
            <version>1.0.6</version>
        </dependency>

        <dependency>
            <groupId>net.sf.json-lib</groupId>
            <artifactId>json-lib</artifactId>
            <version>2.4</version>
        </dependency>

    </dependencies>

 

 

如果需要maven来帮你build项目也是可以的, 方法也是非常简单

    <build>
        <plugins>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>8</source>
                    <target>8</target>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.19.1</version>
                <configuration>
                    <!--<suiteXmlFiles>-->
                        <!--<suiteXmlFile>./src/test/testng.xml</suiteXmlFile>-->
                    <!--</suiteXmlFiles>-->
                    <threadCount>1</threadCount>
                    <forkCount>1</forkCount>
                    <reuseForks>true</reuseForks>
                    <forkMode>once</forkMode>
                    <argLine>-Dfile.encoding=UTF-8</argLine>
                    <groups>${groupsTest}</groups>

                    <includes>
                        <include>**/*Test.java</include>
                    </includes>
                </configuration>
            </plugin>
        </plugins>
    </build>

 

 

测试思路:

以下示例中参数都来自于chrome开发者工具中捕获的信息, 因为被测接口是post方法,且在接口中声明了提交方式:

Content-Type:application/x-www-form-urlencoded; charset=UTF-8


 

所以在接口测试过程中, form data里的信息都要作为参数添加.
完成以上内容就可以开始创建一个测试的java类, 在你的项目的/test/java下面创建 NewApiTest.java

import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.testng.annotations.Test;


import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/*
 * @auth:louiezhou
 * @return: 测试数据
 * @parame:
 *
 */



public class NewApiTest {

    CloseableHttpResponse response=null;
    String entityStr = null;
    //登录url
    String url ="https://passport.jd.com/uc/loginService?uuid=a25f6873-4dd9-4334-ad4c-b8e3f&ReturnUrl=https%3A%2F%2Fwww.jd.com%2F&r=0.8097302259069017&version=2015";

    @Test(invocationCount = 1, threadPoolSize = 0)
    public void JdLoginTest() throws IOException {

        // 获取连接客户端工具
        CloseableHttpClient httpClient=HttpClients.createDefault();
        // 创建POST请求对象
        HttpPost httpPost=new HttpPost(url);

        // httpPost.addHeader post请求 header
        httpPost.addHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
        httpPost.addHeader("User-Agent:","Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.122 Safari/537.36");
//        List<Header> headerList= Lists.newArrayList();
//        headerList.add(new BasicHeader(HttpHeaders.CONTENT_TYPE,"application/x-www-form-urlencoded; charset=UTF-8"));
//        headerList.add(new BasicHeader(HttpHeaders.USER_AGENT,"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.122 Safari/537.36"));

        //参数封装对象
        List<NameValuePair> params=new ArrayList<NameValuePair>();

        try{
            params.add(new BasicNameValuePair("uuid","a25f6873-4dd9-4334-ad4c-e6f8992b8e3f"));
            params.add(new BasicNameValuePair("eid","BHM2GM3F6FVMTA3YNGT4JRDJBHAWFK7MSHDPF3SU4FHFSOLMBUP77TDQE5EB43BZPDT5J6PAQ6CFI"));
            params.add(new BasicNameValuePair("fp","f0aef80fec848c0a78c823bd234"));
            params.add(new BasicNameValuePair("loginType","c"));
            params.add(new BasicNameValuePair("loginname","zhangsan"));
            params.add(new BasicNameValuePair("nloginpwd","Y2nHBYQrhMS1/YvBs+cW8rrvbW1CYlmX61xfoLx7tgSpbgxZ8+/HCgefeAXphJVmefDdN/3d3UQLnFiyl7GSkGjZQNU4pw+9+202NovcR6q9G/haGpNKp/5h+Xs+J7BrUfKXvkmdmKS0fIs7ly0K+OY/BHKcYg="));
            params.add(new BasicNameValuePair("authcode","107fdb562e32406cab356c3ff97"));
            params.add(new BasicNameValuePair("pubKey","MIGfMA0GCSqGSIb3DQEgQDC7kw8r6tq43pwApYvkJ5laljaN9BZb21TAIfT/vexbobzH7Q8SUdP5uDPXEBKzOjx2L28y7Xs1d9v3tdPfKI2LR7PAzWBmDMn8riHrDDNpUpJnlAGUqJG9ooPn8j7YNpcxCa1iybOlc2kEhmJn5uwoanQq+CA6agNkqly2H4j6wIDAQAB"));
            params.add(new BasicNameValuePair("sa_token", "B68C442BEF615E156C81EFA53D580517BB9357FB9516A01E25761124AE9AF7B3CFA3C38D38484A734CB58C286401C2DEC2A5DFF3C9E856280AF80D4851C9B0239587771E8DC06B46454644D4908F4DC165CB70D86EAC7276BFBE489FFE2324EDDC5F71043BFB99B3D6E238B1AE9E67C3F297E0993B8497B1287640777CF4FFBA52FF032510AD19D7F371541C798742CB4378E5DD2119BADE9078310468AF8436A2B88593A92EEAF16FCFD55CD7F121B58D7A9A833D74068FACC5A6D6D8C3D1A850245F0742DEEC12BACF0FF9D5853FFDF1B37AC6A5E676DC635896AFD884D0BBB8A490E57234DF65A76AF189908F4AB80AEA36E56F6DD110EF7D36D119BB77F0B65774780348FFE859A68D2E0B3A3CDDAFD1BEFCD401530D536C8EF68B618969FC2FFD658FE0BA7BC2E7250F9CCDBB8F9AF360FC293F294A7279EA70043E860784C2E2CF11181C44561794A32AADDB2AC37B1294C08E7B63C85E6561F138195ECCF28EA0F08FB5A16DB7A20814DD914FC0C8A12BF29FFC4F73DD39361EBA1A849BB25B9F5957589347E205573754EA468D809CCCA698BFAB16373516DC8F5FEE8A24C2306850D601D6827C161F1A83057E0F93A97A0C034E"));
            params.add(new BasicNameValuePair("seqSid","31251240"));
            params.add(new BasicNameValuePair("useSlideAuthCode","1"));
            params.add(new BasicNameValuePair("_t","_t"));




            // 使用URL实体转换工具
            UrlEncodedFormEntity entityParam = new UrlEncodedFormEntity(params, "UTF-8");

            httpPost.setEntity(entityParam);
            // 执行请求
            response=httpClient.execute(httpPost);
            // 获得响应的实体对象
            HttpEntity entity=response.getEntity();
            // 使用Apache提供的工具类进行转换成字符串
            int code =response.getStatusLine().getStatusCode();
            System.out.println("StatusCode: " + code);
       Assert.assertEquals(200,code);
            entityStr=EntityUtils.toString(entity,"UTF-8");
    
            System.out.println("接口返回结果是:="+entityStr);

        }catch(Exception e){
            e.printStackTrace();
        }finally {

            //释放资源
            if(httpClient!=null) {
                httpClient.close();
            }
            if (response!=null){
                response.close();
            }
        }


    }
}
 

声明:

1.示例使用testng, 因此必须在方法上添加@Test符号,便于被识别到是测试代码块

2.使用了java的httpclient, 它是一个客户端的http通讯实现库.

HttpClient的目标是发送和接收HTTP 报文,不缓存内容, 重新格式化请求/重定向URI,或者其它和 HTTP 运输无关的功能。

3.使用了List 的实现类ArrayList来封装对象, ArrayList的优点是查询速度比较快.



右键java文件, 选择run 即可执行, 让我们来看下执行结果:

思考:
这样一个接口测试就实现了, 但作为一名严格要求自我的资深测试工程师来说, 这样的设计合理吗? 是否满足数据和测试业务分离, 断言是否充足, 代码是否可以重用, 怎么去管理N多个测试用例, 怎么降低代码耦合度, 怎么进行多线程测试, 测试报告如何展示,如何自动构建, 诸多问题等待解决.

请持续关注我, 和你分享更多精彩内容.

Story:

 

关注我们获得更多精彩内容!



赶快来关注吧,这里有你想找的热点内容,这里有你想要的各种资料,还有海量的资源,还在等什么。快来关注,大佬带你开车。

 

关注我们获得更多精彩内容

 

关注我们

方便拉您进群互动

测试常用Linux命令

测试工程师成长笔记

统计代码覆盖率-Python

CI/CD DevOps论述

Jmeter接口测试与数据驱动

 

 


 

    

posted @ 2020-03-12 12:11  Louiezhou  阅读(19216)  评论(2编辑  收藏  举报