作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我参与的一个项目使用 testbook测试 Jupyter 笔记本的代码单元。 Patching工作正常 — 除非要测试的代码要求用户输入 input()
.我只是不知道如何正确修补它。
使用的版本:Python:3.8.10,测试书:0.4.2
要在 Jupyter 代码单元中测试的代码,标记为 name_checking
:
def fix(text):
return text.strip().title()
def check(text):
return len(text) > 1
firstname = input("What's your first name?")
lastname = input("What's your last name?")
fixed_first = fix(firstname)
fixed_last = fix(lastname)
if check(fixed_first) and check(fixed_last):
print(f"Your name is {fixed_first} {fixed_last}.")
else:
print("You entered an invalid name.")
尝试 1:测试代码补丁
builtins.input
@testbook(path)
def test_name_checking1(tb): # execute cell tagged "name_checking"
with tb.patch("builtins.input") as mock_input:
mock_input.return_value = [" haRrY ", " pOtter "]
result = tb.execute_cell("name_checking")
assert tb.cell_output_text("name_checking") == "Harry"
这失败了:
E 8
E ----> 9 firstname = input("What's your first name?")
E 10 lastname = input("What's your last name?")
E 11
E
E ~/proj/.venv/lib/python3.8/site-packages/ipykernel/kernelbase.py in raw_input(self, prompt)
E 976 """
E 977 if not self._allow_stdin:
E --> 978 raise StdinNotImplementedError(
E 979 "raw_input was called, but this frontend does not support input requests."
E 980 )
E
E StdinNotImplementedError: raw_input was called, but this frontend does not support input requests.
E StdinNotImplementedError: raw_input was called, but this frontend does not support input requests.
../.venv/lib/python3.8/site-packages/testbook/client.py:135: TestbookRuntimeError
尝试 2:测试代码补丁
ipykernel.kernelbase.Kernel.raw_input
@testbook(path)
def test_name_checking2(tb):
with tb.patch("ipykernel.kernelbase.Kernel.raw_input") as mock_input:
mock_input.return_value = [" haRrY ", " pOtter "]
result = tb.execute_cell("name_checking")
firstname = tb.ref("firstname")
assert tb.cell_output_text("name_checking") == "Harry"
这失败了:
tb = <testbook.client.TestbookNotebookClient object at 0x7f74aaca2af0>
@testbook(path)
def test_name_checking2(tb): # execute cell tagged "name_checking"
with tb.patch("ipykernel.kernelbase.Kernel.raw_input") as mock_input:
mock_input.return_value = [" haRrY ", " pOtter "]
result = tb.execute_cell("name_checking")
firstname = tb.ref("firstname")
print(firstname)
> assert tb.cell_output_text("name_checking") == "Harry"
E AssertionError: assert 'You entered an invalid name.' == 'Harry'
E - Harry
E + You entered an invalid name.
03-functions/tests/test_name_checking.py:33: AssertionError
----------------------------------------------------------------------------------------------------------------- Captured stdout call ------------------------------------------------------------------------------------------------------------------
"<MagicMock name='raw_input()' id='139650606478576'>"
有没有人知道必须修补什么或如何正确修补
input()
?
最佳答案
我通过深入研究 testbook 的代码并了解有关修补的更多信息找到了解决方案:
ipykernel.kernelbase.Kernel.raw_input
是正确的方法input()
的返回值使用 side_effect
参数,而不是 mock_input.return_value
@testbook(path)
def test_name_checking(tb):
with tb.patch(
"ipykernel.kernelbase.Kernel.raw_input",
side_effect=[" haRrY ", " pOtter "],
) as mock_input:
tb.execute_cell("name_checking")
assert tb.cell_output_text("name_checking") == "Your name is Harry Potter."
关于python - 使用 testbook 测试 Jupyter notebook 单元时如何修补 input()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69014966/
我参与的一个项目使用 testbook测试 Jupyter 笔记本的代码单元。 Patching工作正常 — 除非要测试的代码要求用户输入 input() .我只是不知道如何正确修补它。 使用的版本:
我正在尝试创建允许用户输入这本书的java程序数字和程序将显示所选书籍的价格。我有创建了书籍类,但大多数没有语法错误可能存在逻辑错误并且 TestBook 类有错误在调用输入时。你能帮助我吗?我迷路了
我是一名优秀的程序员,十分优秀!