Marco Buttu, EuroPython 2015
Marco Buttu @ EuroPython 2015, Bilbao
$ setup KK # Set the K-band receiver (wait a bit...)
$ is_starting
True
$ commanded_setup
KK
$ actual_setup
None
$ is_ready # At some point the setup is completed
True
$ actual_setup
KK
@pytest.fixture
def component():
from myframework import services
return services.get_component('component_name')
def test_setup(component):
component.cli('setup KK')
assert component.cli('is_starting') == 'True'
assert component.cli('commanded_setup') == 'KK'
assert component.cli('actual_setup') == 'None'
wait_until(component.is_ready)
assert component.cli('is_ready') == 'True'
assert component.cli('actual_setup') == 'KK'
@pytest.fixture
def component():
from myframework import get_component
mycomponent = get_component('component_name')
mycomponent.setup('KK')
return mycomponent
def test_is_starting(component):
assert component.is_starting()
wait_until(component.is_ready)
assert not component.is_starting()
@pytest.fixture
def component():
from myframework import get_component
mycomponent = get_component('component_name')
mycomponent.setup('KK')
return mycomponent
def test_setup_set_device_position(component):
expected_pos = component.conf.position()
wait_until(component.is_ready)
actual_pos = component.get_position()
assert math.isclose(actual_pos, expected_pos) # Py3.5
class MyComponent(object):
def __init__(self):
self.device = get_component('low_level_device')
self.conf = Configuration()
def setup(self, setup_code):
# ... At some point:
p = self.conf.get_position(setup_code)
self.device.set_position(p)
Sometimes the external resources are not available during development.
@pytest.fixture
def component():
from myframework import get_component
mycomponent = get_component('component_name')
mycomponent.device.set_position = unittest.mock.MagicMock()
mycomponent.setup('KK')
return component
def test_setup_set_device_position(component):
wait_until(component.is_ready)
expected = component.conf.position('KK')
component.device.set_position.assert_called_with(expected)