import unittest
import json
import requests
from requests.auth import HTTPBasicAuth
#开启鉴权,所以要先导入这个
class JenkinPostCase(unittest.TestCase):
def setUp(self):
self.build_job_url = 'http://localhost:8080/jenkins/job/check_python_version/build'
self.disable_job_url = 'http://localhost:8080/jenkins/job/check_python_version/disable'
self.job_url = 'http://localhost:8080/jenkins/job/check_python_version/api/json'
def tearDown(self):
print ('the end')
def test_build_job(self):
r = requests.post(self.build_job_url,data={},auth=('admin','admin'))
print (r.status.code)
#打印返回码,20*代表成功
self.assertEqual(r.status.code,201)
def test_disable_job(self):
#job_info = requests.get(self.job_url,auth=('admin','admin')).json()
#self.assertTrue(job_info['buildable'])
#检查是否为true状态
status = get_job_status()
self.assertTrue(status)
r = requests.post(self.disable_job_url,data={},auth=('admin','admin'))
print (r.status.code)
self.assertEqual(r.status.code,200)
#job_info = requests.get(self.job_url,auth=('admin','admin')).json()
#self.assertFalse(job_info['buildable'])
status = self.get_job_status()
self.assertFalse(status)
#如果有重复调用的语句,建议直接写成函数,以免下次多处修复
def get_job_status(self):
job_info = requests.get(self.job_url,auth =('admin','admin')).json
return job_info['disable']
if __name__ == '__main':
unittest.main()