Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-57879: Increase test coverage for pstats.py #103928

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions Lib/test/test_pstats.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
from enum import StrEnum, _test_simple_enum

import pstats
import tempfile
import cProfile
from os import remove, path
from functools import cmp_to_key

class AddCallersTestCase(unittest.TestCase):
"""Tests for pstats.add_callers helper."""
Expand All @@ -30,12 +33,35 @@ class StatsTestCase(unittest.TestCase):
def setUp(self):
stats_file = support.findfile('pstats.pck')
self.stats = pstats.Stats(stats_file)
to_compile = 'import os'
self.temp_storage = tempfile.mktemp()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

profiled = compile(to_compile, '<string>', 'exec')
cProfile.run(profiled, filename=self.temp_storage)

def tearDown(self):
remove(self.temp_storage)

def test_add(self):
stream = StringIO()
stats = pstats.Stats(stream=stream)
stats.add(self.stats, self.stats)

def test_dump_and_load_works_correctly(self):
self.stats.dump_stats(filename=self.temp_storage)
tmp_stats = pstats.Stats(self.temp_storage)
self.assertEqual(self.stats.stats, tmp_stats.stats)

def test_load_equivalent_to_init(self):
empty = pstats.Stats()
empty.load_stats(self.temp_storage)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like your new tests both dump to and load from this temp file, functionality wise it works for now, but I think we should "isolate" the tests as much as possible? Reordering the execution of the tests could cause this load_stats to load different files - it could be the file created at setUp, or it could be the file created in test_dump_and_load_works_correctly. Again, it works because the test does not care what's in the file, as long as it's valid, it's just a bit convoluted. It seems like the profile created in setUp is only (potentially) used in this test? Maybe consider to move that piece into the unittest directly? The other test will overwrite thie file anyway I assume?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

created = pstats.Stats(self.temp_storage)
self.assertEqual(empty.stats, created.stats)

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

def test_sort_stats_int(self):
valid_args = {-1: 'stdname',
0: 'calls',
Expand Down Expand Up @@ -119,5 +145,42 @@ def test_SortKey_enum(self):
self.assertEqual(SortKey.FILENAME, 'filename')
self.assertNotEqual(SortKey.FILENAME, SortKey.CALLS)


class TupleCompTestCase(unittest.TestCase):

def test_tuple_comp_compare_is_correct(self):
comp_list = [(0, 1), (1, -1), (2, 1)]
tup = pstats.TupleComp(comp_list)
to_sort = [(1, 3, 4), (2, 3, 1), (5, 4, 3)]
desired = [(1, 3, 4), (2, 3, 1), (5, 4, 3)]
to_sort.sort(key=cmp_to_key(tup.compare))
self.assertEqual(to_sort, desired)


class UtilsTestCase(unittest.TestCase):
def test_count_calls(self):
dic = {
1: 2, 3: 5
}
dic_null = {
1: 0, 2: 0
}
self.assertEqual(pstats.count_calls(dic), 7)
self.assertEqual(pstats.count_calls(dic_null), 0)

def test_f8(self):
self.assertEqual(pstats.f8(2.3232), ' 2.323')
self.assertEqual(pstats.f8(0), ' 0.000')

def test_func_name(self):
func = ('file', 10, 'name')
self.assertEqual(pstats.func_get_function_name(func), 'name')

def test_strip_path(self):
func = (path.join('long', 'path'), 10, 'name')
desired = ('path', 10, 'name')
self.assertEqual(pstats.func_strip_path(func), desired)


if __name__ == "__main__":
unittest.main()