Skip to content

Commit ceeded2

Browse files
committed
use mocker instead of unittest mock
1 parent d2c55ba commit ceeded2

File tree

2 files changed

+49
-62
lines changed

2 files changed

+49
-62
lines changed

tests/test_cli.py

Lines changed: 33 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1-
import json
21
from pathlib import Path
32
from typing import List
4-
from unittest.mock import patch
53

64
import pytest
7-
import yaml
85
from click.testing import CliRunner
96

107
from vivarium_profiling.tools.cli import profile_sim
@@ -47,29 +44,19 @@ def run_profile_sim_command(
4744
assert result.exit_code == 0
4845

4946

50-
def test_profile_sim_parameters():
51-
"""Test that profile_sim has the expected parameters."""
52-
expected_parameters = {
53-
"model_specification",
54-
"results_directory",
55-
"skip_writing",
56-
"skip_processing",
57-
"profiler",
58-
}
59-
actual_parameters = {param.name for param in profile_sim.params}
60-
assert actual_parameters == expected_parameters
61-
62-
63-
@patch("subprocess.run")
64-
def test_profile_sim_scalene_default(
65-
mock_subprocess_run, runner: CliRunner, model_spec: str, results_dir: Path
66-
):
47+
def test_profile_sim_scalene(mocker, runner: CliRunner, model_spec: str, results_dir: Path):
6748
"""Test profile_sim with scalene profiler (default)."""
6849
# Mock successful subprocess run
50+
mock_subprocess_run = mocker.patch("subprocess.run")
6951
mock_subprocess_run.return_value = None
7052

7153
# Run the command
72-
run_profile_sim_command(runner, model_spec, results_dir)
54+
run_profile_sim_command(
55+
runner,
56+
model_spec,
57+
results_dir,
58+
extra_args=["--profiler", "scalene"],
59+
)
7360

7461
# Verify subprocess was called with correct scalene command
7562
mock_subprocess_run.assert_called_once()
@@ -89,45 +76,39 @@ def test_profile_sim_scalene_default(
8976
assert outfile_path.endswith(".json")
9077

9178

92-
@patch("subprocess.run")
93-
@patch("pstats.Stats")
79+
def test_profile_sim_cprofile_skip_processing(
80+
mocker, runner: CliRunner, model_spec: str, results_dir: Path
81+
):
82+
"""Test profile_sim with cprofile and processing skipped."""
83+
mocker.patch("subprocess.run")
84+
run_profile_sim_command(runner, model_spec, results_dir, ["--skip_processing"])
85+
86+
9487
def test_profile_sim_cprofile_with_processing(
95-
mock_pstats, mock_subprocess_run, runner: CliRunner, model_spec: str, results_dir: Path
88+
mocker, runner: CliRunner, model_spec: str, results_dir: Path
9689
):
9790
"""Test profile_sim with cprofile and post-processing enabled."""
91+
mock_subprocess_run = mocker.patch("subprocess.run")
9892
mock_subprocess_run.return_value = None
9993

94+
mock_pstats = mocker.patch("pstats.Stats")
10095
mock_stats_instance = mock_pstats.return_value
10196

102-
run_profile_sim_command(runner, model_spec, results_dir, ["--profiler", "cprofile"])
97+
run_profile_sim_command(runner, model_spec, results_dir)
10398

10499
# Verify that pstats was used for processing
105100
mock_pstats.assert_called_once()
106101
mock_stats_instance.sort_stats.assert_called_once_with("cumulative")
107102
mock_stats_instance.print_stats.assert_called_once()
108103

109104

110-
@patch("subprocess.run")
111-
def test_profile_sim_cprofile_skip_processing(
112-
mock_subprocess_run, runner: CliRunner, model_spec: str, results_dir: Path
113-
):
114-
"""Test profile_sim with cprofile and processing skipped."""
115-
mock_subprocess_run.return_value = None
116-
117-
run_profile_sim_command(
118-
runner, model_spec, results_dir, ["--profiler", "cprofile", "--skip_processing"]
119-
)
120-
121-
122-
@patch("subprocess.run")
123105
def test_profile_sim_skip_writing(
124-
mock_subprocess_run, runner: CliRunner, model_spec: str, results_dir: Path
106+
mocker, runner: CliRunner, model_spec: str, results_dir: Path
125107
):
126108
"""Test profile_sim with skip_writing flag."""
127-
# Mock successful subprocess run
128-
mock_subprocess_run.return_value = None
109+
mock_subprocess_run = mocker.patch("subprocess.run")
110+
mocker.patch("pstats.Stats")
129111

130-
# Run the command with skip_writing
131112
run_profile_sim_command(runner, model_spec, results_dir, ["--skip_writing"])
132113

133114
# Verify the config override was empty (indicating no output directory set)
@@ -137,12 +118,13 @@ def test_profile_sim_skip_writing(
137118
assert config_override == "{}"
138119

139120

140-
@patch("subprocess.run")
141121
def test_profile_sim_results_directory_structure(
142-
mock_subprocess_run, runner: CliRunner, model_spec: str, results_dir: Path
122+
mocker, runner: CliRunner, model_spec: str, results_dir: Path
143123
):
144124
"""Test that profile_sim creates the expected directory structure and file paths."""
145-
mock_subprocess_run.return_value = None
125+
mocker.patch("subprocess.run")
126+
mocker.patch("pstats.Stats")
127+
146128
run_profile_sim_command(runner, model_spec, results_dir)
147129

148130
# Check that results directory exists and has timestamped subdirectory
@@ -157,13 +139,15 @@ def test_profile_sim_results_directory_structure(
157139
assert all(part.isdigit() for part in parts)
158140

159141

160-
@patch("subprocess.run")
161142
def test_profile_sim_extra_args(
162-
mock_subprocess_run, runner: CliRunner, model_spec: str, results_dir: Path
143+
mocker, runner: CliRunner, model_spec: str, results_dir: Path
163144
):
164145
"""Test that profile_sim passes extra arguments to scalene."""
165-
mock_subprocess_run.return_value = None
166-
run_profile_sim_command(runner, model_spec, results_dir, ["--cpu-only", "--html"])
146+
mock_subprocess_run = mocker.patch("subprocess.run")
147+
148+
run_profile_sim_command(
149+
runner, model_spec, results_dir, ["--profiler", "scalene", "--cpu-only", "--html"]
150+
)
167151

168152
# Verify extra arguments were passed to scalene
169153
call_args = mock_subprocess_run.call_args[0][0]

tests/test_run_profile.py

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
from pathlib import Path
2-
from unittest.mock import MagicMock, patch
32

43
import pytest
54

@@ -15,16 +14,17 @@ def model_spec(test_model_specs: list[Path]) -> str:
1514
return str(test_model_specs[0])
1615

1716

18-
@patch("vivarium_profiling.tools.run_profile.SimulationContext")
19-
@patch("scalene.scalene_profiler.enable_profiling")
20-
def test_run_profile_scalene_uses_correct_context_manager(
21-
mock_enable_profiling, mock_simulation_context, model_spec: str
22-
):
17+
def test_run_profile_scalene_uses_correct_context_manager(mocker, model_spec: str):
2318
"""Test that run_profile_scalene uses scalene's enable_profiling context manager."""
2419
# Setup mocks
25-
mock_sim = MagicMock()
20+
mock_sim = mocker.MagicMock()
21+
mock_simulation_context = mocker.patch(
22+
"vivarium_profiling.tools.run_profile.SimulationContext"
23+
)
2624
mock_simulation_context.return_value = mock_sim
27-
mock_context_manager = MagicMock()
25+
26+
mock_context_manager = mocker.MagicMock()
27+
mock_enable_profiling = mocker.patch("scalene.scalene_profiler.enable_profiling")
2828
mock_enable_profiling.return_value = mock_context_manager
2929

3030
# Run the function
@@ -45,16 +45,19 @@ def test_run_profile_scalene_uses_correct_context_manager(
4545
mock_sim.run_simulation.assert_called_once()
4646

4747

48-
@patch("vivarium_profiling.tools.run_profile.SimulationContext")
49-
@patch("cProfile.Profile")
5048
def test_run_profile_cprofile_uses_correct_context_manager(
51-
mock_profile_class, mock_simulation_context, model_spec: str, tmp_path: Path
49+
mocker, model_spec: str, tmp_path: Path
5250
):
5351
"""Test that run_profile_cprofile uses cProfile.Profile context manager."""
5452
# Setup mocks
55-
mock_sim = MagicMock()
53+
mock_sim = mocker.MagicMock()
54+
mock_simulation_context = mocker.patch(
55+
"vivarium_profiling.tools.run_profile.SimulationContext"
56+
)
5657
mock_simulation_context.return_value = mock_sim
57-
mock_profiler = MagicMock()
58+
59+
mock_profiler = mocker.MagicMock()
60+
mock_profile_class = mocker.patch("cProfile.Profile")
5861
mock_profile_class.return_value = mock_profiler
5962

6063
# The context manager should return the same profiler instance

0 commit comments

Comments
 (0)