代码改变世界

java+maven+testng+poi实现接口自动化

2020-08-11 17:08  HQS_blog  阅读(327)  评论(0编辑  收藏  举报

1. maven中先引入testng的包

<dependency>
  <groupId>org.testng</groupId>
  <artifactId>testng</artifactId>
  <version>6.14.3</version>
  <scope>test</scope>
</dependency>

2.安装testng插件     eclipse下载插件参考http://www.mamicode.com/info-detail-2224104.html

3.右击项目或者包--testng--create testng class  (安装完成就会有testng选项),选择需要的注解,设置testng xml配置文件

 

4.配置如下

<?xml version="1.0" encoding="UTF-8"?>
<suite name="Suite" parallel="false">
<test name="Test">
<classes>
<class name="com.javaTestest.casetest.test1"/>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->

 


5.测试的链接:

http://apis.juhe.cn/simpleWeather/query?city="苏州"&key=

代码:

package com.javaTestest.casetest;

import java.util.List;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class test1 {
	
	private String execlPath = "E:\\tools\\Python\\test\\update2.xlsx";
	

//	public static void main(String[] args) {
//		post();
//	}
	
	@Test(dataProvider = "getDatas")
	private void testng1(Object[] datas1) {
		post(datas1);
	}
	
	
	//数据提供者
	@DataProvider
	private Object[] getDatas(){
		List<NameValuePair[]> nvpList = Utils.read(execlPath);
		Object[] datas = nvpList.toArray();
		return datas;
	}
	
	private  void post(Object[] datas1) {
		HttpClient httpClient = new HttpClient();
		PostMethod postMethod = new PostMethod("http://apis.juhe.cn/simpleWeather/query");
//		NameValuePair[] datastest = {new NameValuePair("city","苏州"),new NameValuePair("key","")};
//		System.out.println(datastest);
		System.out.println(datas1.toString());
		NameValuePair[] datas = (NameValuePair[])datas1;
		
		postMethod.setRequestBody(datas);
		try {
			httpClient.executeMethod(postMethod);
			String string = new String(postMethod.getResponseBody(),"utf-8");
			int statusCode = postMethod.getStatusCode();
			System.out.println(statusCode);
			System.out.println(string);

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		
		
	}
	
	@BeforeTest
	private void beforetestng(){
		System.out.println("==========@BeforeTest===========");
	}
	@AfterTest
	private void aftertestng(){
		System.out.println("==========@AfterTest===========");
		
	}

}

  

package com.javaTestest.casetest;

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

import org.apache.commons.httpclient.NameValuePair;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;

public class Utils {	
	
	public static List<NameValuePair[]> read(String path) {
		List<NameValuePair[]> list = new ArrayList<NameValuePair[]>();
		try {
			Workbook wb = WorkbookFactory.create(new File(path));
               //获取需要的sheet 下标0表示第一个sheet Sheet sheetAt = wb.getSheetAt(0);
               //获得此sheet的总行数 int endRow = sheetAt.getPhysicalNumberOfRows(); for (int rowNum = 1; rowNum < endRow; rowNum++) { Row row = sheetAt.getRow(rowNum); // String city = row.getCell(2) == null?"":row.getCell(2).setCellType(CellType.STRING).getStringCellValue(); // cellCity.setCellType(CellType.STRING);
                    //如果获取execl中的值为string类型,可以统一用 .toString 数字1读取变成1.0的情况可以设置 数据-分列-下一步到第三步选择文本 如图: String city = row.getCell(2) == null?"":row.getCell(2).toString(); String key = row.getCell(3) == null?"":row.getCell(3).toString(); System.out.println(rowNum+"========"+city+"============="+key); if("" == city){ continue; } NameValuePair[] nvp = {new NameValuePair("city",city),new NameValuePair("key",key)}; list.add(nvp); } // sheetAt.getRow(arg0); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return list; } }

  

5.1  @DataProvider 为数据提供者,返回的数据类型必须与@Test方法接收的类型一致
     @Test(dataProvider = "getDatas") dataProvider不设置默认就是@DataProvider的方法名,想设置名字的话就@DataProvider(name="dp") 那么@Test(dataProvider = "dp")

传入多组数据,会对应执行多次@Test中的方法:
     @DataProvider 方法出参是List,@Test方法会将List中的值遍历去执行测试方法

5.2 使用poi来读取execl
  引入poi的pom:

  <dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>3.17</version>
  </dependency>

  

数字1读取变成1.0的情况可以设置 数据-分列-下一步到第三步选择文本 如图: