2024-heraklion-testing-debu.../extra_slides/mocking/telescope/test_telescope_model_with_mock.py

30 lines
885 B
Python
Raw Normal View History

2024-08-26 13:54:13 +02:00
from unittest import mock
import numpy as np
from py.test import raises
from telescope_model import TelescopeModel
def test_unsafe_elevation_angle():
with mock.patch('telescope_model.telescope_driver'):
telescope = TelescopeModel(address='10.2.1.1')
elevation_angle = np.pi / 2.0
with raises(ValueError):
telescope.set_elevation_angle(elevation_angle)
def test_model_initialization():
connection_id = 'bogus_connection'
initial_angle = 1.23
with mock.patch('telescope_model.telescope_driver') as driver:
driver.connect.return_value = connection_id
driver.get_angle.return_value = initial_angle
telescope = TelescopeModel(address='10.2.1.1')
assert telescope.connection == connection_id
assert driver.connect.call_count == 1
assert telescope.current_angle == initial_angle