MCPcopy Create free account
hub / github.com/pytest-dev/pytest / copy_example

Method copy_example

src/_pytest/pytester.py:922–964  ·  view source on GitHub ↗

Copy file from project's directory into the testdir. :param str name: The name of the file to copy. :return: path to the copied directory (inside ``self.path``).

(self, name: Optional[str] = None)

Source from the content-addressed store, hash-verified

920 return p
921
922 def copy_example(self, name: Optional[str] = None) -> Path:
923 """Copy file from project's directory into the testdir.
924
925 :param str name: The name of the file to copy.
926 :return: path to the copied directory (inside ``self.path``).
927
928 """
929 example_dir = self._request.config.getini("pytester_example_dir")
930 if example_dir is None:
931 raise ValueError("pytester_example_dir is unset, can't copy examples")
932 example_dir = self._request.config.rootpath / example_dir
933
934 for extra_element in self._request.node.iter_markers("pytester_example_path"):
935 assert extra_element.args
936 example_dir = example_dir.joinpath(*extra_element.args)
937
938 if name is None:
939 func_name = self._name
940 maybe_dir = example_dir / func_name
941 maybe_file = example_dir / (func_name + ".py")
942
943 if maybe_dir.is_dir():
944 example_path = maybe_dir
945 elif maybe_file.is_file():
946 example_path = maybe_file
947 else:
948 raise LookupError(
949 f"{func_name} can't be found as module or package in {example_dir}"
950 )
951 else:
952 example_path = example_dir.joinpath(name)
953
954 if example_path.is_dir() and not example_path.joinpath("__init__.py").is_file():
955 copytree(example_path, self.path)
956 return self.path
957 elif example_path.is_file():
958 result = self.path.joinpath(example_path.name)
959 shutil.copy(example_path, result)
960 return result
961 else:
962 raise LookupError(
963 f'example "{example_path}" is not found as a file or directory'
964 )
965
966 def getnode(
967 self, config: Config, arg: Union[str, "os.PathLike[str]"]

Calls 3

copytreeFunction · 0.90
iter_markersMethod · 0.80
getiniMethod · 0.45

Tested by

no test coverage detected