一、配置JDKhtml
见另外一篇博客:http://www.cnblogs.com/testlurunxiu/p/5933912.htmljson
二、安装Eclipse以及TestNG浏览器
Eclipse下载地址:http://beust.com/eclipseapp
TestNG安装过程:框架
在线安装eclipse
输入网址:http://beust.com/eclipsemaven
在线安装会比较慢,有的人可能还会连接不上这个地址,因此下面介绍一个离线下载的方法测试
离线下载:TestNG Eclipse 插件下载地址http://testng.org/doc/download.htmlui
a.下载离线安装包并解压url
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是否安装成功了呢?
三、接口测试框架的搭建
新建一个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>节点里面的为运行内容
导入成功以后的项目工程以下:
四、接口测试用例
获取而且执行接口代码以下:
public class HttpUtils { static CloseableHttpClient httpclient =null; public static void OpenHttpClient() {
//打开浏览器 httpclient = HttpClients.createDefault(); } public static void CloseHttpClient() {
//关闭浏览器 try { httpclient.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } httpclient = null; } public static JSONObject visitUrl(String url) { //CloseableHttpClient httpclient = HttpClients.createDefault(); 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 = new StringBuilder(); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(entity.getContent(), "UTF-8"), 8 * 1024); String line = null; while ((line = bufferedReader.readLine()) != null) { jsonStr.append(line + "/n"); } EntityUtils.consume(entity); //获取JSON对象的值 jsonObj = new JSONObject(jsonStr.toString()); response.close(); } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return jsonObj; } }
测试用例代码:
public class Test {
public Assertion assertion; @BeforeClass public void beforeClass() { assertion = new Assertion(); } @BeforeMethod public void runBeforeMethod() { // 打开httpclient,至关于打开一个浏览器 HttpUtils.OpenHttpClient();//这边必定要记得在测试用例开始以前打开浏览器,不然会出现空指针的错误 } @AfterMethod public void runAfterMethod() { // 打开httpclient,至关于打开一个浏览器 HttpUtils.CloseHttpClient(); } @org.testng.annotations.Test public void f() throws ClientProtocolException, IOException { String loginUrl = "http://xx.xxx.cn/Org/PCUserLogin.do?u=11111&p=1111&groupId=1"; JSONObject json = HttpUtils.visitUrl(loginUrl); boolean success = json.getBoolean("success"); String enterTrainningUrl = "http://xx.xxx.cn/Training/enterTrainingCamp.do?roomid=1111"; System.out.println(enterTrainningUrl); JSONObject enterObj = HttpUtils.visitUrl(enterTrainningUrl); System.out.println(enterObj.toString()); boolean success2 = enterObj.getBoolean("success"); assertion.assertTrue(success); }
}
右键单击testng.xml运行
结果以下,passed
运行完成以后,刷新工程,在根目录下会生成一个test_output文件夹,打开index.html,能够看见测试报告