Fixed an issue where the tests weren't really doing a realistic game. Now the players exchange points instead of one player scoring all theirs, and the other player scoring all theirs.

This commit is contained in:
emilybache 2012-11-21 10:48:38 +01:00
parent 5fb9f52b48
commit 52fb51eede
3 changed files with 16 additions and 13 deletions

View File

@ -173,4 +173,6 @@ class TennisGameDefactored3:
return "Deuce" return "Deuce"
s = self.p1N if self.p1 > self.p2 else self.p2N s = self.p1N if self.p1 > self.p2 else self.p2N
return "Advantage " + s if ((self.p1-self.p2)*(self.p1-self.p2) == 1) else "Win for " + s return "Advantage " + s if ((self.p1-self.p2)*(self.p1-self.p2) == 1) else "Win for " + s
TennisGame = TennisGameDefactored3
# NOTE: You must change this to point at the one of the three examples that you're working on!
TennisGame = TennisGameDefactored1

View File

@ -1,16 +1,11 @@
import pytest import pytest
from tennis import TennisGame from tennis import TennisGame
from tennis_unittest import test_cases from tennis_unittest import test_cases, play_game
class TestTennis: class TestTennis:
@pytest.mark.parametrize('p1Points p2Points score p1Name p2Name'.split(), test_cases) @pytest.mark.parametrize('p1Points p2Points score p1Name p2Name'.split(), test_cases)
def test_get_score(self, p1Points, p2Points, score, p1Name, p2Name): def test_get_score(self, p1Points, p2Points, score, p1Name, p2Name):
game = TennisGame(p1Name, p2Name) game = play_game(p1Points, p2Points, p1Name, p2Name)
for i in range(p1Points):
game.won_point(p1Name)
for i in range(p2Points):
game.won_point(p2Name)
assert score == game.score() assert score == game.score()

View File

@ -1,4 +1,5 @@
import unittest import unittest
from itertools import izip_longest
from tennis import TennisGame from tennis import TennisGame
@ -49,16 +50,21 @@ test_cases = [
] ]
def play_game(p1Points, p2Points, p1Name, p2Name):
game = TennisGame(p1Name, p2Name)
for p1, p2 in izip_longest(range(p1Points), range(p2Points)):
if p1 is not None:
game.won_point(p1Name)
if p2 is not None:
game.won_point(p2Name)
return game
class TestTennis(unittest.TestCase): class TestTennis(unittest.TestCase):
def test_Score(self): def test_Score(self):
for testcase in test_cases: for testcase in test_cases:
(p1Points, p2Points, score, p1Name, p2Name) = testcase (p1Points, p2Points, score, p1Name, p2Name) = testcase
game = TennisGame(p1Name, p2Name) game = play_game(p1Points, p2Points, p1Name, p2Name)
for i in range(p1Points):
game.won_point(p1Name)
for i in range(p2Points):
game.won_point(p2Name)
self.assertEquals(score, game.score()) self.assertEquals(score, game.score())
if __name__ == "__main__": if __name__ == "__main__":