Embedding nose inside Python

It's very convenient to embed nose inside a Python script. This lets us create higher level test tools besides letting the developer add testing to an existing tool.

How to do it...

With these steps, we will explore using nose's API inside a Python script to run some tests:

  1. Create a new file called recipe12.py to contain the code from this recipe.
  2. Create a class to test. For this recipe, we will use a shopping cart application that lets us load items and then calculate the bill.
    class ShoppingCart(object):
        def __init__(self):
            self.items = []
    
        def add(self, item, price):
            self.items.append(Item(item, price))
            return self
    
        def item(self, index):
            return self.items[index-1].item
    
        def price(self, index):
            return self.items[index-1].price
    
        def total(self, sales_tax):
            sum_price = sum([item.price for item in self.items])
            return sum_price*(1.0 + sales_tax/100.0)
    
        def __len__(self):
            return len(self.items)
    
    class Item(object):
        def __init__(self, item, price):
            self.item = item
            self.price = price
  3. Create a test case with several test methods.
    import unittest
    
    class ShoppingCartTest(unittest.TestCase):
        def setUp(self):
            self.cart = ShoppingCart().add("tuna sandwich", 15.00)
    
        def test_length(self):
            self.assertEquals(1, len(self.cart))
    
        def test_item(self):
            self.assertEquals("tuna sandwich", self.cart.item(1))
    
        def test_price(self):
            self.assertEquals(15.00, self.cart.price(1))
    
        def test_total_with_sales_tax(self):
            self.assertAlmostEquals(16.39, 
                                    self.cart.total(9.25), 2)
  4. Create a script named recipe12_nose.py to use nose's API to run tests.
  5. Make the script runnable and use nose's run() method to run selected arguments.
    if __name__ == "__main__":
        import nose
        nose.run(argv=["", "recipe12", "--verbosity=2"])
  6. Run the test script from the command line and see the verbose output.
    How to do it...

How it works...

In the test-running code, we are using nose.run(). With no arguments, it simply picks up on sys.argv and acts like the command-line nosetests. But in this recipe, we are plugging in the name of the current module along with an increased verbosity.

There's more

Unittest has unittest.main(), which discovers and runs test cases as well. How is this different? unittest.main() is geared to discover the test cases in the same module where it is run. nose.run() is geared to let us pass in command-line arguments or load them programmatically.

For example, look at the following steps which we must complete to turn up verbosity with unittest:

if __name__ == "__main__":
    import unittest
    from recipe12 import *
    suite = unittest.TestLoader().loadTestsFromTestCase(
                                        ShoppingCartTest)
    unittest.TextTestRunner(verbosity=2).run(suite)

We had to import the test cases, use a test loader to create a test suite, and then run it through the TextTestRunner.

To do the same thing with nose, this is all we need:

if __name__ == "__main__":
    import nose
    nose.run(argv=["", "recipe12", "--verbosity=2"])

This is much more succinct. Any command-line options we could use with nosetests are able to be used here. This comes in handy when we use nose plugin, which we will explore in more detail in this chapter and through the rest of the book.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.176.99