Lightning-AI/pytorch-lightning · error · MisconfigurationException
`test_dataloader` must be implemented to be used with the…
Error message
`test_dataloader` must be implemented to be used with the Lightning Trainer
What it means
The base stub of test_dataloader raises MisconfigurationException when called. You must override it (on the LightningModule or the LightningDataModule in use) or supply test dataloaders explicitly, otherwise trainer.test() has no data source.
Solutions
- Implement def test_dataloader(self) returning the test DataLoader
- Or call trainer.test(model, datamodule=dm) with a DataModule that defines test_dataloader
- Or pass it directly: trainer.test(model, test_dataloaders=test_dl)
Example fix
# before
trainer.test(model) # MisconfigurationException: no test_dataloader
# after
class MyModule(LightningModule):
def test_dataloader(self):
return DataLoader(self.test_dataset)
trainer.test(model)
# or trainer.test(model, test_dataloaders=test_dl) Defensive patterns
Strategy: validation
Validate before calling
def has_test_dataloader(module_or_dm) -> bool:
return 'test_dataloader' in type(module_or_dm).__dict__
if not has_test_dataloader(model):
trainer.test(model, test_dataloaders=test_dl) Type guard
def implements_test_dataloader(cls) -> bool:
"""True if cls overrides the base test_dataloader stub."""
return 'test_dataloader' in cls.__dict__ Prevention
- Always pass datamodule or test_dataloaders to trainer.test
- Define test_dataloader alongside test_step in one commit
When it happens
Trigger: trainer.test(model) (or Trainer(limit_train_batches..) sanity flows) where neither a LightningDataModule with test_dataloader nor the module's test_dataloader is defined and no test_dataloaders= was passed.
Common situations: Running evaluation on a checkpoint whose original class didn't define test_dataloader; assuming the datamodule provides it while passing datamodule=None; typo'd method name.
Understand the failure class
Background: "NotImplementedError: Subclasses should override this method" / "must be implemented" — abstract method errors explained — this error's family across 40 libraries.
Related errors
- `train_dataloader` must be implemented to be used with the…
- All datasets are iterable-style datasets.
- DeepSpeed handles gradient clipping automatically within…
- f"An invalid dataloader was passed to `Trainer.
- Loading a single module or optimizer object from a…
AI-assisted analysis of Lightning-AI/pytorch-lightning@9fed5c27d2 (2026-08-28).
Data as JSON: /api/errors/7f635cc5d2e5abc3.
Report an issue: GitHub.
Appendix: source
Thrown at src/lightning/pytorch/core/hooks.py:513
However, the above are only necessary for distributed processing.
.. warning:: do not assign state in prepare_data
- :meth:`~lightning.pytorch.trainer.trainer.Trainer.test`
- :meth:`prepare_data`
- :meth:`setup`
Note:
Lightning tries to add the correct sampler for distributed and arbitrary hardware.
There is no need to set it yourself.
Note:
If you don't need a test dataset and a :meth:`test_step`, you don't need to implement
this method.
"""
raise MisconfigurationException("`test_dataloader` must be implemented to be used with the Lightning Trainer")
def val_dataloader(self) -> EVAL_DATALOADERS:
r"""An iterable or collection of iterables specifying validation samples.
For more information about multiple dataloaders, see this :ref:`section <multiple-dataloaders>`.
The dataloader you return will not be reloaded unless you set
:paramref:`~lightning.pytorch.trainer.trainer.Trainer.reload_dataloaders_every_n_epochs` to
a positive integer.
It's recommended that all data downloads and preparation happen in :meth:`prepare_data`.
- :meth:`~lightning.pytorch.trainer.trainer.Trainer.fit`
- :meth:`~lightning.pytorch.trainer.trainer.Trainer.validate`
- :meth:`prepare_data`
- :meth:`setup`
Note:View on GitHub (pinned to 9fed5c27d2)