coverage should be using the sys.settrace() function to get at code to be covered. I don't know that it has a specific dependency on using __import__ hooks, which Mako bypasses so that the compiled templates don't show up in sys.modules.
It works for me:
test script:
from mako.template import Template
with open("some_template.mako", "w") as f:
f.write("hello world")
t = Template(filename="some_template.mako", module_directory='data')
print(t.render())
usage example:
coverage run test.py
hello world
classics-MacBook-Pro:mako classic$ coverage report
Name Stmts Miss Cover
---------------------------------------------
data/some_template.mako 19 0 100%
mako/__init__ 1 0 100%
mako/_ast_util 538 437 19%
mako/ast 80 65 19%
mako/cache 63 36 43%
mako/codegen 573 370 35%
mako/compat 118 57 52%
mako/exceptions 155 117 25%
mako/ext/__init__ 0 0 100%
mako/ext/pygmentplugin 41 11 73%
mako/filters 85 47 45%
mako/lexer 258 128 50%
mako/parsetree 260 145 44%
mako/pygen 126 58 54%
mako/pyparser 364 328 10%
mako/runtime 418 263 37%
mako/template 230 103 55%
mako/util 222 144 35%
test 5 0 100%
---------------------------------------------
TOTAL 3556 2309 35%
then:
coverage annotate data/
some_template.mako.py
output file looks like:
# -*- coding:ascii -*-
> from mako import runtime, filters, cache
> UNDEFINED = runtime.UNDEFINED
> __M_dict_builtin = dict
> __M_locals_builtin = locals
> _magic_number = 9
> _modified_time = 1380833728.719491
> _enable_loop = True
> _template_filename = 'some_template.mako'
> _template_uri = 'some_template.mako'
> _source_encoding = 'ascii'
> _exports = []
> def render_body(context,**pageargs):
> __M_caller = context.caller_stack._push_frame()
> try:
> __M_locals = __M_dict_builtin(pageargs=pageargs)
> __M_writer = context.writer()
# SOURCE LINE 1
> __M_writer(u'hello world')
> return ''
> finally:
> context.caller_stack._pop_frame()
So I'd say make sure coverage is running before you do anything with templates and also that it knows where to look for source code.