def test_model_checkpoint_topk_zero(tmp_path):
    """Test that no checkpoints are saved when save_top_k=0."""
    model = LogInTwoMethods()
    checkpoint_callback = ModelCheckpoint(dirpath=tmp_path, save_top_k=0, save_last=True)
    trainer = Trainer(default_root_dir=tmp_path, callbacks=[checkpoint_callback], max_epochs=2, logger=False)
    trainer.fit(model)
    # these should not be set if monitor is None
    assert checkpoint_callback.monitor is None
    assert checkpoint_callback.best_model_path == ""
    assert checkpoint_callback.best_model_score is None
    assert checkpoint_callback.best_k_models == {}
    assert checkpoint_callback.kth_best_model_path == ""
    # check that only the last ckpt was created
    assert os.listdir(tmp_path) == ["last.ckpt"]
    assert checkpoint_callback.last_model_path == str(tmp_path / "last.ckpt")
    # 'last.ckpt' is not a symlink because there are no top-k checkpoints to link
    assert not os.path.islink(checkpoint_callback.last_model_path)
Test that no checkpoints are saved when save_top_k=0.
tests/tests_pytorch/checkpointing/test_model_checkpoint.py