在编写接口用例的过程中,针对一个接口,往往只是参数不同,那么参数化就非常有必要了。
seldom 中参数化的用法非常灵活,这里仅介绍@file_data()
的N种玩法。
二维列表
当参数比较简单时可以试试下面的方式。
- 参数化数据
{"add_guest": [["参数错误", "", "", 10021, "parameter error"],["查询为空", "tom", "13711001100", 10022, "event id null"],]
}
- 测试用例
import seldom
from seldom import file_dataclass 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_dataclass 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_dataclass 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)
通过json编写用例
虽然不推荐用文件写用例,你想写也是可以的。
- 测试用例文件
{"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_dataclass 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":passif __name__ == "__main__":seldom.main(base_url="http://127.0.0.1:8000", debug=True)
最后的这种写法不是seldom推荐的,假如要测试的接口足够简单,设计一个测试方法来处理简单的接口也未尝不可。当然,往往实际项目中的用例并不足够简单。回到代码的方式编写才能足够个兼具灵活性和易用性。
最后感谢每一个认真阅读我文章的人,礼尚往来总是要有的,虽然不是什么很值钱的东西,如果你用得到的话可以直接拿走:
这些资料,对于【软件测试】的朋友来说应该是最全面最完整的备战仓库,这个仓库也陪伴上万个测试工程师们走过最艰难的路程,希望也能帮助到你!