API Reference

fake_subprocess

The main entrypoint class for all fake_subprocess operations is the FakeProcess class. This class is instantiated and returned when the fake_subprocess fixture is being used.

class pytest_subprocess.fake_process.FakeProcess

Main class responsible for process operations

allow_unregistered(allow)

Allow / block unregistered processes execution. When allowed, the real subprocesses will be called. Blocking will raise the exception.

Parameters:

allow (bool) – decide whether the unregistered process shall be allowed

Return type:

None

any

alias of Any

call_count(command)

Count how many times a certain command was called. Can be used together with fake_process.any().

Parameters:

command (Sequence[str | Any | PathLike | Program] | str | Command) – lookup command

Return type:

int

Returns:

number of times a command was called

classmethod context()

Return a new FakeProcess instance to use it as a context manager.

Return type:

FakeProcess

keep_last_process(keep)

Keep last process definition from being removed. That can allow / block multiple execution of the same command.

Parameters:

keep (bool) – decide whether last process shall be kept

Return type:

None

pass_command(command, occurrences=1)

Allow to use a real subprocess together with faked ones.

Parameters:
  • command (Sequence[str | Any | PathLike | Program] | str | Command) – allow to execute the supplied command

  • occurrences (int) – allow multiple usages of the same command

Return type:

None

program

alias of Program

regex

alias of Regex

register(command, stdout=None, stderr=None, returncode=0, wait=None, callback=None, callback_kwargs=None, signal_callback=None, occurrences=1, stdin_callable=None)

Main method for registering the subprocess instances.

Parameters:
  • command (Sequence[str | Any | PathLike | Program] | str | Command) – register the command that will be faked

  • stdout (str | bytes | None | Sequence[str | bytes]) – value of the standard output

  • stderr (str | bytes | None | Sequence[str | bytes]) – value of the error output

  • returncode (int) – return code of the faked process

  • wait (float | None) – artificially wait for the process to finish

  • callback (Callable | None) – function that will be executed instead of the process

  • callback_kwargs (Dict[str, Any] | None) – keyword arguments that will be passed into callback

  • occurrences (int) – allow multiple usages of the same command

  • stdin_callable (Callable | None) – function that will interact with stdin

  • signal_callback (Callable | None)

Return type:

ProcessRecorder

register_subprocess(command, stdout=None, stderr=None, returncode=0, wait=None, callback=None, callback_kwargs=None, signal_callback=None, occurrences=1, stdin_callable=None)

Main method for registering the subprocess instances.

Parameters:
  • command (Sequence[str | Any | PathLike | Program] | str | Command) – register the command that will be faked

  • stdout (str | bytes | None | Sequence[str | bytes]) – value of the standard output

  • stderr (str | bytes | None | Sequence[str | bytes]) – value of the error output

  • returncode (int) – return code of the faked process

  • wait (float | None) – artificially wait for the process to finish

  • callback (Callable | None) – function that will be executed instead of the process

  • callback_kwargs (Dict[str, Any] | None) – keyword arguments that will be passed into callback

  • occurrences (int) – allow multiple usages of the same command

  • stdin_callable (Callable | None) – function that will interact with stdin

  • signal_callback (Callable | None)

Return type:

ProcessRecorder

any()

For a non-exact matching, you can use the Any() class that is available to use via fake_subprocess.any. This class can be used to replace a number of arguments that might occur,

class pytest_subprocess.utils.Any(*, min=None, max=None)

Wildcard definition class.

Parameters:
  • min (int | None)

  • max (int | None)