fixtures#
Collection of pytest fixtures used in conda tests.
Classes#
Fixture for calling pip in specific conda environments. |
|
Fixture providing HTTP test server for serving local files. |
Functions#
Suppress Unclosed Socket Warning |
|
|
|
Resets the context object after each test function is run. |
|
|
Used to isolate package or index cache from other tests. |
A parameterized fixture that sets the solver backend to (1) libmamba |
|
|
|
|
|
|
|
|
|
|
A function scoped fixture returning CondaCLIFixture instance. |
|
A session scoped fixture returning CondaCLIFixture instance. |
|
A function scoped fixture returning PipCLIFixture instance. |
|
A function scoped fixture returning PathFactoryFixture instance. |
|
A function scoped fixture returning TmpEnvFixture instance. |
|
A function scoped fixture returning an empty environment. |
|
A session scoped fixture returning TmpEnvFixture instance. |
|
A function scoped fixture returning TmpChannelFixture instance. |
|
A monkeypatch fixture that resets context after each test. |
|
A function scoped fixture returning a temporary package cache directory. |
|
A function scoped fixture returning a temporary environment directory. |
We need to set this so Python loads the dev version of 'conda', usually taken |
|
|
|
|
Function-scoped HTTP test server for serving local files. |
Attributes#
- TEST_CONDARC#
- suppress_resource_warning()#
Suppress Unclosed Socket Warning
It seems urllib3 keeps a socket open to avoid costly recreation costs.
- tmpdir(tmpdir, request)#
- clear_subdir_cache()#
- reset_conda_context()#
Resets the context object after each test function is run.
- temp_package_cache(tmp_path_factory, monkeypatch: pytest.MonkeyPatch) collections.abc.Iterator[pathlib.Path]#
Used to isolate package or index cache from other tests.
- parametrized_solver_fixture(request: pytest.FixtureRequest) collections.abc.Iterable[Literal[libmamba, classic]]#
A parameterized fixture that sets the solver backend to (1) libmamba and (2) classic for each test. It's using autouse=True, so only import it in modules that actually need it.
Note that skips and xfails need to be done _inside_ the test body. Decorators can't be used because they are evaluated before the fixture has done its work!
So, instead of:
@pytest.mark.skipif(context.solver == "libmamba", reason="...") def test_foo():
...
Do:
- def test_foo():
- if context.solver == "libmamba":
pytest.skip("...")
...
- solver_classic(request: pytest.FixtureRequest) collections.abc.Iterable[Literal[classic]]#
- solver_libmamba(request: pytest.FixtureRequest) collections.abc.Iterable[Literal[libmamba]]#
- Solver#
- _solver_helper(request: pytest.FixtureRequest, solver: Solver) collections.abc.Iterable[Solver]#
- session_capsys(request) collections.abc.Iterator[_pytest.capture.MultiCapture]#
- class CondaCLIFixture#
-
- __call__(*argv: conda.common.path.PathType, raises: type[Exception] | tuple[type[Exception], Ellipsis]) tuple[str, str, pytest.ExceptionInfo]#
- __call__(*argv: conda.common.path.PathType) tuple[str, str, int]
Test conda CLI. Mimic what is done in conda.cli.main.main.
conda ... == conda_cli(...)
- Parameters:
argv -- Arguments to parse.
raises -- Expected exception to intercept. If provided, the raised exception will be returned instead of exit code (see pytest.raises and pytest.ExceptionInfo).
- Returns:
Command results (stdout, stderr, exit code or pytest.ExceptionInfo).
- static _cast_args(argv: tuple[conda.common.path.PathType, Ellipsis]) collections.abc.Iterable[str]#
Cast args to string.
- conda_cli(capsys: pytest.CaptureFixture) collections.abc.Iterator[CondaCLIFixture]#
A function scoped fixture returning CondaCLIFixture instance.
Use this for any commands that are local to the current test (e.g., creating a conda environment only used in the test).
- session_conda_cli() collections.abc.Iterator[CondaCLIFixture]#
A session scoped fixture returning CondaCLIFixture instance.
Use this for any commands that are global to the test session (e.g., creating a conda environment shared across tests, conda info, etc.).
- class PipCLIFixture#
Fixture for calling pip in specific conda environments.
- __call__(*argv: conda.common.path.PathType, prefix: conda.common.path.PathType, raises: type[Exception] | tuple[type[Exception], Ellipsis]) tuple[str, str, pytest.ExceptionInfo]#
- __call__(*argv: conda.common.path.PathType, prefix: conda.common.path.PathType) tuple[str, str, int]
Test pip CLI in a specific conda environment.
pip ... in environment == pip_cli(..., prefix=env_path)
- Parameters:
argv -- Arguments to pass to pip.
prefix -- Path to the conda environment containing pip.
raises -- Expected exception to intercept. If provided, the raised exception will be returned instead of exit code (see pytest.raises and pytest.ExceptionInfo).
- Returns:
Command results (stdout, stderr, exit code or pytest.ExceptionInfo).
- pip_cli() collections.abc.Iterator[PipCLIFixture]#
A function scoped fixture returning PipCLIFixture instance.
Use this for calling pip commands in specific conda environments during tests. Uses python -m pip for reliable cross-platform execution.
Example
- def test_pip_install(tmp_env, pip_cli):
- with tmp_env("python=3.10", "pip") as prefix:
stdout, stderr, code = pip_cli("install", "requests", prefix=prefix) assert code == 0
- class PathFactoryFixture#
- tmp_path: pathlib.Path#
- __call__(name: str | None = None, *, prefix: str | None = None, infix: str | None = None, suffix: str | None = None) pathlib.Path#
Unique, non-existent path factory.
Extends pytest's tmp_path fixture with a new unique, non-existent path for usage in cases where we need a temporary path that doesn't exist yet.
- Default behavior (no arguments):
path_factory()→tmp_path/ab12cd34ef56(12-char UUID)
Two modes of operation (mutually exclusive):
Name mode: Pass a complete path name.
path_factory("myfile.txt")→tmp_path/myfile.txtParts mode: Pass prefix/infix/suffix; unspecified parts get UUID defaults.
path_factory(infix="!")→tmp_path/ab12!ef56path_factory(suffix=".yml")→tmp_path/ab12cd34.yml
- Parameters:
name -- Complete path name (mutually exclusive with prefix/infix/suffix)
prefix -- Prefix for generated name (mutually exclusive with name param)
infix -- Infix for generated name (mutually exclusive with name param)
suffix -- Suffix for generated name (mutually exclusive with name param)
- Returns:
A new unique path
- path_factory(tmp_path: pathlib.Path) collections.abc.Iterator[PathFactoryFixture]#
A function scoped fixture returning PathFactoryFixture instance.
Use this to generate any number of temporary paths for the test that are unique and do not exist yet.
- class TmpEnvFixture#
- path_factory: PathFactoryFixture | pytest.TempPathFactory#
- conda_cli: CondaCLIFixture#
- get_path(name: str | None = None, prefix: str | None = None, infix: str | None = None, suffix: str | None = None) pathlib.Path#
- __call__(*args: str, prefix: str | os.PathLike | None = None, name: str | None = None, path_prefix: str | None = None, path_infix: str | None = None, path_suffix: str | None = None, shallow: bool | None = None) collections.abc.Iterator[pathlib.Path]#
Generate a conda environment with the provided packages.
Path customization (mutually exclusive options):
Auto-generated path (default): Unique path in tmp_path.
tmp_env()→tmp_path/ab12cd34ef56(12-char UUID)Custom prefix: Specify exact location.
tmp_env(prefix="/path/to/env")→/path/to/envName mode: Specify env name directly.
tmp_env(name="my-test-env")→tmp_path/my-test-envParts mode: Customize path name generation (useful for special char testing).
tmp_env(path_infix="!")→tmp_path/ab12!ef56
- Parameters:
args -- Arguments to pass to conda create (e.g., packages, flags)
prefix -- Exact prefix path (mutually exclusive with name/path_* params)
name -- Env name (mutually exclusive with prefix/path_* params)
path_prefix -- Prefix for path name (mutually exclusive with prefix/name params)
path_infix -- Infix for path name (mutually exclusive with prefix/name params)
path_suffix -- Suffix for path name (mutually exclusive with prefix/name params)
shallow -- If True, create env on disk only without conda create
- Returns:
The conda environment's prefix
- tmp_env(path_factory: PathFactoryFixture, conda_cli: CondaCLIFixture) collections.abc.Iterator[TmpEnvFixture]#
A function scoped fixture returning TmpEnvFixture instance.
Use this when creating a conda environment that is local to the current test.
- empty_env(tmp_env: TmpEnvFixture) pathlib.Path#
A function scoped fixture returning an empty environment.
Use this when creating a conda environment that is empty.
- session_tmp_env(tmp_path_factory: pytest.TempPathFactory, session_conda_cli: CondaCLIFixture) collections.abc.Iterator[TmpEnvFixture]#
A session scoped fixture returning TmpEnvFixture instance.
Use this when creating a conda environment that is shared across tests.
- class TmpChannelFixture#
- path_factory: PathFactoryFixture#
- conda_cli: CondaCLIFixture#
- __call__(*specs: str) collections.abc.Iterator[tuple[pathlib.Path, str]]#
- tmp_channel(path_factory: PathFactoryFixture, conda_cli: CondaCLIFixture) collections.abc.Iterator[TmpChannelFixture]#
A function scoped fixture returning TmpChannelFixture instance.
- context_aware_monkeypatch(monkeypatch: pytest.MonkeyPatch) pytest.MonkeyPatch#
A monkeypatch fixture that resets context after each test.
- tmp_pkgs_dir(path_factory: PathFactoryFixture, mocker: pytest_mock.MockerFixture) collections.abc.Iterator[pathlib.Path]#
A function scoped fixture returning a temporary package cache directory.
- tmp_envs_dir(path_factory: PathFactoryFixture, mocker: pytest_mock.MockerFixture) collections.abc.Iterator[pathlib.Path]#
A function scoped fixture returning a temporary environment directory.
- PYTHONPATH()#
We need to set this so Python loads the dev version of 'conda', usually taken from conda/ in the root of the cloned repo. This root is usually the working directory when we run pytest. Otherwise, it will import the one installed in the base environment, which might have not been overwritten with pip install -e . --no-deps. This doesn't happen in other tests because they run with the equivalent of python -m conda. However, some tests directly run conda (shell function) which calls `conda (Python entry point). When a script is called this way, it bypasses the automatic "working directory is first on sys.path" behavior you find in python -m style calls. See https://docs.python.org/3/library/sys_path_init.html for details.
- class HttpTestServerFixture#
Fixture providing HTTP test server for serving local files.
- server: http.server.ThreadingHTTPServer#
- directory: pathlib.Path#
- __post_init__()#
Log server startup for debugging.
- http_test_server(request: pytest.FixtureRequest, path_factory: PathFactoryFixture) collections.abc.Iterator[HttpTestServerFixture]#
Function-scoped HTTP test server for serving local files.
This fixture starts an HTTP server on a random port and serves files from a directory. The server supports both IPv4 and IPv6.
- Usage without parametrize (dynamic content):
- def test_dynamic(http_test_server):
# Server uses temporary directory automatically (http_test_server.directory / "file.txt").write_text("content") url = http_test_server.get_url("file.txt") response = requests.get(url) assert response.status_code == 200
- Usage with parametrize (pre-existing directory):
@pytest.mark.parametrize("http_test_server", ["tests/data/mock-channel"], indirect=True) def test_existing(http_test_server):
url = http_test_server.get_url("file.txt") response = requests.get(url) assert response.status_code == 200
- Use
Nonein parametrize to mix pre-existing directories with dynamic content: @pytest.mark.parametrize("http_test_server", ["tests/data", None], indirect=True)
- Parameters:
request -- pytest fixture request object
path_factory -- path_factory fixture for creating unique temp directories
- Returns:
HttpTestServerFixture with server, host, port, url, and directory attributes
- Raises:
ValueError -- If parametrized directory is invalid