【发布时间】:2022-01-05 17:40:27
【问题描述】:
我对编程很陌生,我正在编写一些代码来收集用户的信息并将其添加到电子表格中。这是我要测试的基础:
# let's say this file is called collect.py
def infoBreakdown():
userData = input("Enter a detail about your info: ")
# add detail to a new column
assignLetter = input("Want to assign a letter to this detail? (y/n) ")
addLetter = False
if assignLetter == "y":
addLetter = True
while addLetter:
newLetter = input("Enter a letter to assign to this detail: ")
# add letter to a single cell within that column
# each additional letter adds to the same cell
newLetter = input("Want to assign another letter for this detail?: ")
if newLetter == "n":
addLetter = False
def main():
infoBreakdown()
addInfo = True
while addInfo:
newInfo = input("Cool! Want to add more info? (y/n) ")
if newInfo == "y":
infoBreakdown()
else:
addInfo = False
main()
所以我希望我的测试能够循环添加多个细节和字母,但由于某种原因,它只运行一次 while 循环(我知道是因为我添加了几个标志来确保)。更令人困惑的是它只承认 第二次 运行而不是第一次运行。这是我的测试代码:
def input_main(prompt):
inputRequest = {
"Enter a detail about your info: ": "Detail #1",
"Want to assign a letter to this detail? (y/n) ":, "y",
"Enter a letter to assign to this detail: ":, "A",
"Want to assign another letter for this detail? (y/n) ":, "y",
"Enter a letter to assign to this detail: ":, "B",
"Want to assign another letter for this detail? (y/n) ":, "n",
"Cool! Want to add more info? (y/n) ": "y",
"Enter a detail about your info: ": "Detail #2",
"Want to assign a letter to this detail? (y/n) ":, "n",
"Cool! Want to add more info? (y/n) ": "n"
}
return inputRequest[prompt]
def test_main(monkeypatch):
monkeypatch.setattr('builtins.input', input_main)
assert collect.main() == None
所以我希望在电子表格中同时看到“Detail #1”(以及带有“AB”的单元格)和“Detail #2”,但是当我运行时它只有“Detail #2”考试。这告诉我这可能不是覆盖电子表格的问题,因为它们被输入到不同的列中。
如果我只针对第一个细节运行它,那么我希望在电子表格上看到“Detail #1”和一个带有“AB”的单元格,但它只有“Detail #1”和一个带有“B”的单元格”。
这仅在使用 pytest 时发生 - 如果我手动测试,代码运行良好。问题是我要求的输入比这多得多,手动测试非常浪费时间。对我所缺少的有任何见解吗?
【问题讨论】:
标签: python testing while-loop pytest monkeypatching