不确定这是否可能,但我想验证我的函数是否正确显示用户输入。我在整个测试套件中使用 caplog 来验证警告和错误消息是否正确记录,但不知道如何利用它来测试用户的简单提示。
def test_select_org_exact_prompt(atconn, monkeypatch, caplog):
monkeypatch.setattr("builtins.input", lambda x: "1\n") # Select the first organization
atconn.select_org()
assert atconn.organization == "default"
#Retrieve the captured logs from caplog
logs = caplog.records
print("Captured logs:\n", logs)
# Assert that the prompt for selecting the organization was logged
assert "Please choose an organization:" in logs
这是我尝试的,但日志是空的,大概是因为它不是警告或错误。使用 caplog 是否可以做到这一点,还是我需要使用其他东西?
caplog
固定装置仅捕获使用 stdlib logging
模块进行的调用。
因此像
logger.info(..)
这样的东西会被捕获,但 print(..)
不会被捕获。不幸的是,在这方面input(..)
就像print(..)
。
但是,
capsys
夹具正是您所需要的!