{"record":{"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/0d6fbdeffa57c796123f62f81f7dd370d9b7ecdc/src/_pytest/pytester.py#L940-L976","documentation":"Thrown by Pytester.copy_example() when the `pytester_example_dir` ini option is not set. copy_example() needs a project-level directory to find example files; without it there is nowhere to copy from.","triggerScenarios":"Calling pytester.copy_example() in a test where pytest.ini/tox.ini/setup.cfg does not define pytester_example_dir; running tests in a downstream project that reuses your test suite but didn't carry over the ini setting.","commonSituations":"Copy-pasting a pytester test that uses copy_example into a new project without adding the ini key; upgrading pytest and forgetting that the ini option name must match exactly.","solutions":["Add `pytester_example_dir = example` (or your dir) under [pytest] in pytest.ini / pyproject.toml / tox.ini.","If examples live elsewhere, point pytester_example_dir at the correct relative path from rootdir.","Skip the test when the ini is unset: pytest.importorskip or a custom skip guard."],"exampleFix":"// before - pytest.ini missing the key\n[pytest]\naddopts = -ra\n// after\n[pytest]\naddopts = -ra\npytester_example_dir = examples","handlingStrategy":"validation","validationCode":"example_dir = pytester._request.config.getini('pytester_example_dir')\nif example_dir is None:\n    pytest.skip('pytester_example_dir not configured')\npytester.copy_example()","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Add pytester_example_dir to [pytest] in pytest.ini when using copy_example.","Skip tests that require examples when the ini is unset.","Document the ini dependency in the test module docstring."],"tags":["pytester","copy-example","ini-config","config"],"backgroundTag":null,"analyzedSha":"0d6fbdeffa57c796123f62f81f7dd370d9b7ecdc","analyzedAt":"2026-08-11T20:52:36.969Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}