工作需要,要寫 API 相關的 test cases。目前只有先測 API interface 的部份,寫著寫著發現我根本在造重複的輪子,幾乎在做複製貼上跟填空的工作,程式碼大概長的像下面這樣:
1 |
class Foo_0000:<br /> “”“<br /> POST /login/token<br /> ““”<br /><br /> def __init__(self):<br /> self.REST_PATH = “/login/token”<br /> self.headers = {“content-type”: “application/json”}<br /><br /> self.parse_response = {{<br /> 200: self.parse_response_200,<br /> 400: self.parse_response_400,<br /> 500: self.parse_response_500<br /> }}<br /><br /> def post_api(self, payload):<br /> payload = json.dumps(payload)<br /> r = requests.post(CONFIG.HOST + self.REST_PATH,<br /> data=payload,<br /> headers=self.headers)<br /><br /> return r<br /><br /> def parse_response_200(self, payload, r):<br /> if self.expect_status_code != 200:<br /> return False, “Response expect %d, got: %s, payload: %s” % (self.expect_status_code, r.status_code, payload)<br /><br /> resp = r.json()<br /> ...<br /> ...<br /><br /> return True, “”<br /><br /> def parse_response_400(self, payload, resp):<br /> if self.expect_status_code != 400:<br /> return False, “Response expect %d, got: %s, payload %s” % (self.expect_status_code, resp.status_code, payload)<br /><br /> return True, “”<br /><br /> def parse_response_500(self, payload, resp):<br /> if self.expect_status_code != 500:<br /> return False, “Response expect %d, got: %s, payload %s” % (self.expect_status_code, resp.status_code, payload)<br /><br /> return True, “”<br /><br /> @start_tunnel<br /> def run(self):<br /> for k, v in self.data.items():<br /> payload = self.data[k][“payload”]<br /> self.expect_status_code = self.data[k][“result”][“expect_status_code”]<br /> self.expect_response = self.data[k][“result”][“expect_response”]<br /><br /> r = self.post_api(payload)<br /> try:<br /> result, text = self.parse_response[r.status_code](payload, r)<br /> if result is False:<br /> self.result += (text + ” test code: %s” % (k))<br /> except Exception as e:<br /> traceback.print_exc()<br /> return False, str(e)<br /><br /> if self.result:<br /> return False, self.result<br /><br /> return True, “”<br /> |
這時候才停下工作,去查有沒有辦法做到 「auto generate auto test cases」的方式。Google 了一下發現這個「Model-Based Testing」的關鍵字,發現這應該滿有用的。
Automated Test Case Generation and Execution from Models from Dharmalingam Ganesan
- Model-Based testing
- Model-based testing tools
- How to Make the Most of Model-based Testing – Atos
- Design and Implementation of a Model-Driven Approach for Restful APIs
- Model-driven Testing of RESTful APIs
- Towards Property-Based Testing of RESTful Web Services
目前是實作 offline MBT,也就是說,會先把 test cases generate 出來,然後在帶到 runner 上去跑測試。現在公司是用 pi-tester 作為 API testing 的 framework。這套 framework 是 dkent 完成,用 python 做出來的一套輕便 API testing framework。
Leave a Reply