Repository: Lightning-AI/pytorch-lightning

Issue:
test_dataloader called during check_testing_model_configuration
## 🐛 Bug

When testing a model that has been loaded using the `resume_from_checkpoint` Trainer flag (note: `.fit()` has **not** been called yet in this case), the `check_testing_model_configuration` method [calls](https://github.com/PyTorchLightning/pytorch-lightning/blob/c1c6e3b6c988c690f9e2cd5726da4ab3402dbd03/pytorch_lightning/trainer/trainer.py#L1026) `test_dataloader()`, which results in undefined attribute errors if the test dataloader depends on stuff set during `prepare_data` (which is called at the beginning of `fit`)

### To Reproduce

Steps to reproduce the behavior:

1. Initialize a Trainer like `trainer = Trainer(model, resume_from_checkpoint="...", ...)` (make sure `.fit` is not called yet)
2. Run `trainer.test()`
3. If test dataloader depends on prepare_data, errors will be thrown that trace back to `check_testing_model_configuration`

### Additional context

This seems like It could be fixed with a simple change--remove the `model.test_dataloader()` call from this [line](https://github.com/PyTorchLightning/pytorch-lightning/blob/c1c6e3b6c988c690f9e2cd5726da4ab3402dbd03/pytorch_lightning/trainer/trainer.py#L1026) and replace `hasattr` with `is_overriden` call . I can submit a PR if this solution is acceptable.



Discussion:

Resolving patch:
diff --git a/pytorch_lightning/trainer/trainer.py b/pytorch_lightning/trainer/trainer.py
--- a/pytorch_lightning/trainer/trainer.py
+++ b/pytorch_lightning/trainer/trainer.py
@@ -1054,7 +1054,7 @@ def check_testing_model_configuration(self, model: LightningModule):
 
         has_test_step = self.is_overridden('test_step', model)
         has_test_epoch_end = self.is_overridden('test_epoch_end', model)
-        gave_test_loader = hasattr(model, 'test_dataloader') and model.test_dataloader()
+        gave_test_loader = self.is_overridden('test_dataloader', model)
 
         if gave_test_loader and not has_test_step:
             raise MisconfigurationException('You passed in a `test_dataloader` but did not implement `test_step()`')


Test patch:
diff --git a/tests/trainer/test_checks.py b/tests/trainer/test_checks.py
--- a/tests/trainer/test_checks.py
+++ b/tests/trainer/test_checks.py
@@ -1,7 +1,7 @@
 import pytest
 
 import tests.base.utils as tutils
-from pytorch_lightning import Trainer
+from pytorch_lightning import Trainer, LightningModule
 from pytorch_lightning.utilities.exceptions import MisconfigurationException
 from tests.base import EvalModelTemplate
 
@@ -101,7 +101,7 @@ def test_wrong_test_settigs(tmpdir):
     # ----------------
     with pytest.raises(MisconfigurationException):
         model = EvalModelTemplate(hparams)
-        model.test_dataloader = lambda: None
+        model.test_dataloader = LightningModule.test_dataloader
         trainer.test(model)
 
     # ----------------
@@ -109,7 +109,7 @@ def test_wrong_test_settigs(tmpdir):
     # ----------------
     with pytest.raises(MisconfigurationException):
         model = EvalModelTemplate(hparams)
-        model.test_dataloader = lambda: None
+        model.test_dataloader = LightningModule.test_dataloader
         model.test_step = None
         trainer.test(model, test_dataloaders=model.dataloader(train=False))
 
@@ -118,6 +118,6 @@ def test_wrong_test_settigs(tmpdir):
     # ----------------
     with pytest.warns(RuntimeWarning):
         model = EvalModelTemplate(hparams)
-        model.test_dataloader = lambda: None
+        model.test_dataloader = LightningModule.test_dataloader
         model.test_epoch_end = None
         trainer.test(model, test_dataloaders=model.dataloader(train=False))
diff --git a/tests/trainer/test_trainer.py b/tests/trainer/test_trainer.py
--- a/tests/trainer/test_trainer.py
+++ b/tests/trainer/test_trainer.py
@@ -457,7 +457,7 @@ def test_testpass_overrides(tmpdir):
     # Misconfig when neither test_step or test_end is implemented
     with pytest.raises(MisconfigurationException, match='.*not implement `test_dataloader`.*'):
         model = EvalModelTemplate(hparams)
-        model.test_dataloader = model.test_dataloader__empty
+        model.test_dataloader = LightningModule.test_dataloader
         Trainer().test(model)
 
     # Misconfig when neither test_step or test_end is implemented