Testing for exceptions
The update method should also raise a ValueError when the price is less than zero. The following is how we verify this in the doctest:
def update(self, timestamp, price):
"""Updates the stock with the price at the given timestamp
>>> from datetime import datetime
>>> stock = Stock("GOOG")
>>> stock.update(datetime(2014, 10, 2), 10)
>>> stock.price
10
The method raises a ValueError exception if the price is negative
>>> stock.update(datetime(2014, 10, 2), -1)
Traceback (most recent call last):
...
ValueError: price should not be negative
"""
if price < 0:
raise ValueError("price should not be negative")
self.history.update(timestamp, price)
self.updated.fire(self)The next section shows the expectation that doctest looks at:
Traceback (most recent call last):
...
ValueError: price...