在编写接口用例的过程中,针对一个接口,往往只是参数不同,那么参数化就非常有必要了。
seldom 中参数化的用法非常灵活,这里仅介绍@file_data() 的N种玩法。
当参数比较简单时可以试试下面的方式。
参数化数据
{ "add_guest": [ ["参数错误", "", "", 10021, "parameter error"], ["查询为空", "tom", "13711001100", 10022, "event id null"], ] }
测试用例
import seldom from seldom import file_data class AddGuestTest(seldom.TestCase): @file_data("add_guest.json", key="add_guest") def test_case(self, _, real_name, phone, status, msg): payload = {"realname": real_name, "phone": phone} self.post("/api/add_guest/", data=payload) self.assertStatusCode(200) self.assertPath("status", status) self.assertPath("message", msg) if __name__ == "__main__": seldom.main(base_url="http://127.0.0.1:8000", debug=True)
当参数很多时,不便于阅读,可以通过字典的方式定义。
参数化数据
{ "add_guest": [ { "name": "参数错误", "real_name": "", "phone": "", "status": 10021, "msg": "parameter error" }, { "name": "查询为空", "real_name": "tom", "phone": "13711001100", "status": 10022, "msg": "event id null" }, ] }
测试用例
import seldom from seldom import file_data class AddGuestTest(seldom.TestCase): @file_data("add_guest.json", key="add_guest") def test_case(self, _, real_name, phone, status, msg): payload = {"realname": real_name, "phone": phone} self.post("/api/add_guest/", data=payload) self.assertStatusCode(200) self.assertPath("status", status) self.assertPath("message", msg) if __name__ == "__main__": seldom.main(base_url="http://127.0.0.1:8000", debug=True)
我们可以将接口的入参、出参进一步拆分。
参数化数据
{ "add_guest": [ { "name": "参数错误", "req": { "real_name": "", "phone": "" }, "resp": { "status": 10021, "msg": "parameter error" } }, { "name": "查询为空", "req": { "real_name": "tom", "phone": "13711001100" }, "resp": { "status": 10022, "msg": "event id null" } } ] }
测试用例
import seldom from seldom import file_data class AddGuestTest(seldom.TestCase): @file_data("add_guest.json", key="add_guest") def test_case(self, _, req, resp): payload = {"realname": req["real_name"], "phone": req["phone"]} self.post("/api/add_guest/", data=payload) self.assertStatusCode(200) self.assertPath("status", resp["status"]) self.assertPath("message", resp["msg"]) if __name__ == "__main__": seldom.main(base_url="http://127.0.0.1:8000", debug=True)
虽然不推荐用文件写用例,你想写也是可以的。
测试用例文件
{ "cases": [ { "name": "参数错误", "method": "post", "url": "/api/add_guest/", "req": { "eid": "", "real_name": "", "phone": "" }, "resp": { "status": 10021, "msg": "parameter error" } }, { "name": "查询为空", "method": "post", "url": "/api/add_guest/", "req": { "eid": 901, "real_name": "tom", "phone": "13711001100" }, "resp": { "status": 10022, "msg": "event id null" } } ] }
解析测试用例
import seldom from seldom import file_data class SampleCaseTest(seldom.TestCase): @file_data("test_case.json", key="cases") def test_case(self, _, method, url, req, resp): if method == "post": payload = {"eid": req["eid"], "realname": req["real_name"], "phone": req["phone"]} self.post(url, data=payload) self.assertStatusCode(200) self.assertPath("status", resp["status"]) self.assertPath("message", resp["msg"]) elif method == "get": pass if __name__ == "__main__": seldom.main(base_url="http://127.0.0.1:8000", debug=True)
最后的这种写法不是seldom推荐的,假如要测试的接口足够简单,设计一个测试方法来处理简单的接口也未尝不可。当然,往往实际项目中的用例并不足够简单。回到代码的方式编写才能足够个兼具灵活性和易用性。
作者:虫师
原文链接:https://www.cnblogs.com/fnng/p/15906956.html