我有一个验证模型类的函数,它必须在字符串的情况下检查类的每个成员是否为非空或非空。
这个is_complete
函数的逻辑是这样的:
def is_complete(profile):
if profile.first_name in (None, ''):
return False
elif profile.last_name in (None, ''):
return False
elif profile.dob is None:
return False
.
.
.# and checks so on for all members of the profile instance
.
.
return True
我的问题是,由于执行可以采用的可能路径数量非常大,并且profile
与要检查的成员变量的数量成比例增加,那么如何可靠地为所有可能的路径编写测试?
现在,我有两个简单的测试用例:
assertFalse(is_complete(foo))
assertTrue(is_complete(foo))
但我有一种感觉,这可能还不够。
我不确定你说的MxN
路径是什么意思。在发布的代码中,您拥有与字段 + 1 一样多的路径。
创建一个辅助方法,该方法创建一个profile
完整且通过的is_complete
。
添加一个测试方法来验证is_complete(profile)
是否True
完整profile
。
为每个字段添加一个测试方法,步骤如下:
is_complete
is_complete
返回False
您将拥有与字段 + 1 一样多的测试方法。
顺便说一句,而不是这个:
if profile.first_name in (None, ''):
你可以写得更简单:
if not profile.first_name:
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句