Skip to content

Commit

Permalink
pythongh-57879: Increase test coverage for pstats.py (pythongh-111447)
Browse files Browse the repository at this point in the history
  • Loading branch information
tigercosmos authored and Glyphack committed Jan 27, 2024
1 parent 95cecbd commit 435abba
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions Lib/test/test_pstats.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
from pstats import SortKey
from enum import StrEnum, _test_simple_enum

import os
import pstats
import tempfile
import cProfile

class AddCallersTestCase(unittest.TestCase):
Expand Down Expand Up @@ -36,6 +38,33 @@ def test_add(self):
stats = pstats.Stats(stream=stream)
stats.add(self.stats, self.stats)

def test_dump_and_load_works_correctly(self):
temp_storage_new = tempfile.NamedTemporaryFile(delete=False)
try:
self.stats.dump_stats(filename=temp_storage_new.name)
tmp_stats = pstats.Stats(temp_storage_new.name)
self.assertEqual(self.stats.stats, tmp_stats.stats)
finally:
temp_storage_new.close()
os.remove(temp_storage_new.name)

def test_load_equivalent_to_init(self):
stats = pstats.Stats()
self.temp_storage = tempfile.NamedTemporaryFile(delete=False)
try:
cProfile.run('import os', filename=self.temp_storage.name)
stats.load_stats(self.temp_storage.name)
created = pstats.Stats(self.temp_storage.name)
self.assertEqual(stats.stats, created.stats)
finally:
self.temp_storage.close()
os.remove(self.temp_storage.name)

def test_loading_wrong_types(self):
stats = pstats.Stats()
with self.assertRaises(TypeError):
stats.load_stats(42)

def test_sort_stats_int(self):
valid_args = {-1: 'stdname',
0: 'calls',
Expand Down

0 comments on commit 435abba

Please sign in to comment.