本文整理汇总了Python中trace.Trace.runfunc方法的典型用法代码示例。如果您正苦于以下问题:Python Trace.runfunc方法的具体用法?Python Trace.runfunc怎么用?Python Trace.runfunc使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类trace.Trace
的用法示例。
在下文中一共展示了Trace.runfunc方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: TestCallers
# 需要导入模块: from trace import Trace [as 别名]
# 或者: from trace.Trace import runfunc [as 别名]
class TestCallers(unittest.TestCase):
"""White-box testing of callers tracing"""
def setUp(self):
self.addCleanup(sys.settrace, sys.gettrace())
self.tracer = Trace(count=0, trace=0, countcallers=1)
self.filemod = my_file_and_modname()
@unittest.skipIf(hasattr(sys, 'gettrace') and sys.gettrace(),
'pre-existing trace function throws off measurements')
def test_loop_caller_importing(self):
self.tracer.runfunc(traced_func_importing_caller, 1)
expected = {
((os.path.splitext(trace.__file__)[0] + '.py', 'trace', 'Trace.runfunc'),
(self.filemod + ('traced_func_importing_caller',))): 1,
((self.filemod + ('traced_func_simple_caller',)),
(self.filemod + ('traced_func_linear',))): 1,
((self.filemod + ('traced_func_importing_caller',)),
(self.filemod + ('traced_func_simple_caller',))): 1,
((self.filemod + ('traced_func_importing_caller',)),
(self.filemod + ('traced_func_importing',))): 1,
((self.filemod + ('traced_func_importing',)),
(fix_ext_py(testmod.__file__), 'testmod', 'func')): 1,
}
self.assertEqual(self.tracer.results().callers, expected)
示例2: test_linear_methods
# 需要导入模块: from trace import Trace [as 别名]
# 或者: from trace.Trace import runfunc [as 别名]
def test_linear_methods(self):
# XXX todo: later add 'static_method_linear' and 'class_method_linear'
# here, once issue1764286 is resolved
#
for methname in ["inst_method_linear"]:
tracer = Trace(count=1, trace=0, countfuncs=0, countcallers=0)
traced_obj = TracedClass(25)
method = getattr(traced_obj, methname)
tracer.runfunc(method, 20)
firstlineno = get_firstlineno(method)
expected = {(self.my_py_filename, firstlineno + 1): 1}
self.assertEqual(tracer.results().counts, expected)
示例3: _run_with_trace
# 需要导入模块: from trace import Trace [as 别名]
# 或者: from trace.Trace import runfunc [as 别名]
def _run_with_trace(self):
from trace import Trace
trace = Trace(ignoredirs=[sys.prefix, sys.exec_prefix], trace=False,
count=True)
try:
trace.runfunc(self._run_tests)
finally:
results = trace.results()
real_stdout = sys.stdout
sys.stdout = open(self.coverage_summary, 'w')
try:
results.write_results(show_missing=True, summary=True,
coverdir=self.coverage_dir)
finally:
sys.stdout.close()
sys.stdout = real_stdout
示例4: TestFuncs
# 需要导入模块: from trace import Trace [as 别名]
# 或者: from trace.Trace import runfunc [as 别名]
class TestFuncs(unittest.TestCase):
"""White-box testing of funcs tracing"""
def setUp(self):
self.addCleanup(sys.settrace, sys.gettrace())
self.tracer = Trace(count=0, trace=0, countfuncs=1)
self.filemod = my_file_and_modname()
self._saved_tracefunc = sys.gettrace()
def tearDown(self):
if self._saved_tracefunc is not None:
sys.settrace(self._saved_tracefunc)
def test_simple_caller(self):
self.tracer.runfunc(traced_func_simple_caller, 1)
expected = {
self.filemod + ('traced_func_simple_caller',): 1,
self.filemod + ('traced_func_linear',): 1,
}
self.assertEqual(self.tracer.results().calledfuncs, expected)
def test_loop_caller_importing(self):
self.tracer.runfunc(traced_func_importing_caller, 1)
expected = {
self.filemod + ('traced_func_simple_caller',): 1,
self.filemod + ('traced_func_linear',): 1,
self.filemod + ('traced_func_importing_caller',): 1,
self.filemod + ('traced_func_importing',): 1,
(fix_ext_py(testmod.__file__), 'testmod', 'func'): 1,
}
self.assertEqual(self.tracer.results().calledfuncs, expected)
@unittest.skipIf(hasattr(sys, 'gettrace') and sys.gettrace(),
'pre-existing trace function throws off measurements')
def test_inst_method_calling(self):
obj = TracedClass(20)
self.tracer.runfunc(obj.inst_method_calling, 1)
expected = {
self.filemod + ('TracedClass.inst_method_calling',): 1,
self.filemod + ('TracedClass.inst_method_linear',): 1,
self.filemod + ('traced_func_linear',): 1,
}
self.assertEqual(self.tracer.results().calledfuncs, expected)
def test_traced_decorated_function(self):
self.tracer.runfunc(traced_decorated_function)
expected = {
self.filemod + ('traced_decorated_function',): 1,
self.filemod + ('decorator_fabric',): 1,
self.filemod + ('decorator2',): 1,
self.filemod + ('decorator1',): 1,
self.filemod + ('func',): 1,
}
self.assertEqual(self.tracer.results().calledfuncs, expected)
示例5: TestCallers
# 需要导入模块: from trace import Trace [as 别名]
# 或者: from trace.Trace import runfunc [as 别名]
class TestCallers(unittest.TestCase):
"""White-box testing of callers tracing"""
def setUp(self):
self.tracer = Trace(count=0, trace=0, countcallers=1)
self.filemod = my_file_and_modname()
def test_loop_caller_importing(self):
self.tracer.runfunc(traced_func_importing_caller, 1)
expected = {
((os.path.splitext(trace.__file__)[0] + '.py', 'trace', 'Trace.runfunc'),
(self.filemod + ('traced_func_importing_caller',))): 1,
((self.filemod + ('traced_func_simple_caller',)),
(self.filemod + ('traced_func_linear',))): 1,
((self.filemod + ('traced_func_importing_caller',)),
(self.filemod + ('traced_func_simple_caller',))): 1,
((self.filemod + ('traced_func_importing_caller',)),
(self.filemod + ('traced_func_importing',))): 1,
((self.filemod + ('traced_func_importing',)),
(fix_ext_py(testmod.__file__), 'testmod', 'func')): 1,
}
self.assertEqual(self.tracer.results().callers, expected)
示例6: TestFuncs
# 需要导入模块: from trace import Trace [as 别名]
# 或者: from trace.Trace import runfunc [as 别名]
class TestFuncs(unittest.TestCase):
"""White-box testing of funcs tracing"""
def setUp(self):
self.tracer = Trace(count=0, trace=0, countfuncs=1)
self.filemod = my_file_and_modname()
def test_simple_caller(self):
self.tracer.runfunc(traced_func_simple_caller, 1)
expected = {
self.filemod + ('traced_func_simple_caller',): 1,
self.filemod + ('traced_func_linear',): 1,
}
self.assertEqual(self.tracer.results().calledfuncs, expected)
def test_loop_caller_importing(self):
self.tracer.runfunc(traced_func_importing_caller, 1)
expected = {
self.filemod + ('traced_func_simple_caller',): 1,
self.filemod + ('traced_func_linear',): 1,
self.filemod + ('traced_func_importing_caller',): 1,
self.filemod + ('traced_func_importing',): 1,
(fix_ext_py(testmod.__file__), 'testmod', 'func'): 1,
}
self.assertEqual(self.tracer.results().calledfuncs, expected)
def test_inst_method_calling(self):
obj = TracedClass(20)
self.tracer.runfunc(obj.inst_method_calling, 1)
expected = {
self.filemod + ('TracedClass.inst_method_calling',): 1,
self.filemod + ('TracedClass.inst_method_linear',): 1,
self.filemod + ('traced_func_linear',): 1,
}
self.assertEqual(self.tracer.results().calledfuncs, expected)
示例7: TestFuncs
# 需要导入模块: from trace import Trace [as 别名]
# 或者: from trace.Trace import runfunc [as 别名]
class TestFuncs(unittest.TestCase):
"""White-box testing of funcs tracing"""
def setUp(self):
self.addCleanup(sys.settrace, sys.gettrace())
self.tracer = Trace(count=0, trace=0, countfuncs=1)
self.filemod = my_file_and_modname()
def test_simple_caller(self):
self.tracer.runfunc(traced_func_simple_caller, 1)
expected = {self.filemod + ("traced_func_simple_caller",): 1, self.filemod + ("traced_func_linear",): 1}
self.assertEqual(self.tracer.results().calledfuncs, expected)
def test_loop_caller_importing(self):
self.tracer.runfunc(traced_func_importing_caller, 1)
expected = {
self.filemod + ("traced_func_simple_caller",): 1,
self.filemod + ("traced_func_linear",): 1,
self.filemod + ("traced_func_importing_caller",): 1,
self.filemod + ("traced_func_importing",): 1,
(fix_ext_py(testmod.__file__), "testmod", "func"): 1,
}
self.assertEqual(self.tracer.results().calledfuncs, expected)
@unittest.skipIf(hasattr(sys, "gettrace") and sys.gettrace(), "pre-existing trace function throws off measurements")
def test_inst_method_calling(self):
obj = TracedClass(20)
self.tracer.runfunc(obj.inst_method_calling, 1)
expected = {
self.filemod + ("TracedClass.inst_method_calling",): 1,
self.filemod + ("TracedClass.inst_method_linear",): 1,
self.filemod + ("traced_func_linear",): 1,
}
self.assertEqual(self.tracer.results().calledfuncs, expected)
示例8: TestLineCounts
# 需要导入模块: from trace import Trace [as 别名]
# 或者: from trace.Trace import runfunc [as 别名]
class TestLineCounts(unittest.TestCase):
"""White-box testing of line-counting, via runfunc"""
def setUp(self):
self.addCleanup(sys.settrace, sys.gettrace())
self.tracer = Trace(count=1, trace=0, countfuncs=0, countcallers=0)
self.my_py_filename = fix_ext_py(__file__)
def test_traced_func_linear(self):
result = self.tracer.runfunc(traced_func_linear, 2, 5)
self.assertEqual(result, 7)
# all lines are executed once
expected = {}
firstlineno = get_firstlineno(traced_func_linear)
for i in range(1, 5):
expected[(self.my_py_filename, firstlineno + i)] = 1
self.assertEqual(self.tracer.results().counts, expected)
def test_traced_func_loop(self):
self.tracer.runfunc(traced_func_loop, 2, 3)
firstlineno = get_firstlineno(traced_func_loop)
expected = {
(self.my_py_filename, firstlineno + 1): 1,
(self.my_py_filename, firstlineno + 2): 6,
(self.my_py_filename, firstlineno + 3): 5,
(self.my_py_filename, firstlineno + 4): 1,
}
self.assertEqual(self.tracer.results().counts, expected)
def test_traced_func_importing(self):
self.tracer.runfunc(traced_func_importing, 2, 5)
firstlineno = get_firstlineno(traced_func_importing)
expected = {
(self.my_py_filename, firstlineno + 1): 1,
(fix_ext_py(testmod.__file__), 2): 1,
(fix_ext_py(testmod.__file__), 3): 1,
}
self.assertEqual(self.tracer.results().counts, expected)
def test_trace_func_generator(self):
self.tracer.runfunc(traced_func_calling_generator)
firstlineno_calling = get_firstlineno(traced_func_calling_generator)
firstlineno_gen = get_firstlineno(traced_func_generator)
expected = {
(self.my_py_filename, firstlineno_calling + 1): 1,
(self.my_py_filename, firstlineno_calling + 2): 11,
(self.my_py_filename, firstlineno_calling + 3): 10,
(self.my_py_filename, firstlineno_gen + 1): 1,
(self.my_py_filename, firstlineno_gen + 2): 11,
(self.my_py_filename, firstlineno_gen + 3): 10,
}
self.assertEqual(self.tracer.results().counts, expected)
def test_trace_list_comprehension(self):
self.tracer.runfunc(traced_caller_list_comprehension)
firstlineno_calling = get_firstlineno(traced_caller_list_comprehension)
firstlineno_called = get_firstlineno(traced_doubler)
expected = {
(self.my_py_filename, firstlineno_calling + 1): 1,
# List compehentions work differently in 3.x, so the count
# below changed compared to 2.x.
(self.my_py_filename, firstlineno_calling + 2): 12,
(self.my_py_filename, firstlineno_calling + 3): 1,
(self.my_py_filename, firstlineno_called + 1): 10,
}
self.assertEqual(self.tracer.results().counts, expected)
def test_linear_methods(self):
# XXX todo: later add 'static_method_linear' and 'class_method_linear'
# here, once issue1764286 is resolved
#
for methname in ['inst_method_linear',]:
tracer = Trace(count=1, trace=0, countfuncs=0, countcallers=0)
traced_obj = TracedClass(25)
method = getattr(traced_obj, methname)
tracer.runfunc(method, 20)
firstlineno = get_firstlineno(method)
expected = {
(self.my_py_filename, firstlineno + 1): 1,
}
self.assertEqual(tracer.results().counts, expected)