Getting nosy with coverage

Install the coverage nose plugin, and run your test suite using nose. This provides a quick and convenient report using the ubiquitous nosetests tool. This recipe assumes you have already created the network management application as described in the Building a network management application section.

How to do it...

With these steps, we will see how to combine the coverage tool with nose.

  1. Create a new file called recipe55.py to store our test code.
  2. Create a test case that injects a faulting alarm.
    from network import *
    import unittest
    from springpython.database.factory import *
    from springpython.database.core import *
    
    class EventCorrelationTest(unittest.TestCase):
        def setUp(self):
            db_name = "recipe55.db"
            factory = Sqlite3ConnectionFactory(db=db_name)
            self.correlator = EventCorrelator(factory)
    
            dt = DatabaseTemplate(factory)
            sql = open("network.sql").read().split(";")
            for statement in sql:
                dt.execute(statement + ";")
    
        def test_process_events(self):
            evt1 = Event("pyhost1", "serverRestart", 5)
    
            stored_event, is_active, 
               updated_services, updated_equipment = 
                         self.correlator.process(evt1)
    
            print "Stored event: %s" % stored_event
            if is_active:
                print "This event was an active event."
    
            print "Updated services: %s" % updated_services
            print "Updated equipment: %s" % updated_equipment
            print "---------------------------------"
  3. Run the test module using the coverage plugin by typing nosetests recipe55 –with-coverage.
    How to do it...

How it works...

The nose plugin for coverage invokes the coverage tool and provides a formatted report. For each module, it displays:

  • Total number of statements
  • Number of missed statements
  • Percentage of covered statements
  • Line numbers for the missed statements

There's more...

A common behavior of nose is to alter stdout, disabling the print statements embedded in the test case.

Why use the nose plugin instead of the coverage tool directly?

The coverage tool works fine by itself, as was demonstrated in other recipes in this chapter, however, nose is a ubiquitous testing tool used by many developers. Providing a plugin makes it easy to support this vast community by empowering them to run the exact set of test plugins they want, with coverage being part of that test complement.

Why are sqlite3 and springpython included?

Sqlite3 is a relational database library that is included with Python. It is file based which means that no separate processes are required to create and use a database. Details about Spring Python can be found in the earlier sections of this chapter.

The purpose of this recipe was to measure coverage of our network management application and the corresponding test case. So why are these third-party libraries included? The coverage tool has no way of automatically knowing what we want and the things we don't want to see from a coverage perspective. To delve into this, refer to the next section Filtering out test noise from coverage.

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

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