我有一些功能foo(list)
我希望
assert foo(list with one element) == that one element
assert foo(list with 100 elements) == foo(list)
如何在不知道列表本身是什么的情况下执行此操作,理想情况下只使用断言语句?
答案 0 :(得分:0)
if len(x) == 1:
assert foo(x) == x[0]
elif len(x) == 100:
assert foo(x) == x
或者,如果您希望确定foo
的返回值始终具有上述属性,则可以执行以下操作:
def foo(x):
# compute return value `retval` based on `x`
if len(x) == 1:
assert retval == x[0]
elif len(x) == 100:
assert retval == x
return retval