Skip to content

Commit 8d0830b

Browse files
awaelchlilantiga
authored andcommitted
Fix test interactions (#18994)
(cherry picked from commit 340961a)
1 parent d4d27a6 commit 8d0830b

File tree

3 files changed

+9
-11
lines changed

3 files changed

+9
-11
lines changed

tests/tests_fabric/loggers/test_tensorboard.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,10 +214,10 @@ def test_tensorboard_finalize(monkeypatch, tmp_path):
214214

215215

216216
@mock.patch("lightning.fabric.loggers.tensorboard.log")
217-
def test_tensorboard_with_symlink(log, tmp_path):
217+
def test_tensorboard_with_symlink(log, tmp_path, monkeypatch):
218218
"""Tests a specific failure case when tensorboard logger is used with empty name, symbolic link ``save_dir``, and
219219
relative paths."""
220-
os.chdir(tmp_path) # need to use relative paths
220+
monkeypatch.chdir(tmp_path) # need to use relative paths
221221
source = os.path.join(".", "lightning_logs")
222222
dest = os.path.join(".", "sym_lightning_logs")
223223

tests/tests_pytorch/checkpointing/test_model_checkpoint.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ def test_model_checkpoint_no_extraneous_invocations(tmpdir):
401401
assert trainer.state.finished, f"Training failed with {trainer.state}"
402402

403403

404-
def test_model_checkpoint_format_checkpoint_name(tmpdir):
404+
def test_model_checkpoint_format_checkpoint_name(tmpdir, monkeypatch):
405405
# empty filename:
406406
ckpt_name = ModelCheckpoint._format_checkpoint_name("", {"epoch": 3, "step": 2})
407407
assert ckpt_name == "epoch=3-step=2"
@@ -422,18 +422,16 @@ def test_model_checkpoint_format_checkpoint_name(tmpdir):
422422
assert ckpt_name == "epoch=003-epoch_test=003"
423423

424424
# prefix
425-
char_org = ModelCheckpoint.CHECKPOINT_JOIN_CHAR
426-
ModelCheckpoint.CHECKPOINT_JOIN_CHAR = "@"
425+
monkeypatch.setattr(ModelCheckpoint, "CHECKPOINT_JOIN_CHAR", "@")
427426
ckpt_name = ModelCheckpoint._format_checkpoint_name("{epoch},{acc:.5f}", {"epoch": 3, "acc": 0.03}, prefix="test")
428427
assert ckpt_name == "test@epoch=3,acc=0.03000"
429-
ModelCheckpoint.CHECKPOINT_JOIN_CHAR = char_org
428+
monkeypatch.undo()
430429

431430
# non-default char for equals sign
432-
default_char = ModelCheckpoint.CHECKPOINT_EQUALS_CHAR
433-
ModelCheckpoint.CHECKPOINT_EQUALS_CHAR = ":"
431+
monkeypatch.setattr(ModelCheckpoint, "CHECKPOINT_EQUALS_CHAR", ":")
434432
ckpt_name = ModelCheckpoint._format_checkpoint_name("{epoch:03d}-{acc}", {"epoch": 3, "acc": 0.03})
435433
assert ckpt_name == "epoch:003-acc:0.03"
436-
ModelCheckpoint.CHECKPOINT_EQUALS_CHAR = default_char
434+
monkeypatch.undo()
437435

438436
# no dirpath set
439437
ckpt_name = ModelCheckpoint(monitor="early_stop_on", dirpath=None).format_checkpoint_name({"epoch": 3, "step": 2})

tests/tests_pytorch/loggers/test_tensorboard.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -313,10 +313,10 @@ def test_tensorboard_save_hparams_to_yaml_once(tmp_path):
313313

314314

315315
@mock.patch("lightning.pytorch.loggers.tensorboard.log")
316-
def test_tensorboard_with_symlink(log, tmp_path):
316+
def test_tensorboard_with_symlink(log, tmp_path, monkeypatch):
317317
"""Tests a specific failure case when tensorboard logger is used with empty name, symbolic link ``save_dir``, and
318318
relative paths."""
319-
os.chdir(tmp_path) # need to use relative paths
319+
monkeypatch.chdir(tmp_path) # need to use relative paths
320320
source = os.path.join(".", "lightning_logs")
321321
dest = os.path.join(".", "sym_lightning_logs")
322322

0 commit comments

Comments
 (0)