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 (Union[Sequence[Union[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 (Union[Sequence[Union[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

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 (Union[Sequence[Union[str, Any, PathLike, Program]], str, Command]) – register the command that will be faked

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

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

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

  • wait (Optional[float]) – artificially wait for the process to finish

  • callback (Optional[Callable]) – function that will be executed instead of the process

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

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

  • stdin_callable (Optional[Callable]) – function that will interact with stdin

  • signal_callback (Optional[Callable]) –

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 (Union[Sequence[Union[str, Any, PathLike, Program]], str, Command]) – register the command that will be faked

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

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

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

  • wait (Optional[float]) – artificially wait for the process to finish

  • callback (Optional[Callable]) – function that will be executed instead of the process

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

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

  • stdin_callable (Optional[Callable]) – function that will interact with stdin

  • signal_callback (Optional[Callable]) –

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 (Optional[int]) –

  • max (Optional[int]) –