为什么我们需要API接口?API接口的核心又是什么?

Posted Cris20230328

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了为什么我们需要API接口?API接口的核心又是什么?相关的知识,希望对你有一定的参考价值。

 

 

 

 API(Application Programming Interface)是一种连接不同软件之间的标准化的接口,可以让不同软件间进行数据交互和通信。API接口的作用很多,以下是几个主要的原因:

1.提高软件系统的灵活性和可扩展性。API接口可以将不同的模块分离开来,使得系统更加模块化,便于后续的扩展和更新,降低了代码耦合性。

2.提高开发效率。API接口可以让开发者只关注自己负责的部分,而不用考虑整个系统的实现。这样可以大大降低开发时间,使产品更快地上线。

3.降低成本。API接口的个性化和灵活性可以降低组件成本,提升系统可维护性和扩展性。由于API接口是标准化的,不同厂商可以按照统一的标准进行接口开发,不必关注其他模块。

4.促进创新。API接口可以让不同软件之间的数据更加容易地共享和交流,从而推动创新和业务模式的变革。

总之,API接口在软件开发中具有非常重要的作用,可以快速连接各种不同的软件和设备,降低开发成本,提高系统的灵活性和可扩展性,促进产品和技术的创新。

举个大家熟知的电商平台API接口调试的例子:

 

 

 

请求地址:获》取》免》费》调》用》测》试

 


	"item": 
		"num_iid": "652874751412",
		"title": "北欧轻奢布艺沙发 小户型简约现代客厅ins风网红款三双人订制沙发",
		"desc_short": "",
		"price": 480,
		"total_price": "",
		"suggestive_price": "",
		"orginal_price": 480,
		"nick": "惜情yqq1127",
		"num": 200,
		"detail_url": "https://item.taobao.com/item.htm?id=652874751412",
		"pic_url": "//gd3.alicdn.com/imgextra/i4/2568161054/O1CN01aYBriY1Jem9UDtt9e_!!2568161054.jpg",
		"brand": null,
		"brandId": "",
		"rootCatId": "",
		"cid": 50020632,
		"desc": "<div > \\n   <div >\\n    <img src=\\"http://img.alicdn.com/imgextra/i3/2568161054/O1CN01LFmSOU1Jem9QOjMPb_!!2568161054.jpg\\"  />\\n    <img src=\\"http://img.alicdn.com/imgextra/i3/2568161054/O1CN014vyOOT1Jem9DpHz3Y_!!2568161054.jpg\\"  />\\n    <img src=\\"http://img.alicdn.com/imgextra/i1/2568161054/O1CN01B3PpsA1Jem9N8V7uf_!!2568161054.jpg\\"  />\\n    <img src=\\"http://img.alicdn.com/imgextra/i2/2568161054/O1CN015JbyeY1Jem9MZshUt_!!2568161054.jpg\\"  />\\n    <img src=\\"http://img.alicdn.com/imgextra/i1/2568161054/O1CN01HXSoxx1Jem9RvgzHN_!!2568161054.jpg\\"  />\\n    <img src=\\"http://img.alicdn.com/imgextra/i3/2568161054/O1CN01IEultA1Jem9MdEx8R_!!2568161054.jpg\\"  />\\n    <img src=\\"http://img.alicdn.com/imgextra/i3/2568161054/O1CN0176K98O1Jem9QOjE69_!!2568161054.jpg\\"  />\\n    <img src=\\"http://img.alicdn.com/imgextra/i4/2568161054/O1CN013Pxp1O1Jem9RvgeTv_!!2568161054.jpg\\"  />\\n    <img src=\\"http://img.alicdn.com/imgextra/i1/2568161054/O1CN01SfyZ8M1Jem9QOi1Gx_!!2568161054.jpg\\"  />\\n    <img src=\\"http://img.alicdn.com/imgextra/i4/2568161054/O1CN01bb1POa1Jem9Sdgve2_!!2568161054.jpg\\"  />\\n    <img src=\\"http://img.alicdn.com/imgextra/i3/2568161054/O1CN018Eo9dV1Jem9KV0y79_!!2568161054.jpg\\"  />\\n    <img src=\\"http://img.alicdn.com/imgextra/i1/2568161054/O1CN01vuEofr1Jem9Nzy9xY_!!2568161054.jpg\\"  />\\n    <img src=\\"http://img.alicdn.com/imgextra/i4/2568161054/O1CN01qw9sAi1Jem8wkNKpy_!!2568161054.jpg\\"  />\\n    <img src=\\"http://img.alicdn.com/imgextra/i1/2568161054/O1CN01HeFhFw1Jem8rLnjBY_!!2568161054.jpg\\"  />\\n    <img src=\\"http://img.alicdn.com/imgextra/i2/2568161054/O1CN01SNgjoi1Jem9QOil15_!!2568161054.jpg\\"  />\\n    <img src=\\"http://img.alicdn.com/imgextra/i2/2568161054/O1CN01RXf3RA1Jem9DpHVwj_!!2568161054.jpg\\"  />\\n    <img src=\\"http://img.alicdn.com/imgextra/i4/2568161054/O1CN01gZmZjt1Jem9ISThgm_!!2568161054.jpg\\"  />\\n    <img src=\\"http://img.alicdn.com/imgextra/i2/2568161054/O1CN01YL0FHM1Jem9PQTjX9_!!2568161054.jpg\\"  />\\n    <img src=\\"http://img.alicdn.com/imgextra/i4/2568161054/O1CN01UhsEhZ1Jem8yvJIhZ_!!2568161054.jpg\\"  />\\n   </div> \\n  </div><img src=\\"https://www.o0b.cn/i.php?t.png&rid=gw-3.647444429c73e&p=1778787678&k=i_key&t=1685341255\\" style=\\"display:none\\" />",
		"item_imgs": [
			
				"url": "//gd3.alicdn.com/imgextra/i4/2568161054/O1CN01aYBriY1Jem9UDtt9e_!!2568161054.jpg"
			,
			
				"url": "//gd3.alicdn.com/imgextra/i3/2568161054/O1CN01kjOfNb1Jem9DmWn8Y_!!2568161054.jpg"
			,
			
				"url": "//gd1.alicdn.com/imgextra/i1/2568161054/O1CN01HoB9ha1Jem9DmWn8r_!!2568161054.jpg"
			,
			
				"url": "//gd4.alicdn.com/imgextra/i4/2568161054/O1CN011PjP2P1Jem9MXEUFT_!!2568161054.jpg"
			,
			
				"url": "//gd3.alicdn.com/imgextra/i3/2568161054/O1CN01KUfBFL1Jem9KTTMn1_!!2568161054.jpg"
			
		],
		"item_weight": "",
		"post_fee": "",
		"freight": "",
		"express_fee": "",
		"ems_fee": "",
		"shipping_to": "",
		"video": 
			"url": null
		,
		"sample_id": "",
		"props_name": "31480:14306495906:几人坐:脚踏90*60*48cm;31480:14306495907:几人坐:双人165*95*67cm;31480:14306495908:几人坐:三人210*95*67cm;31480:14306495909:几人坐:单人100*95*67cm;31480:21480914361:几人坐:四人位240*95*67cm;31480:21480914362:几人坐:大四人320*95*76cm;31480:1387571900:几人坐:3米贵妃沙发;31480:32527954:几人坐:定制尺寸;1627207:28321:颜色分类:乳白色 尺寸颜色可定制;1627207:28321:颜色分类:乳白色 尺寸颜色可定制;1627207:28321:颜色分类:乳白色 尺寸颜色可定制;1627207:28321:颜色分类:乳白色 尺寸颜色可定制;1627207:28321:颜色分类:乳白色 尺寸颜色可定制;1627207:28321:颜色分类:乳白色 尺寸颜色可定制;1627207:28321:颜色分类:乳白色 尺寸颜色可定制;1627207:28321:颜色分类:乳白色 尺寸颜色可定制",
		"prop_imgs": 
			"prop_img": [
				
					"properties": "1627207:28321",
					"url": "//gd3.alicdn.com/imgextra/i1/2568161054/O1CN017GTZ4h1Jem9Qra1ap_!!2568161054.jpg"
				
			]
		,
		"props_imgs": 
			"prop_img": [
				
					"properties": "1627207:28321",
					"url": "//gd3.alicdn.com/imgextra/i1/2568161054/O1CN017GTZ4h1Jem9Qra1ap_!!2568161054.jpg"
				
			]
		,
		"property_alias": "",
		"props": [
			
				"name": "品牌",
				"value": "#0 工厂"
			,
			
				"name": "型号",
				"value": "520"
			,
			
				"name": "材质",
				"value": "木"
			,
			
				"name": "木质材质",
				"value": "松木"
			,
			
				"name": "面料",
				"value": "绒布"
			,
			
				"name": "风格",
				"value": "北欧"
			,
			
				"name": "几人坐",
				"value": "脚踏90*60*48cm 双人165*95*67cm 三人210*95*67cm 单人100*95*67cm 四人位240*95*67cm 大四人320*95*76cm 3米贵妃沙发 定制尺寸"
			,
			
				"name": "颜色分类",
				"value": "乳白色"
			,
			
				"name": "填充物",
				"value": "海绵"
			,
			
				"name": "结构工艺",
				"value": "木质工艺"
			,
			
				"name": "否可定制",
				"value": "是"
			,
			
				"name": "沙发组合形式",
				"value": "U形"
			,
			
				"name": "否可拆洗",
				"value": "是"
			,
			
				"name": "适用对象",
				"value": "成年人"
			,
			
				"name": "是带储物空间",
				"value": "否"
			,
			
				"name": "产地",
				"value": "上海"
			,
			
				"name": "地市",
				"value": "上海市"
			,
			
				"name": "区县",
				"value": "奉贤区"
			,
			
				"name": "是组装",
				"value": "否"
			,
			
				"name": "出租车是可运输",
				"value": "否"
			,
			
				"name": "填充物硬度",
				"value": "软"
			,
			
				"name": "款式定位",
				"value": "经济型"
			
		],
		"total_sold": "-1",
		"skus": 
			"sku": [
				
					"price": 480,
					"total_price": 0,
					"orginal_price": 480,
					"properties": "31480:14306495906;1627207:28321",
					"properties_name": "31480:14306495906:几人坐:脚踏90*60*48cm;1627207:28321:颜色分类:乳白色 尺寸颜色可定制",
					"quantity": 200,
					"sku_id": "4881047531343"
				,
				
					"price": 1688,
					"total_price": 0,
					"orginal_price": 1688,
					"properties": "31480:14306495907;1627207:28321",
					"properties_name": "31480:14306495907:几人坐:双人165*95*67cm;1627207:28321:颜色分类:乳白色 尺寸颜色可定制",
					"quantity": 147,
					"sku_id": "4881047531344"
				,
				
					"price": 2088,
					"total_price": 0,
					"orginal_price": 2088,
					"properties": "31480:14306495908;1627207:28321",
					"properties_name": "31480:14306495908:几人坐:三人210*95*67cm;1627207:28321:颜色分类:乳白色 尺寸颜色可定制",
					"quantity": 200,
					"sku_id": "4881047531345"
				,
				
					"price": 968,
					"total_price": 0,
					"orginal_price": 968,
					"properties": "31480:14306495909;1627207:28321",
					"properties_name": "31480:14306495909:几人坐:单人100*95*67cm;1627207:28321:颜色分类:乳白色 尺寸颜色可定制",
					"quantity": 200,
					"sku_id": "4881047531346"
				,
				
					"price": 2388,
					"total_price": 0,
					"orginal_price": 2388,
					"properties": "31480:21480914361;1627207:28321",
					"properties_name": "31480:21480914361:几人坐:四人位240*95*67cm;1627207:28321:颜色分类:乳白色 尺寸颜色可定制",
					"quantity": 200,
					"sku_id": "5039985183001"
				,
				
					"price": 3188,
					"total_price": 0,
					"orginal_price": 3188,
					"properties": "31480:21480914362;1627207:28321",
					"properties_name": "31480:21480914362:几人坐:大四人320*95*76cm;1627207:28321:颜色分类:乳白色 尺寸颜色可定制",
					"quantity": 200,
					"sku_id": "5039985183002"
				,
				
					"price": 3400,
					"total_price": 0,
					"orginal_price": 3400,
					"properties": "31480:1387571900;1627207:28321",
					"properties_name": "31480:1387571900:几人坐:3米贵妃沙发;1627207:28321:颜色分类:乳白色 尺寸颜色可定制",
					"quantity": 200,
					"sku_id": "5039984824000"
				,
				
					"price": 3000,
					"total_price": 0,
					"orginal_price": 3000,
					"properties": "31480:32527954;1627207:28321",
					"properties_name": "31480:32527954:几人坐:定制尺寸;1627207:28321:颜色分类:乳白色 尺寸颜色可定制",
					"quantity": 200,
					"sku_id": "5039985183003"
				
			]
		,
		"seller_id": "2568161054",
		"sales": 0,
		"shop_id": "567158267",
		"props_list": 
			"31480:14306495906": "几人坐:脚踏90*60*48cm",
			"31480:14306495907": "几人坐:双人165*95*67cm",
			"31480:14306495908": "几人坐:三人210*95*67cm",
			"31480:14306495909": "几人坐:单人100*95*67cm",
			"31480:21480914361": "几人坐:四人位240*95*67cm",
			"31480:21480914362": "几人坐:大四人320*95*76cm",
			"31480:1387571900": "几人坐:3米贵妃沙发",
			"31480:32527954": "几人坐:定制尺寸",
			"1627207:28321": "颜色分类:乳白色 尺寸颜色可定制"
		,
		"seller_info": 
			"nick": "惜情yqq1127",
			"item_score": 4.97561,
			"score_p": 4.98049,
			"delivery_score": 4.96585,
			"shop_type": "",
			"user_num_id": "2568161054",
			"sid": "567158267",
			"title": "",
			"zhuy": "https://shop567158267.taobao.com",
			"cert": null,
			"open_time": "",
			"credit_score": null,
			"shop_name": "现代布艺沙发"
		,
		"tmall": false,
		"error": "",
		"location": "江苏南通",
		"data_from": "Ha",
		"has_discount": "false",
		"is_promotion": "false",
		"promo_type": null,
		"props_img": 
			"1627207:28321": "//gd3.alicdn.com/imgextra/i1/2568161054/O1CN017GTZ4h1Jem9Qra1ap_!!2568161054.jpg"
		,
		"format_check": "ok",
		"desc_img": [
			"http://img.alicdn.com/imgextra/i3/2568161054/O1CN01LFmSOU1Jem9QOjMPb_!!2568161054.jpg",
			"http://img.alicdn.com/imgextra/i3/2568161054/O1CN014vyOOT1Jem9DpHz3Y_!!2568161054.jpg",
			"http://img.alicdn.com/imgextra/i1/2568161054/O1CN01B3PpsA1Jem9N8V7uf_!!2568161054.jpg",
			"http://img.alicdn.com/imgextra/i2/2568161054/O1CN015JbyeY1Jem9MZshUt_!!2568161054.jpg",
			"http://img.alicdn.com/imgextra/i1/2568161054/O1CN01HXSoxx1Jem9RvgzHN_!!2568161054.jpg",
			"http://img.alicdn.com/imgextra/i3/2568161054/O1CN01IEultA1Jem9MdEx8R_!!2568161054.jpg",
			"http://img.alicdn.com/imgextra/i3/2568161054/O1CN0176K98O1Jem9QOjE69_!!2568161054.jpg",
			"http://img.alicdn.com/imgextra/i4/2568161054/O1CN013Pxp1O1Jem9RvgeTv_!!2568161054.jpg",
			"http://img.alicdn.com/imgextra/i1/2568161054/O1CN01SfyZ8M1Jem9QOi1Gx_!!2568161054.jpg",
			"http://img.alicdn.com/imgextra/i4/2568161054/O1CN01bb1POa1Jem9Sdgve2_!!2568161054.jpg",
			"http://img.alicdn.com/imgextra/i3/2568161054/O1CN018Eo9dV1Jem9KV0y79_!!2568161054.jpg",
			"http://img.alicdn.com/imgextra/i1/2568161054/O1CN01vuEofr1Jem9Nzy9xY_!!2568161054.jpg",
			"http://img.alicdn.com/imgextra/i4/2568161054/O1CN01qw9sAi1Jem8wkNKpy_!!2568161054.jpg",
			"http://img.alicdn.com/imgextra/i1/2568161054/O1CN01HeFhFw1Jem8rLnjBY_!!2568161054.jpg",
			"http://img.alicdn.com/imgextra/i2/2568161054/O1CN01SNgjoi1Jem9QOil15_!!2568161054.jpg",
			"http://img.alicdn.com/imgextra/i2/2568161054/O1CN01RXf3RA1Jem9DpHVwj_!!2568161054.jpg",
			"http://img.alicdn.com/imgextra/i4/2568161054/O1CN01gZmZjt1Jem9ISThgm_!!2568161054.jpg",
			"http://img.alicdn.com/imgextra/i2/2568161054/O1CN01YL0FHM1Jem9PQTjX9_!!2568161054.jpg",
			"http://img.alicdn.com/imgextra/i4/2568161054/O1CN01UhsEhZ1Jem8yvJIhZ_!!2568161054.jpg"
		],
		"shop_item": [],
		"relate_items": []
	,
	"error": "",
	"secache": "4ad7ad2480af253fec9c2fd4daa266bb",
	"secache_time": 1685341255,
	"secache_date": "2023-05-29 14:20:55",
	"translate_status": "",
	"translate_time": 0,
	"language": 
		"default_lang": "cn",
		"current_lang": "cn"
	,
	"reason": "",
	"error_code": "0000",
	"cache": 0,
	"api_info": "today:80 max:10100 all[178=80+55+43];expires:2030-12-31",
	"execution_time": "5.263",
	"server_time": "Beijing/2023-05-29 14:20:55",
	"client_ip": "106.6.37.94",
	"call_args": 
		"num_iid": "652874751412",
		"is_promotion": "1"
	,
	"api_type": "taobao",
	"translate_language": "zh-CN",
	"translate_engine": "baidu",
	"server_memory": "6.12MB",
	"request_id": "gw-3.647444429c73e",
	"last_id": "1770397869"

API接口的核心是提供标准化的数据交换方式,让不同的应用程序可以互相交互。具体来说,API接口的核心包括以下几个方面:

1.标准化的接口规范。API接口需要遵循一定的标准化规范,包括接口格式、协议、数据格式等。只有使用相同的规范,不同的应用程序才能有效地进行数据交换。

2.数据安全和授权管理。API接口的开发者需要考虑数据安全和授权管理,以确保数据不会被非法访问或篡改。API接口可以通过使用安全协议,如HTTPS协议,并且只有经过授权的用户才能访问API接口,以防止数据泄露或盗用。

3.灵活的数据交互方式。API接口需要支持不同的数据交互方式,如同步或异步,从而满足不同应用程序的需要。同时,API接口可以添加可选的参数或定制数据返回格式,从而满足更加复杂的应用需求。

4.系统的可扩展性。API接口需要具备良好的可扩展性,以便应对日益增长的数据交换需求。API接口可以通过增加数据传输带宽、优化调用逻辑、增强支持容错等方式,以满足大规模应用场景的需求。

综上所述,API接口的核心在于提供标准化的规范和安全的数据交换方式,并充分考虑不同应用需求的灵活性和系统的可扩展性。

接口测试-Postman VS SoapUI

其实市面上做接口测试的工具很多,为啥挑这两个来讲解了,重点是真心好用。好了,废话不多说,直接上干货。

相信有一定了解的人都知道这两个工具应用最广泛的就是接口测试,既然提到接口测试那我们不得不先普及下什么是接口,接口测试又是啥?

我们常说的接口一般指下面两种:

  1. API:应用程序编程接口。程序间的接口

  2. GUI:图形用户界面。人与程序的接口

我们这里说的接口测试主要指API接口测试,API接口分类一般有如下几种:

  • HTTP 接口

  • Webservice 接口

  • RESTful 接口

HTTP,RESTful 接口走 HTTP 协议,通过路径来区分调用的方法,请求报文入参有多种形式,返回报文一般为 json 串,最常见的是 get 和 post 方法。

WebService 接口是走 soap 协议,请求报文和返回报文都是 xml 格式。

而Postman和SopaUI支持的接口类型如下:

因此,我们需要先辨别项目前后端是用何种接口交互再来选取相应的接口测试工具。

接口测试又是啥?

接口测试就是模拟客户端向服务器发送请求报文,服务器接收请求报文后对相应的报文做处理并向客户端返回应答,客户端再接收应答然后对应答做一些校验的过程。

下面我们分别介绍下如何用PostMan和SoapUI做接口自动化测试。

A: Postman + Newman + Jenkins 实现接口自动化测试

1.安装Postman,编写API接口测试用例

示例:豆瓣公开查找书籍接口

2.导出Collection(项目-接口用例),安装NewMan,用NewMan Command运行Collection并输出HTML报告。

C:\\Users\\Li.Shi\\AppData\\Roaming\\npm\\newman run C:\\Users\\Li.Shi\\Desktop\\PostMan\\LiShiTest.postman_collection.json --reporters cli,json --reporters cli,html --reporter-html-export htmlOut.html

3.安装部署Jenkins,其中Jenkins的配置如下:

至此,我们可完成基于postman和Jenkins的自动化接口测试。

B:SoapUI+UnitTest 实现接口自动化测试

1.安装SoapUI,自行创建一个可运行的SoapUI的Project,得到项目XML文件.eg:DeviceReportService-soapui-project.xml

2.用VS(Visual Studio)创建一个Unit Test Project.添加reference,Check SystemSystem.ConfigurationSystem.CoreSystem.Data

3.添加app config文件,指定soapUI TestRunner.exe所在路径.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="SoapUIHome" value="C:\\Program Files\\SmartBear\\SoapUI-5.3.0\\bin"/>
  </appSettings>
</configuration>
View Code

4.添加SoapUIRunner公共类,通过新建Process去调用TestRunner.exe命令进而运行SoapUI的case.

using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SoapUI
{
    public class SoapUIRunner
    {
        public void RunSoapUItest(string soapProject,string testSuiteName,string testName,string report,string set)
        {
            const string fileName = "cmd.exe";
            var soapProjectFileName = Path.GetFullPath(soapProject);

            var arguments = string.Format("/C testrunner.bat -s\\"{0}\\" -c\\"{1}\\" \\"{2}\\" -r -a -f\\"{3}\\" -t\\"{4}\\" ", testSuiteName, testName, soapProjectFileName, report, set);
            var soapHome = System.Configuration.ConfigurationManager.AppSettings["SoapUIHome"];
            //start a process and hook up the in/output
            var process = new Process
            {
                StartInfo = new ProcessStartInfo
                {
                    FileName = fileName,
                    Arguments = arguments,
                    WorkingDirectory = soapHome,
                    Verb = "runas",
                    CreateNoWindow = true,
                    ErrorDialog = false,
                    RedirectStandardError= true,
                    RedirectStandardOutput = true,
                    UseShellExecute = false

                },
                EnableRaisingEvents = true
            };

            //pipe the output to console.writeline
            process.OutputDataReceived += (sender, args) =>
              Console.WriteLine(args.Data);
            var errorBuilder = new StringBuilder();

            //store the errors in a stringbuilder
            process.ErrorDataReceived += (sender, args) =>
            {
                if (args != null && args.Data != null)
                {
                    errorBuilder.AppendLine(args.Data);
                }
            };

            process.Start();
            process.BeginOutputReadLine();
            process.BeginErrorReadLine();
            //wait for soapUI to finish
            process.WaitForExit();

            //fail the test if anything fails
            var errorMessage = errorBuilder.ToString();
            if(!string.IsNullOrEmpty(errorMessage))
            {
                Assert.Fail("Test with name \'{0}\' failed. {1} {2}", testName, Environment.NewLine, errorMessage);
            }
        }
    }
}
View Code

5.通过Unit Test调用SoapUI TestSuit, 进而可以运用bat命令集成运行TestCase. 做到接口的自动化测试。

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace SoapUI
{
    [TestClass]
    [DeploymentItem(@"soapUIFiles\\DeviceReportService-soapui-project.xml", "TestData\\\\soapUIFiles")]
    public class DeviceReport:SoapUIRunner
    {
        private string testCaseSuiteName = "BasicHttpBinding_DeviceReport TestSuite";
        private string soapProjectFile= @"TestData\\\\soapUIFiles\\\\DeviceReportService-soapui-project.xml";
        private string reportFile = @"C:\\Users\\" + Environment.UserName + "\\\\Desktop\\\\TestReport";
        private String SoapUISettingsFile = @"TestData\\\\soapUIFiles\\\\soapui-settings.xml";
        private TestContext testContext;

        public TestContext TestContext
        {
            get { return this.testContext; }
            set { this.testContext = value; }
        }

        [TestMethod]
        [TestCategory("DeviceReport")]
        public void Device_Report()
        {
            RunSoapUItest(soapProjectFile, testCaseSuiteName, "DeviceReport TestCase", reportFile, SoapUISettingsFile);
        }
    }
}
View Code

接下来我们对比下用方式A和B做接口自动化的区别:

1.从上面的实现来看,SoapUI自动化需要测试人员有一定的编码能力,想比如Postman会对测试人员要求高些。

2.从两种工具用例组织方式来看:

SoapUI的组织方式如下图,最上层是WorkSpace,所以每个WorkSpace中可以打开多个Project,一个Project也可以在不同的WorkSpace中。

Project对应我们的测试项目,其中可添加WSDL、WADL资源、TestSuite以及MockService。

TestSuite对应我们的测试模块,比如商户中心,其中可以添加TestCase,TestCase对应我们对某个模块的不同接口,比如订单管理接口。而一个接口可以能需要多个Step完成,变量、数据源、请求等都是一个Step。

Postman功能上更简单,组织方式也更轻量级,它主要针对的就是单个的HTTP请求。Collection就相当于是Project,而Collection中可以创建不定层级的Folders,可以自己组织TestSuite。每个Request可以当做是一个TestCase或者Step:

3. 从长期的团队协作来看:

SoapUI:本身一个project是一个xml文件,但是可以通过配置变成一系列文件夹,每个Case、每个Suite均是独立的文件,这样可通过svn/git/TFS进行团队协作。支持性较好。

Postman:有团队协作的功能,需要付费。

 因此从项目支持的接口类型,不同集成测试需求和后期维护来考虑,我们可以根据上面几点选择适合自己项目的接口自动化工具。

以上是关于为什么我们需要API接口?API接口的核心又是什么?的主要内容,如果未能解决你的问题,请参考以下文章

什么是API接口,SDK又是什么,两者有什么关系

什么是接口测试?什么是API?

接口测试-Postman VS SoapUI

/api/contracts/overview/contractId要怎么请求接口

API接口入门(二):API接口的签名验签和加解密原理

什么叫WINDOWS API ?