Skip to content

Pytest fixtures#

Some reusable fixtures for pytest.

+++ 2.4.0

To enable them in your test, add pdm.pytest as a plugin. You can do so in your root conftest.py:

conftest.py
1
2
3
4
5
6
7
8
9
# single plugin
pytest_plugins = "pytest.plugin"

# many plugins
pytest_plugins = [
    ...
    "pdm.pytest",
    ...
]

IndexMap = Dict[str, Path] module-attribute #

Path some root-relative http paths to some local paths

IndexOverrides = Dict[str, bytes] module-attribute #

PyPI indexes overrides fixture format

IndexesDefinition = Dict[str, Union[Tuple[IndexMap, IndexOverrides, bool], IndexMap]] module-attribute #

Mock PyPI indexes format

Distribution #

A mock Distribution

LocalIndexTransport #

Bases: httpx.BaseTransport

A local file transport for HTTPX.

Allows to mock some HTTP requests with some local files

MockWorkingSet #

Bases: collections.abc.MutableMapping

A mock working set

PDMCallable #

Bases: Protocol

The PDM fixture callable signature

__call__(args, strict=False, input=None, obj=None, env=None, cleanup=True, **kwargs) #

Parameters:

Name Type Description Default
args str | list[str]

the command arguments as a single lexable string or a strings array

required
strict bool

raise an exception on failure instead of returning if enabled

False
input str | None

an optional string to be submitted too stdin

None
obj Project | None

an optional existing Project.

None
env Mapping[str, str] | None

override the environment variables with those

None

Returns:

Type Description
RunResult

The command result

RunResult dataclass #

Store a command execution result.

exception: Exception | None = None instance-attribute class-attribute #

If set, the exception raised on execution

exit_code: int instance-attribute #

The execution exit code

output: str property #

The execution stdout output (stdout alias)

outputs: str property #

The execution stdout and stderr outputs concatenated

stderr: str instance-attribute #

The execution stderr output

stdout: str instance-attribute #

The execution stdout output

print() #

A debugging facility

TestRepository #

Bases: BaseRepository

A mock repository to ease testing dependencies

build_env(build_env_wheels, tmp_path_factory) #

A fixture build environment

Parameters:

Name Type Description Default
build_env_wheels Iterable[Path]

a list of wheel to install in the environment

required

Returns:

Type Description
Path

The build environment temporary path

build_env_wheels() #

Expose some wheels to be installed in the build environment.

Override to provide your owns.

Returns:

Type Description
Iterable[Path]

a list of wheels paths to install

local_finder_artifacts() #

The local finder search path as a fixture

Override to provides your own artifacts.

Returns:

Type Description
Path

The path to the artifacts root

pdm(core, monkeypatch) #

A fixture allowing to execute PDM commands

Returns:

Type Description
PDMCallable

A pdm fixture command.

project(project_no_init) #

A fixture creating an initialized test project for the current test.

Returns:

Type Description
Project

The initialized project

project_no_init(tmp_path, mocker, core, build_test_session, monkeypatch, build_env) #

A fixture creating a non-initialized test project for the current test.

Returns:

Type Description
Project

The non-initialized project

pypi_indexes() #

Provides some mocked PyPI entries

Returns:

Type Description
IndexesDefinition

a definition of the mocked indexes

remove_pep582_path_from_pythonpath(pythonpath) #

Remove all pep582 paths of PDM from PYTHONPATH

repository(project, mocker, repository_pypi_json, local_finder) #

A fixture providing a mock PyPI repository

Returns:

Type Description
TestRepository

A mock repository

repository_pypi_json() #

The test repository fake PyPI definition path as a fixture

Override to provides your own definition path.

Returns:

Type Description
Path

The path to a fake PyPI repository JSON definition

venv_backends(project, request) #

A fixture iterating over venv backends

working_set(mocker, repository) #

a mock working set as a fixture

Returns:

Type Description
MockWorkingSet

a mock working set