def test_profiler_teardown(tmp_path, cls):
    """This test checks if profiler teardown method is called when trainer is exiting."""

    class TestCallback(Callback):
        def on_fit_end(self, trainer, *args, **kwargs) -> None:
            # describe sets it to None
            assert trainer.profiler._output_file is None

    profiler = cls(dirpath=tmp_path, filename="profiler")
    model = BoringModel()
    trainer = Trainer(default_root_dir=tmp_path, fast_dev_run=1, profiler=profiler, callbacks=[TestCallback()])
    trainer.fit(model)

    assert profiler._output_file is None
This test checks if profiler teardown method is called when trainer is exiting.
tests/tests_pytorch/profilers/test_profiler.py