python-3.x - 如何在 python 中编写更好的验证/测试用例
问题描述
有一个对象很少有属性。我为测试用例编写了一个函数,用于检查属性是否满足。如果不满足该属性,该函数应抛出异常。
但是,我认为有更好的方法来编写这些测试用例。对象属性的子集如下:
x['polygon'] 是 >= 3 个整数 (x,y) 对的列表,以顺时针或逆时针顺序表示多边形的角。
当前功能如下:
def validate_object(x):
"""This function validates an object x that is supposed to represent an object
inside an image, and throws an exception on failure.
Specifically it is checking that:
x['polygon'] is a list of >= 3 integer (x,y) pairs representing the corners
of the polygon in clockwise or anticlockwise order.
"""
if type(x) != dict:
raise ValueError('dict type input required.')
if 'polygon' not in x:
raise ValueError('polygon object required.')
if not isinstance(x['polygon'], (list,)):
raise ValueError('list type polygon object required.')
points_list = x['polygon']
if len(points_list) < 3:
raise ValueError('More than two points required.')
for x, y in points_list:
if type(x) != int or type(y) != int:
raise ValueError('integer (x,y) pairs required.')
return
如果有人可以建议编写这些测试用例的更好方法,那将非常有帮助。
解决方案
推荐阅读
- javascript - 正则表达式通过两个特殊字符查找常量文字保护... Javascript
- c# - 使用数据流调用最小起订量验证方法
- c# - 编辑 RichTextBox Run 元素而不拆分为多个 Run 元素
- javascript - 具有相同 ID 的对象被推入相同的数组
- java - 在具有不同数据类型的java中初始化二维数组
- html - 如何重新合并合并的单元格?
- python - 机器学习:计算训练和验证错误、不同数量的特征和应用交叉验证
- python - Pandas Dataframe 用逗号在括号内显示结果
- php - 当phpmailer(我使用ISPConfig)从PHP发送邮件时出现ERR_TIMED_OUT
- javascript - 如何在 Twitter 导航上复制悬停效果阴影