Я знаю, что могу использовать макет .MagicMock() и это то, что я использовал. Теперь я хочу получать разные операции стандартного вывода для каждого вызова, и я застрял.
псевдокод:
Код: Выделить всё
def send_command(self, cmd)
self.client.connect(hostname=self.host,
username=self.user,
password=self.password,
timeout=self.timeout)
stdin, stdout, stderr = self.client.exec_command(cmd)
return stdin, stdout, stderr
def func():
stdin, stdout, stderr = report.send_command('ls -la')
resp = stdout.read().decode()
stdin, stdout, stderr = report.send_command('pwd')
resp2 = stdout.read().decode()
return [resp, resp2]
Код: Выделить всё
@pytest.fixture
def mock_paramiko_ssh_client(items):
"""Mocks the paramiko SSHClient.s"""
with mock.patch('paramiko.SSHClient', autospec=True) as mock_client:
# Mock exec_command()
stdin = mock.MagicMock()
stdout = mock.MagicMock()
stderr = mock.MagicMock()
stdout.read.side_effect= items.param
stderr.read.return_value = b''
mock_client.return_value.exec_command.side_effect = (stdin, stdout, stderr)
yield mock_client
@pytest.mark.parametrize(mock_paramiko_ssh_client, [[b'file1', b'/root']], indirect=True)
def test():
result = func()
assert (result == ['file1', '/root']
Подробнее здесь: https://stackoverflow.com/questions/790 ... de-effects