繁体   English   中英

类型错误:test_custom_function。<locals> .<lambda> () 缺少 1 个必需的位置参数:'_'</lambda></locals>

[英]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 _: )。
你需要保持一致。 要么

  1. 调用时传递一个参数

    identity = int(input("ENTER STUDENT ID: ")
  2. 指示unittest.mock在没有 arguments 的情况下调用 function

     mock.patch.object(builtins, 'input', lambda: '19')

从我的PoV来看,前者更有意义,因为您之前也可以摆脱print语句。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM