Skip to content

Instantly share code, notes, and snippets.

@GenevieveBuckley
Last active December 12, 2023 18:57
Show Gist options
  • Star 24 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GenevieveBuckley/efd16862de9e2fe7adfd2bf2bef93e02 to your computer and use it in GitHub Desktop.
Save GenevieveBuckley/efd16862de9e2fe7adfd2bf2bef93e02 to your computer and use it in GitHub Desktop.
Monkeypatching user input with pytest
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
@KhalidCK
Copy link

Neat solution, thanks for sharing.

@alexcreek
Copy link

πŸ’―

@brucestull
Copy link

Great solution!

Thanks for sharing! It's much appreciated! This is the best solution for me for now.

@Elena-GHub
Copy link

πŸ‘πŸΌπŸ‘πŸΌ Sooooo simple and useful. Thank you so much for sharing πŸ‘πŸΌ 😁

@bruskiza
Copy link

bruskiza commented Feb 9, 2022

Who said code can't be art :) Nice one!

@nasimhc
Copy link

nasimhc commented Aug 24, 2022

Nice solution, thanks

@Bazzaware
Copy link

Nice, clean and simple. Thank you.

@ascrookes
Copy link

This is great πŸŽ‰

@terencebarrett
Copy link

Thank you!

@ZathrasXI
Copy link

Excellent, a simple solution to my problem!

@nathannovaes
Copy link

Thank you!! Great call!

@morenohernan
Copy link

Thank you, pragmatic solution...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment