Writing tests for nose2

Apart from picking up the existing tests written with the unittest module and running them, nose2 also supports new ways of writing tests.

To start with, nose2 allows tests to be regular functions. We don't need to create a class and inherit it from any base class. As long as the function starts with the word test, it is considered a test and executed.

We can take the following test:

class StockTest(unittest.TestCase):
    def setUp(self):
        self.goog = Stock("GOOG")

    def test_price_of_a_new_stock_class_should_be_None(self):
        self.assertIsNone(self.goog.price)

And write the above test as follows:

def test_price_of_a_new_stock_class_should_be_None():
    goog = Stock("GOOG")
    assert goog.price is None

As we can see, writing tests this way ...

Get Test-Driven Python Development now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.