![](/img/trans.png)
[英]How do I resolve this? TypeError: main.<locals>.<lambda>() missing 1 required positional argument: 'gamepanel'
[英]TypeError: test_custom_function.<locals>.<lambda>() missing 1 required positional argument: '_'
具有 Python 功能如下:
def get_student_id():
while True:
try:
print("ENTER STUDENT ID: ")
identity = int(input())
if identity > 0:
return identity
else:
print("That's not a natural number. Try again: ")
except ValueError:
print("That's not an integer. Try again: ")
和
def test_get_student_id():
with mock.patch.object(builtins, 'input', lambda _: '19'):
assert get_student_id() == '19'
运行 pytest 命令接收错误:TypeError: test_GetStudentId..() missing 1 required positional argument: '_'
请帮助解决上述错误。 谢谢。
调用input时,您没有传递 arguments ( input()
),但您告诉unittest.mock它有一个( lambda _:
)。
你需要保持一致。 要么:
调用时传递一个参数
identity = int(input("ENTER STUDENT ID: ")
指示unittest.mock在没有 arguments 的情况下调用 function
mock.patch.object(builtins, 'input', lambda: '19')
从我的PoV来看,前者更有意义,因为您之前也可以摆脱print语句。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.