关于junit4和HttpClient的自动化测试

 

 

大家好,第一次在这个社区发博,一篇对junit的理解,junit是支持打包测试和参数化测试的,它一样都不少。

 

代码贴上:

 


 


 

package com.junithttpclient;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
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.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
@RunWith(Parameterized.class)
public class TestJhc {
	private String path;private String postUserNameKey;
	private String postUserNameValue;
	private String postPasswordKey;
	private String postPasswordValue;
	CloseableHttpClient httpClient;
	HttpPost httpPost;
	UrlEncodedFormEntity uefEntity;
	List<NameValuePair> params ;
	CloseableHttpResponse response;
}
//以上参数均通过 构造函数赋值
public TestJhc(String path,String postUserNameKey,String postUserNameValue,String postPasswordKey,String postPasswordValue){
	this.path= path;
	this.postUserNameKey=postUserNameKey;
	this.postUserNameValue = postUserNameValue;
	this.postPasswordKey = postPasswordKey;
	this.postPasswordValue = postPasswordValue;
	}
//@Parameters该注解的方法必须为static的,且把数据对应给待赋值的构造方法
@Parameters
public static List<Object[]> paramters(){
	//接口地址PathUrl
	String path = "http://localhost:8080/tedu/user/handleLogin.do";
	String usernameKey = "lname";
	String usernameValue ="alen";
	String userPasswordKey = "lpwd";
	String userpasswordValue = "123456";
	return Arrays.asList(new Object[][]{{path,usernameKey,usernameValue,userPasswordKey,userpasswordValue}});
	}
}
//@Before 表示在执行每个test个例之前均会在test个例之前执行,一般用于每个测试单例均有的数据初始化
@Before
public void setUp(){
	httpClient = HttpClients.createDefault();
	httpPost = new HttpPost(path);
	params = new ArrayList<NameValuePair>();
	System.out.println(path);
	System.out.println(postUserNameKey);
	System.out.println(postUserNameValue);
	System.out.println(postPasswordKey);
	System.out.println(postPasswordValue);
}
//此标注表示测试的实体数据,也就是要测试的具体内容
@Test
public void testRegister(){
	params.add(new BasicNameValuePair(postUserNameKey,postUserNameValue));
	params.add(new BasicNameValuePair(postPasswordKey, postPasswordValue));
	try {
		uefEntity = new UrlEncodedFormEntity(params,"utf-8");
		httpPost.setEntity(uefEntity);
		response = httpClient.execute(httpPost);
		HttpEntity entity = response.getEntity();
		if(entity != null){
			String str = EntityUtils.toString(entity,"utf-8");
			System.out.println(str);
		}
	} catch (UnsupportedEncodingException e) {
		// TODO Auto-generated}catch blocke.printStackTrace();
	} catch (ClientProtocolException e) {
		// TODO Auto-generated catch blocke.printStackTrace();
	} catch (IOException e) {
		// TODO Auto-generated catch blocke.printStackTrace();
			}
		}
	}
}
//此标注一般用于关闭流,释放资源的操作,有点像finally里面的一些执行,在每个test单利的最后执行
@After
public void setDown(){
	try {
		response.close();
		httpClient.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

 

 

junit4+HttpClient的自动化测试说明:

 

junit的方式目前还做不到完美的代码分离,当然是相比testNg,我在百度上看到过有说junit不支持集成测试的,于是决定发这个博客,其实junit的功能也足够满足日常测试,只是很多junit的功能我们并没有用上,我最近是由于公司业务需要测试接口,就研究了下,感觉还是收获挺多;

junit支持打包测试,参数化测试,我以上代码就用了junit的参数化测试功能,相结合HttpClient进行自动化测试,但是有点不足,就是硬编码,但是在junit中也是无奈之举,目前junit只能将测试参数写在代码中,以上我尽可能的分离测试参数,想通过xml分离测试参数,但是难免不了又会导dom4j一大堆的东西,于是我放弃这个组合的分离测试参数,如果有喜欢测试的朋友可以看看TestNg的测试,毕竟TestNg也是参考junit出的,介于junit3 junit4之间,好处就是测试参数的分离做的挺好,以上纯粹个人理解

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值