首页 > 代码库 > python接口测试

python接口测试

上篇已经用mock server配置了接口。

python代码编写接口测试,主要运用了unittest、requests两个框架

import json
import unittest, requests


class getCase(unittest.TestCase):

	#测试用例之前执行
	def setUp(self):
		print("testing start")
		self.demian = "http://localhost:12306/"
		self.headers = {‘content-type‘ :‘application/json‘}
		self.json_data = http://www.mamicode.com/json.dumps({"new" : "QQ","old" : "taobao"})

	#测试用例执行完之后执行
	def tearDown(self):
		print("testing engding")

		#测试get接口
	def test_get_case(self):
		results = requests.get(self.url(‘gets‘)).json()
		self.assertEqual(len(results), 2)

		self.assertEqual(results[0][‘title‘], ‘java‘)
		self.assertEqual(results[0][‘version‘], ‘1.8.0‘)

		self.assertEqual(results[1][‘title‘], ‘python‘)
		self.assertEqual(results[1][‘version‘], ‘3.5‘)

		#测试post接口
	def test_post_case(self):
		results = requests.post(self.url(‘posts‘), data = http://www.mamicode.com/self.json_data, headers = self.headers)>

 运行结果:

技术分享

python接口测试