Skip to content

Instantly share code, notes, and snippets.

@alysivji
Created April 21, 2018 15:39
Show Gist options
  • Save alysivji/a2535eae67a5569fa0169766c228f818 to your computer and use it in GitHub Desktop.
Save alysivji/a2535eae67a5569fa0169766c228f818 to your computer and use it in GitHub Desktop.
import pytest
import interface
@pytest.fixture(scope='session')
def run_command_mock(mocker):
def _create_run_command_mock(mock_return_value):
mock_run = mocker.MagicMock(name='run command mock')
mock_run.return_value = "foo"
run_command_patch = mocker.patch('interface.run_command', new=mock_run)
return run_command_patch
return _create_run_command_mock
@jeffsilverm
Copy link

Where does mocker come from?

jeffs@jeffs-desktop:/home/jeffs/python/nbmdt  (development) *  $ python3 -m pytest test/test_interface_pytest.py 
========================================================================= test session starts =========================================================================
platform linux -- Python 3.6.5, pytest-3.3.1, py-1.5.2, pluggy-0.6.0
rootdir: /home/jeffs/python/nbmdt, inifile:
collected 1 item                                                                                                                                                      

test/test_interface_pytest.py E                                                                                                                                 [100%]

=============================================================================== ERRORS ================================================================================
__________________________________________________________________ ERROR at setup of test_init_eno1 ___________________________________________________________________
file /home/jeffs/python/nbmdt/test/test_interface_pytest.py, line 50
  def test_init_eno1(run_command_mock):
file /home/jeffs/python/nbmdt/test/test_interface_pytest.py, line 37
  @pytest.fixture(scope='session')
  def run_command_mock(mocker):
E       fixture 'mocker' not found
>       available fixtures: cache, capfd, capfdbinary, caplog, capsys, capsysbinary, doctest_namespace, monkeypatch, pytestconfig, record_xml_property, recwarn, run_command_mock, tmpdir, tmpdir_factory
>       use 'pytest --fixtures [testpath]' for help on them.

/home/jeffs/python/nbmdt/test/test_interface_pytest.py:37
======================================================================= 1 error in 0.02 seconds =======================================================================
jeffs@jeffs-desktop:/home/jeffs/python/nbmdt  (development) *  $ 

`

I combined all of the relevant python software into a single file:

import pytest
import sys
from typing import List
import subprocess

class Interface(object):

    def run_command(self, command : List[str] )->str:
        """
         Run the command on the CLI

         :param command:
         :return:
         """

        completed = subprocess.run(command,
                                   stdin=None,
                                   input=None,
                                   stdout=subprocess.PIPE, stderr=None, shell=False, timeout=None,
                                   check=False)
        completed_str = completed.stdout.decode('ascii')
        return completed_str

    def __init__(self, name: str) -> None:
        print(f"In the constructor for Interface, name is {name}", file=sys.stderr)
        link_str: str = self.run_command(["ip", "link", "show", name])
        if link_str == "foo":
            print("run_command was mocked", file=sys.stderr)
        elif "eno1:" in link_str :
            print("run_command ran for real and did it right", file=sys.stderr)
        else:
            print(f"something else happened in the run_command, which returned {link_str}", file=sys.stderr)
        self.name = link_str



@pytest.fixture(scope='session')
def run_command_mock(mocker):
    print("In run_command_mock", file=sys.stderr)
    def _create_run_command_mock(mock_return_value):
        print("In run_command_mock._create_run_command_mock", file=sys.stderr)
        mock_run = mocker.MagicMock(name='run command mock')
        mock_run.return_value = "foo"
        run_command_patch = mocker.patch('interface.run_command', new=mock_run)
        return run_command_patch
    
    return _create_run_command_mock


def test_init_eno1(run_command_mock):
    print("In test_init_eno1", file=sys.stderr)
    eno1_obj = Interface("eno1")
    assert eno1_obj.name == "foo", f"run_command was not mocked, return is {eno1_obj.name}."

I'm sorry it so long to get back to you. I appreciate your kind assistance.

@alysivji
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment