python接口测试(三)
引入unittest框架
get请求
#coding: utf-8
import unittest
import requests
import JSON
class TestGet(unittest.TestCase):
def setUp(self):
self.test_url = 'Http://localhost:8080/jenkins/api/json?tree=jobs[name]'
def test_get(self):
self.r = requests.get(self.test_url)
'''
result = self.r.text
json_result = json.loads(result) #反序列化过程
'''
json_result = self.r.json() #反序列化等同于上面注释代码
print json_result
self.assertEquals(json_result['jobs'][0]['name'],'check_python_version')
if __name__ == "__mian__":
unittest.mian()
post请求
#coding: utf-8
import unittest
import requests
import json
class TestPost(unittest.TestCase):
def setUp(self):
self.test_url = 'http://localhost:8080/jenkins/job/check_Python_version/build'
def test_post(self):
self.r = requests.post(self.test_url, data={}, auth=('admin', '123456'))
print self.r.status_code
self.assertEqual(self.r.status_code, 200)
if __name__ == "__mian__":
unittest.mian()
相关文章