{"record":{"id":"7f635cc5d2e5abc3","repo":"Lightning-AI/pytorch-lightning","slug":"test-dataloader-must-be-implemented-to-be-used-w","errorCode":null,"errorMessage":"`test_dataloader` must be implemented to be used with the Lightning Trainer","messagePattern":"`test_dataloader` must be implemented to be used with the Lightning Trainer","errorType":"exception","errorClass":"MisconfigurationException","httpStatus":null,"severity":"error","filePath":"src/lightning/pytorch/core/hooks.py","lineNumber":513,"sourceCode":"        However, the above are only necessary for distributed processing.\n\n        .. warning:: do not assign state in prepare_data\n\n\n        - :meth:`~lightning.pytorch.trainer.trainer.Trainer.test`\n        - :meth:`prepare_data`\n        - :meth:`setup`\n\n        Note:\n            Lightning tries to add the correct sampler for distributed and arbitrary hardware.\n            There is no need to set it yourself.\n\n        Note:\n            If you don't need a test dataset and a :meth:`test_step`, you don't need to implement\n            this method.\n\n        \"\"\"\n        raise MisconfigurationException(\"`test_dataloader` must be implemented to be used with the Lightning Trainer\")\n\n    def val_dataloader(self) -> EVAL_DATALOADERS:\n        r\"\"\"An iterable or collection of iterables specifying validation samples.\n\n        For more information about multiple dataloaders, see this :ref:`section <multiple-dataloaders>`.\n\n        The dataloader you return will not be reloaded unless you set\n        :paramref:`~lightning.pytorch.trainer.trainer.Trainer.reload_dataloaders_every_n_epochs` to\n        a positive integer.\n\n        It's recommended that all data downloads and preparation happen in :meth:`prepare_data`.\n\n        - :meth:`~lightning.pytorch.trainer.trainer.Trainer.fit`\n        - :meth:`~lightning.pytorch.trainer.trainer.Trainer.validate`\n        - :meth:`prepare_data`\n        - :meth:`setup`\n\n        Note:","sourceCodeStart":495,"sourceCodeEnd":531,"githubUrl":"https://github.com/Lightning-AI/pytorch-lightning/blob/9fed5c27d2a62ff0efd6c3573599921d6ff67c14/src/lightning/pytorch/core/hooks.py#L495-L531","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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)"],"exampleFix":"# before\ntrainer.test(model)  # MisconfigurationException: no test_dataloader\n# after\nclass MyModule(LightningModule):\n    def test_dataloader(self):\n        return DataLoader(self.test_dataset)\ntrainer.test(model)\n# or trainer.test(model, test_dataloaders=test_dl)","handlingStrategy":"validation","validationCode":"def has_test_dataloader(module_or_dm) -> bool:\n    return 'test_dataloader' in type(module_or_dm).__dict__\n\nif not has_test_dataloader(model):\n    trainer.test(model, test_dataloaders=test_dl)","typeGuard":"def implements_test_dataloader(cls) -> bool:\n    \"\"\"True if cls overrides the base test_dataloader stub.\"\"\"\n    return 'test_dataloader' in cls.__dict__","tryCatchPattern":null,"preventionTips":["Always pass datamodule or test_dataloaders to trainer.test","Define test_dataloader alongside test_step in one commit"],"tags":["test-dataloader","not-implemented","lightning-module","required-method"],"backgroundTag":"abstract-method-not-implemented","analyzedSha":"9fed5c27d2a62ff0efd6c3573599921d6ff67c14","analyzedAt":"2026-08-28T11:52:41.083Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}