1 import unittest 2 from test import support 3 from io import StringIO 4 import pstats 5 6 7 8 class AddCallersTestCase(unittest.TestCase): 9 """Tests for pstats.add_callers helper.""" 10 11 def test_combine_results(self): 12 # pstats.add_callers should combine the call results of both target 13 # and source by adding the call time. See issue1269. 14 # new format: used by the cProfile module 15 target = {"a": (1, 2, 3, 4)} 16 source = {"a": (1, 2, 3, 4), "b": (5, 6, 7, 8)} 17 new_callers = pstats.add_callers(target, source) 18 self.assertEqual(new_callers, {'a': (2, 4, 6, 8), 'b': (5, 6, 7, 8)}) 19 # old format: used by the profile module 20 target = {"a": 1} 21 source = {"a": 1, "b": 5} 22 new_callers = pstats.add_callers(target, source) 23 self.assertEqual(new_callers, {'a': 2, 'b': 5}) 24 25 26 class StatsTestCase(unittest.TestCase): 27 def setUp(self): 28 stats_file = support.findfile('pstats.pck') 29 self.stats = pstats.Stats(stats_file) 30 31 def test_add(self): 32 stream = StringIO() 33 stats = pstats.Stats(stream=stream) 34 stats.add(self.stats, self.stats) 35 36 37 if __name__ == "__main__": 38 unittest.main() 39