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.
With these steps, we will see how to combine the coverage tool with nose.
recipe55.py
to store our test code.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 "---------------------------------"
nosetests
recipe55
–with-coverage
.The nose plugin for coverage invokes the coverage tool and provides a formatted report. For each module, it displays:
A common behavior of nose is to alter stdout
, disabling the print statements embedded in the test case.
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.
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.
3.148.104.242