code: def test_top_k(
      save_mock,
      tmpdir,
      k,
      epochs,
      val_check_interval,
      expected,
      save_last,
  ):
      class TestModel(BoringModel):
          def __init__(self):
              super().__init__()
              self.last_coeff = 10.0
  
          def training_step(self, batch, batch_idx):
              loss = self.step(torch.ones(32, device=self.device))
              loss = loss / (loss + 0.0000001)
              loss += self.last_coeff
              self.log("my_loss", loss)
              self.last_coeff *= 0.999
              return loss
  
      model = TestModel()
      trainer = Trainer(
          callbacks=[
              callbacks.ModelCheckpoint(
                  dirpath=tmpdir,
                  monitor="my_loss",
                  save_top_k=k,
                  save_last=save_last,
              )
          ],
          default_root_dir=tmpdir,
          max_epochs=epochs,
          enable_model_summary=False,
          val_check_interval=val_check_interval,
      )
      trainer.fit(model)
  
      # save_last=True: last epochs are saved every step (so double the save calls)
      expected = expected * 2 if save_last is True else expected
      assert save_mock.call_count == expected
  
file_path: pytorch-lightning/tests/tests_pytorch/checkpointing/test_checkpoint_callback_frequency.py
imports: import os
  from unittest import mock
  import pytest
  import torch
  from lightning.pytorch import Trainer, callbacks
  from lightning.pytorch.demos.boring_classes import BoringModel
  from tests_pytorch.helpers.runif import RunIf
label_desc: Use image node_id 3 for calling a global function with example usage: test_top_k(save_mock, tmpdir, k, epochs, val_check_interval, expected, save_last) and returns: loss