[转] JAVA+Maven+TestNG搭建接口测试框架及实例

JAVA+Maven+TestNG搭建接口测试框架及实例
        </h1>
        <div class="clear"></div>
        <div class="postBody">

1、配置JDK

见另一篇博客:http://www.cnblogs.com/testlurunxiu/p/5933912.html

2、安装Eclipse以及TestNG

Eclipse下载地址:http://beust.com/eclipse

TestNG安装过程:

在线安装

输入网址:http://beust.com/eclipse

在线安装会比较慢,有的人可能还会链接不上这个地址,所以下面介绍一个离线下载的方法

离线下载:TestNG Eclipse 插件下载地址http://testng.org/doc/download.html

a.下载离线安装包并解压

b.将解压后的文件..\eclipse-testng离线包\features\目录下的文件夹org.testng.eclipse_6.8.6.20130607_0745放到eclipse--》features目录下;
c.将解压后的文件..\eclipse-testng离线包\org.testng.eclipse_6.8.6.20130607_0745文件夹放到eclipse--》plugins目录下;
d.重启eclipse.

如何查看testng是否安装成功了呢?

3、接口测试框架的搭建

新建一个maven程序

Finish之后,工程以及默认pxm.xml文件内容,如图所示:

在pom.xml文件里面导入需要的jar包依赖,类似如下代码

<dependencies>
  <dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
  </dependency>
</dependencies>

导入TestNG依赖包

新建testng class文件

 新建的testng自动生成如下,其中<class>节点里面的为运行内容

导入成功之后的项目工程如下:

 4、接口测试用例

获取并且执行接口代码如下:

复制代码
public class HttpUtils {
</span><span style="color: #0000ff;">static</span> CloseableHttpClient httpclient =<span style="color: #0000ff;">null</span><span style="color: #000000;">;

</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> OpenHttpClient()
{<br>    //打开浏览器
    httpclient </span>=<span style="color: #000000;"> HttpClients.createDefault();
}

</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> CloseHttpClient()
{<br>     //关闭浏览器
    </span><span style="color: #0000ff;">try</span><span style="color: #000000;"> {
        httpclient.close();
    } </span><span style="color: #0000ff;">catch</span><span style="color: #000000;"> (IOException e) {
        
        </span><span style="color: #008000;">//</span><span style="color: #008000;"> TODO Auto-generated catch block</span>

e.printStackTrace();
}
httpclient = null;
}

</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span><span style="color: #000000;"> JSONObject visitUrl(String url)
{
    </span><span style="color: #008000;">//</span><span style="color: #008000;">CloseableHttpClient httpclient = HttpClients.createDefault();</span>
HttpGet httpGet = new HttpGet(url); // HttpPost httpPost = new HttpPost(url); JSONObject jsonObj= null ; try { CloseableHttpResponse response = httpclient.execute(httpGet); HttpEntity entity = response.getEntity();
        StringBuilder jsonStr </span>= <span style="color: #0000ff;">new</span><span style="color: #000000;"> StringBuilder();
        BufferedReader bufferedReader </span>= <span style="color: #0000ff;">new</span> BufferedReader(<span style="color: #0000ff;">new</span> InputStreamReader(entity.getContent(), "UTF-8"<span style="color: #000000;">),
                </span>8 * 1024<span style="color: #000000;">);
        String line </span>= <span style="color: #0000ff;">null</span><span style="color: #000000;">;
        </span><span style="color: #0000ff;">while</span> ((line = bufferedReader.readLine()) != <span style="color: #0000ff;">null</span><span style="color: #000000;">) {
            jsonStr.append(line </span>+ "/n"<span style="color: #000000;">);
        }
        EntityUtils.consume(entity);
        //获取JSON对象的值</span>
        jsonObj = <span style="color: #0000ff;">new</span><span style="color: #000000;"> JSONObject(jsonStr.toString());
        response.close();


    } </span><span style="color: #0000ff;">catch</span><span style="color: #000000;"> (ClientProtocolException e) {
        </span><span style="color: #008000;">//</span><span style="color: #008000;"> TODO Auto-generated catch block</span>

e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();

    }

    
    </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> jsonObj;
}

}

复制代码

 测试用例代码:

复制代码
public class Test {   
  public Assertion assertion; @BeforeClass public void beforeClass() { assertion = new Assertion(); }
@BeforeMethod
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> runBeforeMethod() {
    </span><span style="color: #008000;">//</span><span style="color: #008000;"> 打开httpclient,相当于打开一个浏览器</span>

HttpUtils.OpenHttpClient();//这边一定要记得在测试用例开始之前打开浏览器,否则会出现空指针的错误
}
@AfterMethod
public void runAfterMethod() {
// 打开httpclient,相当于打开一个浏览器
HttpUtils.CloseHttpClient();
}

@org.testng.annotations.Test
public void f() throws ClientProtocolException, IOException {

    String loginUrl </span>= "http://xx.xxx.cn/Org/PCUserLogin.do?u=11111&amp;p=1111&amp;groupId=1"<span style="color: #000000;">;
    JSONObject json </span>=<span style="color: #000000;"> HttpUtils.visitUrl(loginUrl);
    </span><span style="color: #0000ff;">boolean</span> success = json.getBoolean("success"<span style="color: #000000;">);
    String enterTrainningUrl </span>= "http://xx.xxx.cn/Training/enterTrainingCamp.do?roomid=1111"<span style="color: #000000;">;
    System.out.println(enterTrainningUrl);
    JSONObject enterObj </span>=<span style="color: #000000;"> HttpUtils.visitUrl(enterTrainningUrl);
    System.out.println(enterObj.toString());
    </span><span style="color: #0000ff;">boolean</span> success2 = enterObj.getBoolean("success"<span style="color: #000000;">);
    assertion.assertTrue(success);

}
}

复制代码

右键单击testng.xml运行

 

结果如下,passed

 运行完成之后,刷新工程,在根目录下会生成一个test_output文件夹,打开index.html,可以看见测试报告

目之所及,皆是回忆 心之所想,皆是过往 眼之所看,皆是遗憾
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值