簡體   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