Skip to content

Commit 2d7d1c5

Browse files
committed
Refactored tests/test_autoprofile.py
tests/test_autoprofile.py test_autoprofile_exec_{package,module,callable_wrapper_objects}() Refactored tests to simplify call/parametrization signatures
1 parent bc3c664 commit 2d7d1c5

File tree

1 file changed

+65
-75
lines changed

1 file changed

+65
-75
lines changed

tests/test_autoprofile.py

Lines changed: 65 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import os
22
import subprocess
33
import sys
4+
import shlex
45
import tempfile
56

67
import pytest
@@ -432,48 +433,41 @@ def test_autoprofile_script_with_prof_imports():
432433

433434

434435
@pytest.mark.parametrize(
435-
['use_kernprof_exec', 'prof_mod', 'no_preimports', 'prof_imports',
436-
'add_one', 'add_two', 'add_operator', 'main'],
437-
[(False, 'test_mod.submod1', False, False,
438-
True, False, True, False),
436+
('use_kernprof_exec', 'prof_mod', 'flags', 'profiled_funcs'),
437+
[(False, ['test_mod.submod1'], '', {'add_one', 'add_operator'}),
439438
# By not using `--no-preimports`, the entirety of `.submod1` is
440439
# passed to `add_imported_function_or_module()`
441-
(False, 'test_mod.submod1', True, False,
442-
True, False, False, False),
443-
(False, 'test_mod.submod2', False, True,
444-
False, True, True, False),
445-
(False, 'test_mod', False, True,
446-
True, True, True, True),
440+
(False, ['test_mod.submod1'], '--no-preimports', {'add_one'}),
441+
(False, ['test_mod.submod2'],
442+
'--prof-imports', {'add_two', 'add_operator'}),
443+
(False, ['test_mod'],
444+
'--prof-imports', {'add_one', 'add_two', 'add_operator', '_main'}),
447445
# Explicitly add all the modules via multiple `-p` flags, without
448446
# using the `--prof-imports` flag
449-
(False, ['test_mod', 'test_mod.submod1,test_mod.submod2'], False, False,
450-
True, True, True, True),
451-
(False, None, False, True,
452-
False, False, False, False),
453-
(True, None, False, True,
454-
False, False, False, False)])
455-
def test_autoprofile_exec_package(
456-
use_kernprof_exec, prof_mod, no_preimports, prof_imports,
457-
add_one, add_two, add_operator, main):
447+
(False, ['test_mod', 'test_mod.submod1,test_mod.submod2'],
448+
'', {'add_one', 'add_two', 'add_operator', '_main'}),
449+
(False, [], '--prof-imports', set()),
450+
(True, [], '--prof-imports', set())])
451+
def test_autoprofile_exec_package(use_kernprof_exec, prof_mod,
452+
flags, profiled_funcs):
458453
"""
459454
Test the execution of a package.
460455
"""
461456
temp_dpath = ub.Path(tempfile.mkdtemp())
462457
_write_demo_module(temp_dpath)
463458

459+
# Sanity check
460+
all_checked_funcs = {'add_one', 'add_two', 'add_operator', '_main'}
461+
profiled_funcs = set(profiled_funcs)
462+
assert not profiled_funcs - all_checked_funcs
463+
464464
if use_kernprof_exec:
465465
args = ['kernprof']
466466
else:
467467
args = [sys.executable, '-m', 'kernprof']
468-
if prof_mod is not None:
469-
if isinstance(prof_mod, str):
470-
prof_mod = [prof_mod]
471-
for pm in prof_mod:
472-
args.extend(['-p', pm])
473-
if no_preimports:
474-
args.append('--no-preimports')
475-
if prof_imports:
476-
args.append('--prof-imports')
468+
for pm in prof_mod:
469+
args.extend(['-p', pm])
470+
args.extend(shlex.split(flags))
477471
args.extend(['-l', '-m', 'test_mod', '1', '2', '3'])
478472
proc = ub.cmd(args, cwd=temp_dpath, verbose=2)
479473
print(proc.stdout)
@@ -488,51 +482,48 @@ def test_autoprofile_exec_package(
488482
print(raw_output)
489483
proc.check_returncode()
490484

491-
assert ('Function: add_one' in raw_output) == add_one
492-
assert ('Function: add_two' in raw_output) == add_two
493-
assert ('Function: add_operator' in raw_output) == add_operator
494-
assert ('Function: _main' in raw_output) == main
485+
for func in all_checked_funcs:
486+
assert (f'Function: {func}' in raw_output) == (func in profiled_funcs)
495487

496488

497489
@pytest.mark.parametrize(
498-
['use_kernprof_exec', 'prof_mod', 'no_preimports', 'prof_imports',
499-
'add_one', 'add_two', 'add_three', 'add_four', 'add_operator', 'main'],
500-
[(False, 'test_mod.submod2,test_mod.subpkg.submod3.add_three', True, False,
501-
False, True, False, False, False, False),
490+
('use_kernprof_exec', 'prof_mod', 'flags', 'profiled_funcs'),
491+
[(False, 'test_mod.submod2,test_mod.subpkg.submod3.add_three',
492+
'--no-preimports', {'add_two'}),
502493
# By not using `--no-preimports`:
503494
# - The entirety of `.submod2` is passed to
504495
# `add_imported_function_or_module()`
505496
# - Despite not having been imported anywhere, `add_three()` is
506497
# still profiled
507-
(False, 'test_mod.submod2,test_mod.subpkg.submod3.add_three', False, False,
508-
False, True, True, False, True, False),
509-
(False, 'test_mod.submod1', False, False,
510-
True, False, False, False, True, False),
511-
(False, 'test_mod.subpkg.submod4', False, True,
512-
True, True, False, True, True, True),
513-
(False, None, False, True,
514-
False, False, False, False, False, False),
515-
(True, None, False, True,
516-
False, False, False, False, False, False)])
517-
def test_autoprofile_exec_module(
518-
use_kernprof_exec, prof_mod, no_preimports, prof_imports,
519-
add_one, add_two, add_three, add_four, add_operator, main):
498+
(False, 'test_mod.submod2,test_mod.subpkg.submod3.add_three',
499+
'', {'add_two', 'add_three', 'add_operator'}),
500+
(False, 'test_mod.submod1', '', {'add_one', 'add_operator'}),
501+
(False, 'test_mod.subpkg.submod4',
502+
'--prof-imports',
503+
{'add_one', 'add_two', 'add_four', 'add_operator', '_main'}),
504+
(False, None, '--prof-imports', {}),
505+
(True, None, '--prof-imports', {})])
506+
def test_autoprofile_exec_module(use_kernprof_exec, prof_mod,
507+
flags, profiled_funcs):
520508
"""
521509
Test the execution of a module.
522510
"""
523511
temp_dpath = ub.Path(tempfile.mkdtemp())
524512
_write_demo_module(temp_dpath)
525513

514+
# Sanity check
515+
all_checked_funcs = {'add_one', 'add_two', 'add_three', 'add_four',
516+
'add_operator', '_main'}
517+
profiled_funcs = set(profiled_funcs)
518+
assert not profiled_funcs - all_checked_funcs
519+
526520
if use_kernprof_exec:
527521
args = ['kernprof']
528522
else:
529523
args = [sys.executable, '-m', 'kernprof']
530524
if prof_mod is not None:
531525
args.extend(['-p', prof_mod])
532-
if no_preimports:
533-
args.append('--no-preimports')
534-
if prof_imports:
535-
args.append('--prof-imports')
526+
args.extend(shlex.split(flags))
536527
args.extend(['-l', '-m', 'test_mod.subpkg.submod4', '1', '2', '3'])
537528
proc = ub.cmd(args, cwd=temp_dpath, verbose=2)
538529
print(proc.stdout)
@@ -547,12 +538,8 @@ def test_autoprofile_exec_module(
547538
print(raw_output)
548539
proc.check_returncode()
549540

550-
assert ('Function: add_one' in raw_output) == add_one
551-
assert ('Function: add_two' in raw_output) == add_two
552-
assert ('Function: add_three' in raw_output) == add_three
553-
assert ('Function: add_four' in raw_output) == add_four
554-
assert ('Function: add_operator' in raw_output) == add_operator
555-
assert ('Function: _main' in raw_output) == main
541+
for func in all_checked_funcs:
542+
assert (f'Function: {func}' in raw_output) == (func in profiled_funcs)
556543

557544

558545
@pytest.mark.parametrize('view', [True, False])
@@ -651,15 +638,14 @@ def test_autoprofile_from_inlined_script(outfile, expected_outfile) -> None:
651638

652639

653640
@pytest.mark.parametrize(
654-
('prof_mod, function, method, class_method, static_method, '
655-
'descriptor'),
656-
[('my_module', True, True, True, True, True),
641+
('prof_mod', 'profiled_funcs'),
642+
[('my_module',
643+
{'function', 'method', 'class_method', 'static_method', 'descriptor'}),
657644
# `function()` included in profiling via `Class.partial_method()`
658-
('my_module.Class', True, True, True, True, True),
659-
('my_module.Class.descriptor', False, False, False, False, True)])
660-
def test_autoprofile_callable_wrapper_objects(
661-
prof_mod, function, method, class_method, static_method,
662-
descriptor):
645+
('my_module.Class',
646+
{'function', 'method', 'class_method', 'static_method', 'descriptor'}),
647+
('my_module.Class.descriptor', {'descriptor'})])
648+
def test_autoprofile_callable_wrapper_objects(prof_mod, profiled_funcs):
663649
"""
664650
Test that on-import profiling catches various callable-wrapper
665651
object types:
@@ -669,6 +655,16 @@ def test_autoprofile_callable_wrapper_objects(
669655
- partialmethod
670656
Like it does regular methods and functions.
671657
"""
658+
# Sanity check
659+
all_checked_funcs = {'function', 'method',
660+
'partial_method', 'class_method', 'static_method',
661+
'descriptor'}
662+
profiled_funcs = set(profiled_funcs)
663+
assert not profiled_funcs - all_checked_funcs
664+
# Note: `partial_method()` not to be included as its own item
665+
# because it's a wrapper around `function()`
666+
assert 'partial_method' not in profiled_funcs
667+
672668
with tempfile.TemporaryDirectory() as tmpdir:
673669
temp_dpath = ub.Path(tmpdir)
674670
path = temp_dpath / 'path'
@@ -723,11 +719,5 @@ def descriptor(self):
723719
print(proc.stderr)
724720
proc.check_returncode()
725721

726-
assert ('Function: function' in raw_output) == function
727-
assert ('Function: method' in raw_output) == method
728-
assert ('Function: class_method' in raw_output) == class_method
729-
assert ('Function: static_method' in raw_output) == static_method
730-
# `partial_method()` not included as its own item because it's a
731-
# wrapper around `function()`
732-
assert 'Function: partial_method' not in raw_output
733-
assert ('Function: descriptor' in raw_output) == descriptor
722+
for func in all_checked_funcs:
723+
assert (f'Function: {func}' in raw_output) == (func in profiled_funcs)

0 commit comments

Comments
 (0)