Last active
February 6, 2025 16:19
-
-
Save GenevieveBuckley/efd16862de9e2fe7adfd2bf2bef93e02 to your computer and use it in GitHub Desktop.
Monkeypatching user input with pytest
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from io import StringIO | |
def double(): | |
x = input("Enter an integer: ") | |
return int(x) * 2 | |
def adding(): | |
x = float(input('Enter the first number')) | |
y = float(input('Enter the second number')) | |
return x + y | |
def test_double(monkeypatch): | |
number_inputs = StringIO('1234\n') | |
monkeypatch.setattr('sys.stdin', number_inputs) | |
assert double() == 2468 | |
def test_adding(monkeypatch): | |
number_inputs = StringIO('2\n3\n') | |
monkeypatch.setattr('sys.stdin', number_inputs) | |
assert adding() == 5 |
π―
Great solution!
Thanks for sharing! It's much appreciated! This is the best solution for me for now.
ππΌππΌ Sooooo simple and useful. Thank you so much for sharing ππΌ π
Who said code can't be art :) Nice one!
Nice solution, thanks
Nice, clean and simple. Thank you.
This is great π
Thank you!
Excellent, a simple solution to my problem!
Thank you!! Great call!
Thank you, pragmatic solution...
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Neat solution, thanks for sharing.