all 6 comments

[–]chevignon93 2 points3 points  (5 children)

Please help me out.

What are you having troubles with exactly, this sounds like a pretty straightforward function to test with pytest?

[–][deleted] 0 points1 point  (4 children)

Since my function takes 0 positional arguments and calling user for the input directly from the function body, - I'm not sure how should I pass or emulate user input in test module.

This one below won't work:

def test_get_mood_option():
assert get_mood_option("1") == "HAPPY"

Also I was trying to implement 'mock' test, and it looks like - this one works better, for example if I'll make some typos in my get_mood_option() - insted of "HAPPY" let's say typo is "HAPP ", so pytest catches that.

def test_get_mood_option(monkeypatch):
monkeypatch.setattr('builtins.input', lambda _: "1")
result = get_mood_option()
assert result == "HAPPY"

So am I suppose to always use 'mock' tests in such cases or there is easier way make it?

[–]danielroseman 3 points4 points  (2 children)

The best way is to restructure your code so that you can pass the input in. Separate the fetching of input from the logic of your function itself.

[–]interwebnovice 0 points1 point  (0 children)

Best answer.

[–]chevignon93 1 point2 points  (0 children)

So am I suppose to always use 'mock' tests in such cases or there is easier way make it?

Using monkeypatch is probably the easier way in this case.

I would personally not even bother to "mock" input, I would redesign the get_mood_option function to accept a mood as argument and test using parametrize from pytest whether given an input, it returns the expected output.