标签:headers ade edit head get book ati .json ==
上篇已经用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 = 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 = self.json_data, headers = self.headers) re= results.json() self.assertEqual(results.status_code, 200) self.assertEqual(len(re), 2) self.assertEqual(re[0][‘fruit‘], ‘apple‘) self.assertEqual(re[0][‘computer‘], ‘lenvo‘) self.assertEqual(re[1][‘mobile‘], ‘iphone‘) self.assertEqual(re[1][‘book‘], ‘testing‘) #测试put接口 def test_edit_case(self): results = requests.put(self.url(‘edit‘), data = self.json_data, headers = self.headers) re= results.json() self.assertEqual(results.status_code, 200) self.assertEqual(re[‘success‘], ‘true‘) #测试delete接口 def test_delete_case(self): results = requests.delete(self.url(‘delete‘)) re= results.json() self.assertEqual(results.status_code, 200) self.assertEqual(re[‘success‘], ‘true‘) def url(self, path): return self.demian + path if __name__ == ‘__main__‘: unittest.main()
运行结果:
标签:headers ade edit head get book ati .json ==
原文地址:http://www.cnblogs.com/waylon/p/6697126.html