Lightning-AI/pytorch-lightning · error · MisconfigurationException

`test_dataloader` must be implemented to be used with the Li

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.

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)

Solutions

  1. Implement def test_dataloader(self) returning the test DataLoader
  2. Or call trainer.test(model, datamodule=dm) with a DataModule that defines test_dataloader
  3. 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

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.

Related errors


AI-assisted analysis of Lightning-AI/pytorch-lightning@9fed5c27d2 (2026-08-28). Data as JSON: /api/errors/7f635cc5d2e5abc3. Report an issue: GitHub.