From 483e2fcf41adcdddad5543c04d3ad42c60def334 Mon Sep 17 00:00:00 2001 From: chenjiankun Date: Tue, 17 Jan 2017 15:40:17 +0000 Subject: Add unittest framework for Yardstick API JIRA: YARDSTICK-538 Currently it is hard to test API, So I add a base class as flask document do. In this framework I will mock a temp sqlite database and a server. Change-Id: If881233cb22655617c07ad018201b8ee08492d06 Signed-off-by: chenjiankun --- tests/unit/apiserver/__init__.py | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 tests/unit/apiserver/__init__.py (limited to 'tests/unit/apiserver/__init__.py') diff --git a/tests/unit/apiserver/__init__.py b/tests/unit/apiserver/__init__.py new file mode 100644 index 000000000..021415296 --- /dev/null +++ b/tests/unit/apiserver/__init__.py @@ -0,0 +1,35 @@ +from __future__ import absolute_import + +import os +import unittest +import tempfile + +from oslo_serialization import jsonutils + +from yardstick.common import constants as consts + + +class APITestCase(unittest.TestCase): + + def setUp(self): + self.db_fd, self.db_path = tempfile.mkstemp() + consts.SQLITE = 'sqlite:///{}'.format(self.db_path) + from api import server + + server.app.config['TESTING'] = True + self.app = server.app.test_client() + + server.init_db() + + def tearDown(self): + os.close(self.db_fd) + os.unlink(self.db_path) + + def _post(self, url, data): + headers = {'Content-Type': 'application/json'} + resp = self.app.post(url, data=jsonutils.dumps(data), headers=headers) + return jsonutils.loads(resp.data) + + def _get(self, url): + resp = self.app.get(url) + return jsonutils.loads(resp.data) -- cgit 1.2.3-korg