0

im starting to learn TDD and got the Bowling Game kata as exercise. I have completed some of the regular score test, but i want to add more just to practice and one idea was to raise an exception if i roll a negative number.

The hole code is not relevant because this is a simple validation

class BowlingGame(object):
    def __init__(self):
        pass
    def roll(self, pins):
        if pins < 0:
            #Want to Return Value Exception

class BowlingGameTest(unittest.TestCase):
    def setUp(self):
        self._game = BowlingGame()
    def test_roll_negative(self):
        self.game.roll(-1)
        #Want to catch exception here with self.assert or except

The problem is that if i return for example 0/0 the exception is raised in the BowlingGame class and not in the BowlingGameTest class (there are in two diferent files).

If i do return ValueError (BowlingGame Class) with self.assertRaises(ValueError): (BowlingGameTest Class) i got AssertionError: ValueError not raised

Any ideas?

2 Answers 2

1

You need to raise ValueError instead of returning it, i.e.,

def roll(self, pins):
    if pins < 0:
        raise ValueError

Returning an exception actually returns the class object, but if you raise an exception, it will throw an error like you want.

It sounds like you're using self.assertRaises correctly - just in case though - it should look like this:

def test_roll_negative(self):
    with self.assertRaises(ValueError):
        self.game.roll(-1)
Sign up to request clarification or add additional context in comments.

Comments

1

something like this ..

try:
    self.game.roll(-1)
except ValueError:
    pass

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.