Having a Python functions as below:
JavaScript
x
12
12
1
def get_student_id():
2
while True:
3
try:
4
print("ENTER STUDENT ID: ")
5
identity = int(input())
6
if identity > 0:
7
return identity
8
else:
9
print("That's not a natural number. Try again: ")
10
except ValueError:
11
print("That's not an integer. Try again: ")
12
and
JavaScript
1
4
1
def test_get_student_id():
2
with mock.patch.object(builtins, 'input', lambda _: '19'):
3
assert get_student_id() == '19'
4
Run pytest command to receive an error: TypeError: test_GetStudentId..() missing 1 required positional argument: ‘_’
Please help to fix above error. Thanks.
Advertisement
Answer
When calling input you’re passing no arguments (input()
), but you’re telling unittest.mock that it has one (lambda _:
).
You need to be consistent. Either:
Pass one argument when calling it
JavaScript121identity = int(input("ENTER STUDENT ID: ")
2
Instruct unittest.mock that function is called with no arguments
JavaScript121mock.patch.object(builtins, 'input', lambda: '19')
2
From my PoV, the former makes more sense, as you could also get rid of the print statement before.