{"id":"26e09bd019fc271b","repo":"pytest-dev/pytest","slug":"pytester-example-dir-is-unset-can-t-copy-examples","errorCode":null,"errorMessage":"pytester_example_dir is unset, can't copy examples","messagePattern":"pytester_example_dir is unset, can't copy examples","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/_pytest/pytester.py","lineNumber":958,"sourceCode":"        gets recognised as a Python package.\n        \"\"\"\n        p = self.path / name\n        p.mkdir()\n        p.joinpath(\"__init__.py\").touch()\n        return p\n\n    def copy_example(self, name: str | None = None) -> Path:\n        \"\"\"Copy file from project's directory into the testdir.\n\n        :param name:\n            The name of the file to copy.\n        :return:\n            Path to the copied directory (inside ``self.path``).\n        :rtype: pathlib.Path\n        \"\"\"\n        example_dir_ = self._request.config.getini(\"pytester_example_dir\")\n        if example_dir_ is None:\n            raise ValueError(\"pytester_example_dir is unset, can't copy examples\")\n        example_dir: Path = self._request.config.rootpath / example_dir_\n\n        for extra_element in self._request.node.iter_markers(\"pytester_example_path\"):\n            assert extra_element.args\n            example_dir = example_dir.joinpath(*extra_element.args)\n\n        if name is None:\n            func_name = self._name\n            maybe_dir = example_dir / func_name\n            maybe_file = example_dir / (func_name + \".py\")\n\n            if maybe_dir.is_dir():\n                example_path = maybe_dir\n            elif maybe_file.is_file():\n                example_path = maybe_file\n            else:\n                raise LookupError(\n                    f\"{func_name} can't be found as module or package in {example_dir}\"","sourceCodeStart":940,"sourceCodeEnd":976,"githubUrl":"https://github.com/pytest-dev/pytest/blob/98b357f69e380da908740a212288d73b2ee06687/src/_pytest/pytester.py#L940-L976","documentation":"Raised by Pytester.copy_example when the ini option 'pytester_example_dir' is not configured. copy_example copies reference example files from a project-local directory into the test's temp dir, and that source directory is declared via pytester_example_dir in your pytest configuration. Without it there is no root from which to resolve examples.","triggerScenarios":"Calling pytester.copy_example() in a test without having set pytester_example_dir in pyproject.toml's [tool.pytest.ini_options], pytest.ini, or tox.ini.","commonSituations":"Using the pytester fixture's example-copying feature for the first time; a CI config that uses a different ini file than the dev machine; migrating config and dropping the option.","solutions":["Add 'pytester_example_dir = examples' (or your chosen dir) under [tool.pytest.ini_options] in pyproject.toml.","Create the referenced examples directory at the repository root (relative to rootpath).","Use the pytester_example_path marker to point at a subdirectory if examples live deeper."],"exampleFix":"// before (pyproject.toml)\n[tool.pytest.ini_options]\n# missing pytester_example_dir\n// after\n[tool.pytest.ini_options]\npytester_example_dir = \"examples\"","handlingStrategy":"validation","validationCode":"from pathlib import Path\nimport pytest\n\ndef get_example_dir(config) -> Path | None:\n    d = config.getini(\"pytester_example_dir\")\n    return Path(config.rootpath / d) if d else None\n\n# in a test\nif get_example_dir(pytestconfig) is None:\n    pytest.skip(\"set pytester_example_dir to use copy_example\")","typeGuard":null,"tryCatchPattern":"try:\n    pytester.copy_example(\"foo.py\")\nexcept ValueError:\n    pytest.skip(\"pytester_example_dir not configured\")","preventionTips":["Add pytester_example_dir to your ini config when adopting copy_example.","Commit the referenced examples directory to the repo.","Document the required ini option for contributors."],"tags":["pytester","configuration","examples"],"analyzedSha":"98b357f69e380da908740a212288d73b2ee06687","analyzedAt":"2026-08-04T20:26:34.442Z","schemaVersion":2}