I am very new to programming and I'm writing some code that collects information from the user and adds it to a spreadsheet. Here's the basis of what I'm trying to test:
# 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()
So I want my test to be able to loop through adding multiple details and letters, but for some reason it's only running through the while loops once (I know because I added a couple flags to make sure). Something even more confusing is that it only acknowledges the second runs and not the first. Here's my test code:
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
So I'm expecting to see both "Detail #1" (and a cell with "AB") and "Detail #2" added to the spreadsheet, but it only has "Detail #2" on there when I run the test. This tells me it's probably not an issue of overwriting on the spreadsheet because they are input into different columns.
If I only run it for the first detail then I'd expect to see "Detail #1" and a cell with "AB" on the spreadsheet, but it only has "Detail #1" and a cell with "B".
This only happens when using pytest - if I test manually, the code is running fine. The problem is I ask for a lot more input than this and it's a huge waste of time to test manually. Any insight into what I'm missing?