[TIP] coverage.py and pyramid web framework?

Ben Cohen cohen.ben at gmail.com
Thu Aug 23 17:34:50 PDT 2012


I'm trying to use the coverage.py module with the pyramid web framework.  

I'm using the coverage api rather than the command line -- when a certain config option is set in my pyramid ini file, I startup my app in 'testing mode' -- which among other things, means I create a coverage.coverage object as follows in my pyramid 'main' function:

```python
cov = coverage.coverage(source=['mymodule'], timid=True)
app_globals.cov = cov
cov.start()
```

I also define a special api when in testing mode which causes an html coverage report to be generated after my test suite has run:

```python
def save_coverage(self):
   """Generate a code coverage report using the 'coverage' api
   """
   from IPython.external.path import path
   coverage_directory = path(Resources.lookup_coverage_dir())

   cov = app_globals.cov
   cov.stop()

   # delete the coverage_directory if it exists to ensure that you don't accidentally look at an old
   # version in the case of problems, then output a coverage report
   if coverage_directory.isdir():
       logger.info('dir:{0} exists -- removing prior to recreating code coverage report'.format(coverage_directory))
       path(coverage_directory).rmtree()
   cov.html_report(directory=coverage_directory)

   cov.start()
```

This seems to work -- when I cause the save_coverage() method to execute, I see that the existing coverage directory is removed and a new coverage report is output.  The coverage report shows coverage of portions of all of the relevant modules -- however there are a lot of methods which show 'no statement coverage' which I '_know_' were called in between the 'cov.start' and 'cov.stop' calls …  I've tried with and without the 'timid' option but I don't get any change.  

The most obvious shared feature of the functions/methods that report no coverage is the use of a venusian callback method decorator -- this decorator marks functions/methods with declarative attributes, subsequently modules are scanned for methods with those attributes and then ingested by the pyramid web framework's web-publishing machinery.

Is there some magic happening within the pyramid framework that causes coverage to fail to register when these methods are called?  Is anybody using coverage and pyramid together without issue …?  I didn't expect so fundamental an issue with the two as the pyramid documentation has a chapter describing the use of coverage in unit tests …

I should add that i'm using the single-threaded waitress web server to serve my app -- my tests are defined in javascript with the jasmine framework.

If anybody has any hints/observations/suggestions or other I'd appreciate it.

Many thanks,
Ben


More information about the testing-in-python mailing list