Skip to content
Permalink
Browse files

tests: refactor to remove monkeypatch usage

With pytest-cov, the monkeypatch fixture is not needed and mocker.patch()
should be used instead. Refactor the tests accordingly.

Signed-off-by: Jan Luebbe <jlu@pengutronix.de>
  • Loading branch information...
jluebbe authored and Emantor committed Aug 8, 2019
1 parent 40a289c commit d6ca32243baf3752a281f34935a6437c69356972
Showing with 28 additions and 41 deletions.
  1. +3 −6 tests/test_onewire.py
  2. +25 −35 tests/test_serialdriver.py
@@ -10,12 +10,9 @@ def onewire_port(target):
return OneWirePIO(target, "pio", "testhost", "29.123450000000/PIO.6")

@pytest.fixture(scope='function')
def onewire_driver(target, onewire_port, monkeypatch, mocker):
onewire_mock = mocker.MagicMock
onewire_mock.set = mocker.MagicMock()
onewire_mock.get = mocker.MagicMock(return_value='1')
import onewire
monkeypatch.setattr(onewire, 'Onewire', onewire_mock)
def onewire_driver(target, onewire_port, mocker):
onewire_mock = mocker.patch('onewire.Onewire')
onewire_mock.return_value.get.return_value = '1'
s = OneWirePIODriver(target, "pio")
target.activate(s)
return s
@@ -1,5 +1,4 @@
import pytest
import serial

from labgrid.driver import SerialDriver
from labgrid.exceptions import NoSupplierFoundError
@@ -10,64 +9,55 @@ def test_instanziation_fail_missing_port(self, target):
with pytest.raises(NoSupplierFoundError):
SerialDriver(target, "serial")

def test_instanziation(self, target, serial_port, monkeypatch, mocker):
serial_mock = mocker.Mock()
monkeypatch.setattr(serial, 'Serial', serial_mock)
def test_instanziation(self, target, serial_port, mocker):
serial_mock = mocker.patch('serial.Serial')
s = SerialDriver(target, "serial")
assert (isinstance(s, SerialDriver))
assert (target.drivers[0] == s)

def test_write(self, target, monkeypatch, serial_port, mocker):
serial_mock = mocker.Mock()
serial_mock.write = mocker.MagicMock()
monkeypatch.setattr(serial, 'Serial', serial_mock)
def test_write(self, target, serial_port, mocker):
serial_mock = mocker.patch('serial.Serial')
s = SerialDriver(target, "serial")
s.serial = serial_mock
target.activate(s)
s.write(b"testdata")
serial_mock.write.assert_called_with(b"testdata")
serial_mock.return_value.open.assert_called_once_with()
serial_mock.return_value.write.assert_called_once_with(b"testdata")

def test_read(self, target, monkeypatch, serial_port, mocker):
serial_mock = mocker.Mock()
serial_mock.read = mocker.MagicMock()
serial_mock.in_waiting = 0
monkeypatch.setattr(serial, 'Serial', serial_mock)
def test_read(self, target, serial_port, mocker):
serial_mock = mocker.patch('serial.Serial')
serial_mock.return_value.in_waiting = 0
s = SerialDriver(target, "serial")
s.serial = serial_mock
target.activate(s)
s.read()
assert (serial_mock.read.called)
serial_mock.return_value.open.assert_called_once_with()
serial_mock.return_value.read.assert_called_once_with(1)

def test_close(self, target, monkeypatch, serial_port, mocker):
serial_mock = mocker.Mock()
serial_mock.close = mocker.MagicMock()
monkeypatch.setattr(serial, 'Serial', serial_mock)
def test_close(self, target, serial_port, mocker):
serial_mock = mocker.patch('serial.Serial')
s = SerialDriver(target, "serial")
s.serial = serial_mock
target.activate(s)
s.close()
assert (serial_mock.close.called)
serial_mock.return_value.open.assert_called_once_with()
serial_mock.return_value.close.assert_called_once_with()

def test_deactivate(self, target, monkeypatch, serial_port, mocker):
serial_mock = mocker.Mock()
serial_mock.close = mocker.MagicMock()
monkeypatch.setattr(serial, 'Serial', serial_mock)
def test_deactivate(self, target, serial_port, mocker):
serial_mock = mocker.patch('serial.Serial')
s = SerialDriver(target, "serial")
s.serial = serial_mock
target.activate(s)
target.deactivate(s)
assert (serial_mock.close.called)
serial_mock.return_value.open.assert_called_once_with()
serial_mock.return_value.close.assert_called_once_with()

def test_rfc2711_instanziation(self, target, serial_rfc2711_port, monkeypatch, mocker):
serial_mock = mocker.Mock()
monkeypatch.setattr(serial, 'Serial', serial_mock)
def test_rfc2711_instanziation(self, target, serial_rfc2711_port, mocker):
serial_mock = mocker.patch('serial.rfc2217.Serial')
s = SerialDriver(target, "serial")
assert (isinstance(s, SerialDriver))
assert (target.drivers[0] == s)
serial_mock.assert_called_once_with()

def test_raw_instanziation(self, target, serial_raw_port, monkeypatch, mocker):
serial_mock = mocker.Mock()
monkeypatch.setattr(serial, 'Serial', serial_mock)
def test_raw_instanziation(self, target, serial_raw_port, mocker):
serial_mock = mocker.patch('serial.serial_for_url')
s = SerialDriver(target, "serial")
assert (isinstance(s, SerialDriver))
assert (target.drivers[0] == s)
serial_mock.assert_called_once_with('socket://', do_not_open=True)

0 comments on commit d6ca322

Please sign in to comment.
You can’t perform that action at this time.