def test_validate_guess(capfd): game = Game() game._answer = 2 # use capfd to look at and test the stdout assert not game._validate_guess(1) out, error = capfd.readouterr() assert out.rstrip() == '1 is too low' assert not game._validate_guess(3) assert game._validate_guess(2)
def test_validate_guess(capfd): game = Game() game._answer = 2 assert not game._validate_guess(1) out, _ = capfd.readouterr() assert out.rstrip() == '1 is too low' assert not game._validate_guess(3) out, _ = capfd.readouterr() assert out.rstrip() == '3 is too high' assert game._validate_guess(2) out, _ = capfd.readouterr() assert out.rstrip() == '2 is correct!'
def test_game_win(inp, capfd): game = Game() game._answer = 6 game() assert game._win is True out = capfd.readouterr()[0] expected = ['4 is too low', 'Number not in range', '9 is too high', 'Already guessed', '6 is correct!', 'It took you 3 guesses'] # splitting output, ignoring blank lines, and striping extra spaces and junk output = [line.strip() for line in out.split('\n') if line.strip()] assert output == expected
def test_game_lose(inp, capfd): game = Game() game._answer = 13 # Play the game and make sure it is recorded as a loss game() assert game._win is False # Test the final print statement if game is lost out = capfd.readouterr()[0] output = [line.strip() for line in out.split('\n') if line.strip()] expected = 'Guessed 5 times, answer was 13' assert output.pop() == expected
def test_game_win(inp, capfd): game = Game() game._answer = 6 game() assert game._win is True out = capfd.readouterr()[0] expected = ['4 is too low', 'Number not in range', '9 is too high', 'Already guessed', '6 is correct!', 'It took you 3 guesses'] output = [line.strip() for line in out.split('\n') if line.strip()] for line, exp in zip(output, expected): assert line == exp
def test_validate_guess(capfd): # captures std output of program in execution game = Game() game._answer = 2 assert not game._validate_guess(1) out, _ = capfd.readouterr() assert out.rstrip() == '1 is too low' assert not game._validate_guess(3) out, _ = capfd.readouterr() assert out.rstrip() == '3 is too high' assert game._validate_guess(2) out, _ = capfd.readouterr() assert out.rstrip() == '2 is correct!'
def test_game_lose(inp, capfd): game = Game() game._answer = 8 game() assert game._win is False out = capfd.readouterr()[0] expected = ['4 is too low', 'Number not in range', '9 is too high', 'Already guessed', '5 is too low', '6 is too low', '7 is too low', 'Guessed 5 times, answer was 8'] output = [line.strip() for line in out.split('\n') if line.strip()] for line, exp in zip(output, expected): assert line == exp
def test_game_lose(inp, capfd): game = Game() game._answer = 10 game() # Mock a game in which you do not win, ie you expend your 5 guesses assert game._win == False assert len(game._guesses) == 5 out = capfd.readouterr()[0] expected_output = [ '1 is too low', '8 is too low', '12 is too high', 'Already guessed', '9 is too low', '11 is too high', 'Guessed 5 times, answer was 10' ] output = [line.strip() for line in out.split('\n') if line.strip()] assert output == expected_output
def test_game_lose(inp, capfd): game = Game() game._answer = 13 game() assert game._win is False out, _ = capfd.readouterr() expected = ['Please enter a number', '5 is too low', '9 is too low', '14 is too high', '11 is too low', '12 is too low', 'Guessed 5 times, answer was 13'] output = [line.strip() for line in out.split('\n') if line.strip()] for line, exp in zip(output, expected): assert line == exp
def test_game_win(inp, capfd): game = Game() game._answer = 14 game() # Mock a game in which you win in 4 tries assert game._win == True assert len(game._guesses) == 4 out = capfd.readouterr()[0] expected_output = [ '1 is too low', 'Number not in range', '19 is too high', '10 is too low', '14 is correct!', 'It took you 4 guesses' ] output = [line.strip() for line in out.split('\n') if line.strip()] assert output == expected_output
def test_validate_guess(capfd): """pytest capture stdout: https://docs.pytest.org/en/2.9.1/capture.html""" game = Game() game._answer = 2 assert not game._validate_guess(1) out, _ = capfd.readouterr() assert out.rstrip() == '1 is too low' assert not game._validate_guess(3) out, _ = capfd.readouterr() assert out.rstrip() == '3 is too high' assert game._validate_guess(2) out, _ = capfd.readouterr() assert out.rstrip() == '2 is correct!'
def test_game_loss(inp, capfd): game = Game() game._answer = 7 game() assert not game._win out = capfd.readouterr()[0] expected = ['Please enter a number.', '11 is too high', '12 is too high', '15 is too high', '16 is too high', '5 is too low', 'Guessed 5 times, answer was 7'] output = [line.strip() for line in out.split('\n') if line.strip()] for line, exp in zip(output, expected): assert line == exp
def test_validate_guess(capfd): game = Game() game._answer = 2 assert not game._validate_guess(1) out, _ = capfd.readouterr() # print(out) # run with pytest -s test_guess.py --> its not capturing the output but it prints it to the console // this can be run only once per assertion if run on multiple assertions it will fail the test assert out.rstrip( ) == '1 is too low' # run without -s to just check the percentage for passing assert not game._validate_guess(3) out, _ = capfd.readouterr() assert out.rstrip() == '3 is too high' assert game._validate_guess(2) out, _ = capfd.readouterr() assert out.rstrip() == '2 is correct!'
def test_validate_guess(capfd): game = Game() game._answer = 2 # Test incorrect values # Too low assert not game._validate_guess(1) out, _ = capfd.readouterr() assert out.strip() == '1 is too low' # Too low assert not game._validate_guess(3) out, _ = capfd.readouterr() assert out.strip() == '3 is too high' # Test correct values assert game._validate_guess(2) out, _ = capfd.readouterr() assert out.strip() == '2 is correct!'
def test_validate_guess(capfd): #capfd captures the standard output to test what is being printed game = Game() game._answer = 2 # If we guess a 1 when the answer is 2, make sure that validate_guess # is returning False assert not game._validate_guess(1) out, _ = capfd.readouterr() assert out.rstrip() == '1 is too low' assert not game._validate_guess(3) out, _ = capfd.readouterr() assert out.rstrip() == '3 is too high' assert game._validate_guess(2) out, _ = capfd.readouterr() assert out.rstrip() == '2 is correct!'
def test_game_win(inp, capfd): """ Modify variable in Game class back from self._answer = 6 to self._answer = get_random_number() when done """ game = Game() game._answer = 6 game() assert game._win is True out = capfd.readouterr()[0] expected = [ '4 is too low', 'Number not in range', '9 is too high', 'Already guessed', '6 is correct!', 'It took you 3 guesses' ] output = [line.strip() for line in out.split('\n') if line.strip()] for line, exp in zip(output, expected): assert line == exp
def test_game_win(inp, capfd): game = Game() game._answer = 6 game() assert game._win is True out, _ = capfd.readouterr() expected = [ "4 is too low", "Number not in range", "9 is too high", "Already guessed", "6 is correct!", "It took you 3 guesses", ] output = [line.strip() for line in out.split("\n") if line.strip()] for line, exp in zip(output, expected): assert line == exp
def test_game_lose(inp, capfd): game = Game() game._answer = 13 game() assert game._win is False out, _ = capfd.readouterr() expected = [ "Please enter a number", "5 is too low", "9 is too low", "14 is too high", "11 is too low", "12 is too low", "Guessed 5 times, answer was 13", ] output = [line.strip() for line in out.split("\n") if line.strip()] for line, exp in zip(output, expected): assert line == exp
def test_game_lose(inp, capfd): game = Game() game._answer = 13 game() assert game._win is False