1、添加线程组
2、添加http请求
3、为线程组添加察看结果树
4、写入接口参数并运行
5、在查看结果树窗口查看结果
6、多组数据可增加CSVDataSetConfig(添加.csv格式的文件,并在参数值里以${x}格式写入)
此时变量值填写${变量名},上图x,y表示每次从文件里读取两个参数,分别命名为x,y
1、GET请求接口测试
1 public void TestGet throws URISyntaxException, ClientProtocolException, IOException{ 2 //1、创建一个客户端对象 3 CloseableHttpClient client=HttpClients.createDefault(); 4 //2、使用URIBuilder()来生成一个get类型的USI 5 URI uri=new URIBuilder().setScheme("http") 6 .setPort(8080) 7 .setHost("localhost") 8 .setPath("/test1334/Calc") 9 .setParameter("a", "2")10 .setParameter("b", "3").build();11 //3、新建一个httpget类型请求对象,并将uri传入请求12 HttpGet get=new HttpGet(uri);13 //4、新建响应对象,用于接收客户端执行get结果14 CloseableHttpResponse response=client.execute(get);15 //5.从响应对象中提取实际结果,与预期结果进行比对16 if(response.getStatusLine().getStatusCode()==200){17 System.out.println(EntityUtils.toString(response.getEntity()));18 }19 }
2、POST请求接口测试
样例(测一个输入两个参数求和的接口):
1 public void TestPOST () throws ClientProtocolException, IOException{ 2 //1.新建一个客户端对象 3 CloseableHttpClient client=HttpClients.createDefault(); 4 //2.新建post类型请求对象,并传入uri 5 HttpPost post = new HttpPost("http://172.31.6.155:8080/test1334/Calc"); 6 //3.使用NameValuePair对参数进行打包 7 List list=new ArrayList(); 8 list.add(new BasicNameValuePair("a","1")); 9 list.add(new BasicNameValuePair("b","2"));10 //4.对打包好的参数,使用UrlEncodedFormEntity工具类生成实体类型数据11 //Consts.UTF_8设置服务器字符集类型12 UrlEncodedFormEntity entity=new UrlEncodedFormEntity(list,Consts.UTF_8);13 //5.将含有请求参数的实体对象放入到post请求对象里14 post.setEntity(entity);15 //6.新建一个响应对象接收客户端执行post请求的结果16 CloseableHttpResponse response=client.execute(post);17 //7.从响应对象中提取实际结果,与预期结果进行比对18 if(response.getStatusLine().getStatusCode()==200){19 System.out.println(EntityUtils.toString(response.getEntity()));20 }21 }
3、自动化框架
1 @RunWith(Feeder.class) 2 public class getParameter { 3 @Test 4 @Source("data/datas.csv") //数据源 5 public void test_get(int x,int y,int expect) throws ClientProtocolException, URISyntaxException, IOException{//expect为预期结果,用于与实际结果进行比对 6 TestRESTfultest=new TestRESTful();//TestRESTful为前边创建TestGet所属类 7 int returns=test.TestGet(x, y);//此处的为修改后的TestGet,添加了参数和返回值; 8 assertEquals(returns,expect); //将结果与预期进行比较 9 }10 }
1、新建项目
2、输入WSDL地址或文件
3、修改“?”内的数据
4、开始测试
1、GET请求接口测试
1 public int testGet(int x, int y) throws RemoteException { 2 String target = "http://172.31.6.94:8080/axis2/services/calc?wsdl";//传入地址 3 //创建一个CalcStub对象 4 CalcStub stub = new CalcStub(target); 5 CalcStub.Add add = new CalcStub.Add(); 6 //传入参数 7 add.setX(x); 8 add.setY(y); 9 AddResponse response = stub.add(add);//结果10 int result = response.get_return();11 return result;12 }
2、POST请求接口测试
1 public static void testPOST(int a,int b) throws ClientProtocolException, IOException{ 2 //创建客户端对象 3 CloseableHttpClient cli=HttpClients.createDefault(); 4 HttpPost po=new HttpPost("http://172.31.6.61:8080/axis2/services/MyService?wsdl"); 5 //将soap协议内容添加进来,即soapXML字符串 6 String soapXML="" 7 +" " 8 +"" 9 +""10 +""+a+" "11 +""+b+" "12 +" "13 +" "14 +"";15 //将String转换成实体类型16 StringEntity entity=new StringEntity(soapXML,Charset.forName("UTF-8"));17 po.setEntity(entity);18 CloseableHttpResponse re=cli.execute(po);19 System.out.println((re.getEntity()).toString()); 20 }
感谢每一个认真阅读我文章的人,看着粉丝一路的上涨和关注,礼尚往来总是要有的,虽然不是什么很值钱的东西,如果你用得到的话可以直接免费拿走:
① 2000多本软件测试电子书(主流和经典的书籍应该都有了)
② 软件测试/自动化测试标准库资料(最全中文版)
③ 项目源码(四五十个有趣且经典的练手项目及源码)
④ Python编程语言、API接口自动化测试、web自动化测试、App自动化测试(适合小白学习)
这一些资料,对做【软件测试】的朋友而言应该是较为完整了,这类学习资料也陪伴我走过了最艰难的路程,希望也可以帮助到你!万事要尽早,尤其是技术行业,一定要提升技术功底。
以上软件测试资料需要的可以私信我关键词(资料)免费获取
留言与评论(共有 0 条评论) “” |