Skip to content
Snippets Groups Projects

WIP: L2SS-938: add coverage trace wrapper

Closed Stefano Di Frischia requested to merge L2SS-938-fix-device-code-coverage into master
Files
36
 
# Copyright (C) 2022 ASTRON (Netherlands Institute for Radio Astronomy)
 
# SPDX-License-Identifier: Apache-2.0
 
 
# Code coverage tools 'coverage' and 'pytest-cov' don't seem to correctly trace
 
# code which is inside methods called from within QThreads, see
 
# https://github.com/nedbat/coveragepy/issues/686
 
# To mitigate this problem, I use a custom decorator '@coverage_resolve_trace'
 
# to be hung onto those method definitions. This will prepend the decorated
 
# method code with 'sys.settrace(threading._trace_hook)' when a code
 
# coverage test is detected. When no coverage test is detected, it will just
 
# pass the original method untouched.
 
import sys
 
import threading
 
from functools import wraps
 
 
__all__ = ["coverage_resolve_trace"]
 
 
 
def coverage_resolve_trace(fn):
 
"""Wrapper to trace code coverage in Tango devices"""
 
 
@wraps(fn)
 
def wrapped(*args, **kwargs):
 
if "coverage" in sys.modules:
 
sys.settrace(threading._trace_hook)
 
return fn(*args, **kwargs)
 
 
return wrapped
Loading