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): 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_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_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_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_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_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_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_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_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_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): """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_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_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_guess(inp): game = Game() # good assert game.guess() == 11 assert game.guess() == 12 # not a number with pytest.raises(ValueError): game.guess() # already guessed with pytest.raises(ValueError): game.guess() # good assert game.guess() == 5 # out of range values with pytest.raises(ValueError): game.guess() with pytest.raises(ValueError): game.guess() # good assert game.guess() == 7 # user hit enter with pytest.raises(ValueError): game.guess()
def test_game_lose(inp, capfd): game = Game() game._answer = 13 game() assert game._win is False
def test_guess(input): game = Game() # good assert game.guess() == 11 assert game.guess() == 12 # not a number with pytest.raises(ValueError): game.guess() # number already guessed with pytest.raises(ValueError): game.guess() # good assert game.guess() == 5 # out of range with pytest.raises(ValueError): game.guess() # out of range with pytest.raises(ValueError): game.guess() # good assert game.guess() == 7 # No input with pytest.raises(ValueError): game.guess()
def test_guess(inp): game = Game() # good assert game.guess() == 1 assert game.guess() == 12
def test_guess(inp): game = Game() # good - 11 and '12' assert game.guess() == 11 assert game.guess() == 12 # not a number - 'bob' with pytest.raises(ValueError): game.guess() # already guessed - 12 with pytest.raises(ValueError): game.guess() # good - 5 assert game.guess() == 5 # number not in range - -1 & 21 with pytest.raises(ValueError): game.guess() with pytest.raises(ValueError): game.guess() # good - 7 assert game.guess() == 7 # not a number - None with pytest.raises(ValueError): game.guess()
def test_guess(inp): game = Game() # good guesses (int or string) assert game.guess() == 11 assert game.guess() == 12 # guess the word 'test' with pytest.raises(ValueError): game.guess() # guess a previously guessed value with pytest.raises(ValueError): game.guess() assert game.guess() == 5 # guess -ve value with pytest.raises(ValueError): game.guess() # guess out of range with pytest.raises(ValueError): game.guess() assert game.guess() == 7 # guess None with pytest.raises(ValueError): game.guess()
def test_guess(anything): game = Game() # good inputs assert game.guess() == 11 assert game.guess() == 12 # not a number with pytest.raises(ValueError): game.guess() # already guessed 12 with pytest.raises(ValueError): game.guess() # good input assert game.guess() == 5 # out of range values with pytest.raises(ValueError): game.guess() # already guessed 12 with pytest.raises(ValueError): game.guess() # good input assert game.guess() == 7 # user hit 'enter' with pytest.raises(ValueError): game.guess()
def test_guess(inp): game = Game() # Good (11, '12') assert game.guess() == 11 assert game.guess() == 12 # NAN ('bob') with pytest.raises(ValueError): game.guess() # Already guessed 12 (12) with pytest.raises(ValueError): game.guess() # Good (5) assert game.guess() == 5 # Out of Range (-1, 21) with pytest.raises(ValueError): game.guess() with pytest.raises(ValueError): game.guess() # Good (7) assert game.guess() == 7 with pytest.raises(ValueError): game.guess()
def test_guess(inp): game = Game() # good assert game.guess() == 11 assert game.guess() == 12 # not a number with pytest.raises(ValueError): game.guess() # already guessed 12 with pytest.raises(ValueError): game.guess() # good assert game.guess() == 5 # out of range (x2) with pytest.raises(ValueError): game.guess() with pytest.raises(ValueError): game.guess() # good assert game.guess() == 7 # hitting enter / no input with pytest.raises(ValueError): game.guess()
def test_guess(inp): game = Game() # good assert game.guess() == 11 assert game.guess() == 12 # not a number with pytest.raises(ValueError): game.guess() # already guessed 12 with pytest.raises(ValueError): game.guess() # good assert game.guess() == 5 # out of range values with pytest.raises(ValueError): game.guess() with pytest.raises(ValueError): game.guess() # good assert game.guess() == 7 # user hit enter with pytest.raises(ValueError): game.guess()
def test_game_lose(inp, capfd): game = Game() game._answer = 13 game() assert game._win is False
def test_game(inp): game = Game() # Good guesses assert game.guess() == 11 assert game.guess() == 12 # Not a number with pytest.raises(ValueError): game.guess() # Alread guessed 12 with pytest.raises(ValueError): game.guess() # Good assert game.guess() == 5 # Out of range values with pytest.raises(ValueError): game.guess() with pytest.raises(ValueError): game.guess() # Good assert game.guess() == 7 # User hit enter with pytest.raises(ValueError): game.guess()
def test_guess(inp): game = Game() # good inputs - 1, '12' ('12' gets converted to int) assert game.guess() == 1 assert game.guess() == 12 # not in range - '-1, 21' with pytest.raises(ValueError): game.guess() with pytest.raises(ValueError): game.guess() # Not a number - 'bob', '', None with pytest.raises(ValueError): game.guess() with pytest.raises(ValueError): game.guess() with pytest.raises(ValueError): game.guess() # Already guessed - 12 with pytest.raises(ValueError): game.guess()
def test_guess(inp): game = Game() # good assert game.guess() == 11 assert game.guess() == 12 # NaN with bob with pytest.raises(ValueError): game.guess() # Alredy has 12 with pytest.raises(ValueError): game.guess() # good assert game.guess() == 5 # Out of range with -1 with pytest.raises(ValueError): game.guess() # Out of range with 21 with pytest.raises(ValueError): game.guess() # good assert game.guess() == 7 # NaN with None with pytest.raises(ValueError): game.guess()