Skip to content

Commit e8edd7e

Browse files
test(train): make hyperparameters-file tests robust to global open/exists calls (#6561)
test_hyperparameters_valid_json and test_hyperparameters_valid_yaml patch os.path.exists and builtins.open globally and assert them with assert_called_once_with. Those are exact-count assertions on global built-ins, so any unrelated exists/open call surfacing during a full-suite run makes them fail intermittently with "Expected 'open'/'exists' to be called once. Called 2 times" — observed on unrelated PR CI runs for both the json and yaml variants. Switch the two assertions in each test to assert_any_call. This still verifies the meaningful behavior (the hyperparameters file is checked for existence and opened for reading) without being brittle to unrelated global calls. No product code change; the loader still opens the file exactly once. Co-authored-by: Mufaddal Rohawala <mufi@amazon.com>
1 parent bd53b10 commit e8edd7e

1 file changed

Lines changed: 12 additions & 4 deletions

File tree

‎sagemaker-train/tests/unit/train/test_model_trainer.py‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1616,8 +1616,12 @@ def test_hyperparameters_valid_json(mock_exists, modules_session):
16161616
hyperparameters="hyperparameters.json",
16171617
)
16181618
assert model_trainer.hyperparameters == expected_hyperparameters
1619-
mock_file_open.assert_called_once_with("hyperparameters.json", "r")
1620-
mock_exists.assert_called_once_with("hyperparameters.json")
1619+
# assert_any_call (not assert_called_once_with): os.path.exists and open
1620+
# are patched globally, so unrelated calls elsewhere in a full-suite run
1621+
# would break an exact-count assertion. We only care that the
1622+
# hyperparameters file itself was checked and opened for reading.
1623+
mock_file_open.assert_any_call("hyperparameters.json", "r")
1624+
mock_exists.assert_any_call("hyperparameters.json")
16211625

16221626

16231627
@patch("os.path.exists")
@@ -1635,8 +1639,12 @@ def test_hyperparameters_valid_yaml(mock_exists, modules_session):
16351639
hyperparameters="hyperparameters.yaml",
16361640
)
16371641
assert model_trainer.hyperparameters == expected_hyperparameters
1638-
mock_file_open.assert_called_once_with("hyperparameters.yaml", "r")
1639-
mock_exists.assert_called_once_with("hyperparameters.yaml")
1642+
# assert_any_call (not assert_called_once_with): os.path.exists and open
1643+
# are patched globally, so unrelated calls elsewhere in a full-suite run
1644+
# would break an exact-count assertion. We only care that the
1645+
# hyperparameters file itself was checked and opened for reading.
1646+
mock_file_open.assert_any_call("hyperparameters.yaml", "r")
1647+
mock_exists.assert_any_call("hyperparameters.yaml")
16401648

16411649

16421650
def test_hyperparameters_not_exist(modules_session):

0 commit comments

Comments
 (0)