Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Pytest: Mocking random numbers

from app import Game

game = Game()
while True:
    user_guess = input("Guess a number: ")
    if user_guess == "x":
        break
    if user_guess == "s":
        print(game.hidden)
        continue
    user_guess = int(user_guess)
    response = game.guess(user_guess)
    if response == 'match':
        print("matched")
        break
    print(response)
import app

def test_game(monkeypatch):
    monkeypatch.setattr(app.random, 'randint', lambda x, y: 70)
    game = app.Game()
    print(game.hidden)
    response = game.guess(100)
    assert response == 'too big'

    response = game.guess(50)
    assert response == 'too small'

    response = game.guess(70)
    assert response == 'match'