Search code examples
pythonunit-testingmockingsubprocesspytest

How to mock returncode, stdout, stderr from subprocess.run with pytest?


I am trying to write unit tests with pytest to mock subprocess.run call following some other examples on here, but running into difficulty. I have the following class:

class Bmx:

    def version(self) -> Tuple[int, int, int]:
        cmd = [self.exec_path, '-v']
        err, out = self._run(cmd)
        version = re.search(r'v\d+\.\d+\.\d+', out, re.IGNORECASE)[0][1:].split('.')
        return int(version[0]), int(version[1]), int(version[2])

    @staticmethod
    def _run(cmd: List[str], shell: bool = False) -> Tuple[str, str]:
        proc = subprocess.run(cmd, stderr=subprocess.PIPE, stdout=subprocess.PIPE, shell=shell, universal_newlines=True)
        if proc.returncode > 0:
            raise BmxError(proc.stderr)
        return proc.stderr, proc.stdout

I tried to write a unit test for the version method as follows:

from unittest.mock import MagicMock, patch

import pytest

from pybmx.bmx import Bmx


@patch('pybmx.bmx.subprocess.run')
def test_version(mock_run):
    mock_proc = MagicMock()
    mock_proc.configure_mock(**{
        'returncode.return_value': 0,
        'stdout.return_value': 'raw2bmx, bmx v1.1.6, Mar  3 2023 10:02:28 (scm v1.1-6-g43fac95-dirty)'
    })
    mock_run.return_value = mock_proc
    assert Bmx('raw2bmx').version() == (1, 1, 6)

However the test fails with the following:

    @staticmethod
    def _run(cmd: List[str], shell: bool = False) -> Tuple[str, str]:
        proc = subprocess.run(cmd, stderr=subprocess.PIPE, stdout=subprocess.PIPE, shell=shell, universal_newlines=True)
>       if proc.returncode > 0:
E       TypeError: '>' not supported between instances of 'MagicMock' and 'int'

I thought that by configuring the returncode.return_value using the MagicMock configure method I would be mocking the return code, but it seems I am not understanding this correctly. I'm also wondering if there is a way to achieve this with pytest rather than unittest. Any advice would be appreciated! Thanks.


Solution

  • The problem seems to be that mock_proc.configure_mock(**{"f.return_value": ...}) is for mocking methods but proc.returncode is an attribute. Mocking attributes is even easier, though (see the docs):

    >>> m = MagicMock(attribute=3, other='fish')
    >>> m.attribute
    3
    >>> m.other
    'fish'