在pycharm进行单元测试(unittest python)
在Edit Configuration中添加Python test 选中相应的脚本或者文件夹
# coding:utf-8 import unittest import requests from config import BASE_URL from config import SC_OK, SC_INTERNAL_SERVER_ERROR DEBUG = True # 镜像 class Iamge(object): def __init__(self): self.ct_user_id = "b4186147a58a2188e9e2e21c99" self.os_id = "a477c664559311e79de10242ac110002" self.disk_size = 30 self.uuid = "af6276a7-1984-4e88-8630-5f4e384b95c2" self.data_center = 1 self.os_type = "1" self.login_name = "admin" self.name = "win2008-20170512" self.os_name = "win2008" self.share = 1 # 镜像查询列表 def images_list(self): URL_ = BASE_URL + "/api/images/list/" payload = {"ct_user_id": self.ct_user_id, "os_id": self.os_id} operation_return = requests.get(URL_, params=payload, headers=dict(Referer=URL_)) print "*********** return status is " + str(operation_return.status_code) print "*********** return type is " + str(operation_return.headers['content-type']) print "*********** content is \n" + str(operation_return.content) return operation_return # 查询某个镜像 def query_images(self): URL_ = BASE_URL + "/api/images/query/" payload = {"ct_user_id": self.ct_user_id, "os_id": self.os_id, "share": self.share, "name": self.name} operation_return = requests.get(URL_, params=payload, headers=dict(Referer=URL_)) print "*********** return status is " + str(operation_return.status_code) print "*********** return type is " + str(operation_return.headers['content-type']) print "*********** content is \n" + str(operation_return.content) return operation_return # 创建镜像 def create_images(self): URL_ = BASE_URL + "/api/images/create/" payload = {"ct_user_id": self.ct_user_id, "os_id": self.os_id, "disk_size": self.disk_size, "uuid": self.uuid, "data_center": self.data_center, "os_type": self.os_type, "login_name": self.login_name, "name": self.name, "os_name": self.os_name} operation_return = requests.post(URL_, data=payload, headers=dict(Referer=URL_)) print "*********** return status is " + str(operation_return.status_code) print "*********** return type is " + str(operation_return.headers['content-type']) print "*********** content is \n" + str(operation_return.content) return operation_return # 更新镜像 def update_images(self): URL_ = BASE_URL + "/api/images/update/" payload = {"ct_user_id": self.ct_user_id, "os_id": self.os_id, "id": 5, "name": self.name} operation_return = requests.post(URL_, data=payload, headers=dict(Referer=URL_)) print "*********** return status is " + str(operation_return.status_code) print "*********** return type is " + str(operation_return.headers['content-type']) print "*********** content is \n" + str(operation_return.content) return operation_return # 删除镜像 def delete_images(self): URL_ = BASE_URL + "/api/images/delete/" payload = {"ct_user_id": self.ct_user_id, "os_id": self.os_id, "id": 5} operation_return = requests.post(URL_, data=payload, headers=dict(Referer=URL_)) print "*********** return status is " + str(operation_return.status_code) print "*********** return type is " + str(operation_return.headers['content-type']) print "*********** content is \n" + str(operation_return.content) return operation_return image = Iamge() class TestCaseImage(unittest.TestCase): def setUp(self): pass def tearDown(self): pass # 获取镜像列表 if DEBUG: def testImageList(self): images_list_status_code = image.images_list().status_code self.assertEqual(images_list_status_code, SC_OK) # 获取某个镜像 if DEBUG: def testImageQuery(self): query_images_status_code = image.query_images().status_code self.assertEqual(query_images_status_code, SC_OK) # 创建镜像 if DEBUG: def testImageCreate(self): create_images_status_code = image.create_images().status_code self.assertEqual(create_images_status_code, SC_OK) # 更新镜像 if DEBUG: def testImageUpdate(self): update_images_status_code = image.update_images().status_code self.assertEqual(update_images_status_code, SC_OK) # 删除镜像 if DEBUG: def testImageDelete(self): delete_images_status_code = image.delete_images().status_code self.assertEqual(delete_images_status_code, SC_OK) if __name__ == "__main__": unittest.main() # run all tests
kingrain送给读者的话:Everthing isn't getting along well,your efforts paid off!