引入unittest框架
get请求
代码语言:javascript复制#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请求
代码语言:javascript复制#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()