strategies for marking test coverage of routes?

10 views
Skip to first unread message

Jonathan Vanasco

unread,
Apr 13, 2020, 4:40:38 PM4/13/20
to pylons-discuss
I'd like to ensure 100% test coverage of every route.

Has anyone worked on a solution to ensuring/reporting this?

I assume it might involve a decorator on my tests stating the routes covered, and then a command to match that against what is registered into the Pyramid app via @view_config or other mechansism.

Jonathan Vanasco

unread,
Apr 15, 2020, 6:29:07 PM4/15/20
to pylons-discuss
I ended up doing a quick proof of concept that works pretty well:

_ROUTES_TESTED = {}



def tests_routes(*args):
   
"""
    `@tests_routes` is a decorator
    when writing/editing a test, declare what routes the test covers, like such:


        @tests_routes(("
foo", "bar"))
        def test_foo_bar(self):
            ...
   
    this will populate a global variable `_ROUTES_TESTED` with the name of the
    tested routes.
   
    invoking the Audit test:
   
        python -m unittest tests.FunctionalTests_AuditRoutes


    will ensure all routes in Pyramid have test coverage
    """

    _routes
= args[0]
   
if isinstance(_routes, (list, tuple)):
       
for _r in _routes:
            _ROUTES_TESTED
[_r] = True
   
else:
        _ROUTES_TESTED
[_routes] = True


   
def _decorator(_function):
       
def _wrapper(*args, **kwargs):
            _function
(*args, **kwargs)
       
return _wrapper
   
return _decorator




class FunctionalTests_AuditRoutes(AppTest):
   
"""
    python -m unittest tests.FunctionalTests_AuditRoutes
    """



   
def test_audit(self):
        pyramid_route_names
= [i.name for i in self.testapp.app.routes_mapper.routelist]
        names_missing
= [r for r in pyramid_route_names if r not in _ROUTES_TESTED.keys()]
       
if names_missing:
           
raise ValueError("no coverage for %s routes: %s" % (len(names_missing), names_missing))




This still needs work, but implementing this into the test suite of an app with 150+ routes, I found 20 without test coverage
Reply all
Reply to author
Forward
0 new messages