Nosetests with input taking functions

Hi everyone,
I have just arrived at exercise 52 and in order to wrap things up I first wanted to write a super simple engine myself (already quite some time ago since I worked on these problems and I wanted to make sure I understand the basics).

What I cannot figure out is how I would write a Nosetest function to test the ‘input_check’ function in my engine that awaits a user input in the middle of it?
I saw how you load a function and pass on a variable at the beginning but is it possible to test a function like mine using Nosetests?

Let me know if you have questions in case I was not clear enough.
Many many thanks for any help in advance.
Stephan

My Engine:

class Engine(object):

def __init__(self):
    self.current_scene = central_corridor
    self.final_scene = generic_death
    self.action =""

def play(self):

    while self.current_scene != self.final_scene:
        print(self.current_scene.description)
        self.input_check()
        next_scene = self.current_scene.paths.get(self.action)
        self.current_scene = next_scene
    
    print(generic_death.description)

def input_check(self):
    path_list_dict = []
    path_list_keys = []

    for item in self.current_scene.paths:
        path_list_dict.append(item)
    
    self.action = input("> ")

    if self.action in path_list_dict:
        return self.action
    else:
        self.input_check()

Have you checked out mocking input with the mock library and side_effects?

https://docs.python.org/3/library/unittest.mock.html

1 Like

Hi @gpkesley
Thanks so much for your response! very much appreciated!

I did some research and actually found something that solves the problem.
You are right ‘mock’ is the keyword in that regards.
A different article in this forum help a lot as well: Testing input() and print()

Many thanks again for your help!

2 Likes
A free service run by Zed A. Shaw for learncodethehardway.org.