Fix slow tests (#3444)

* Fix RFXtrx tests

* Report slow tests on CI

* Minor rfxtrx clean up

* rfxtrx test tweak
This commit is contained in:
Paulus Schoutsen 2016-09-18 21:40:37 -07:00 committed by GitHub
parent 256062fd99
commit 87fe83dcb9
3 changed files with 18 additions and 18 deletions

View File

@ -153,13 +153,15 @@ def setup(hass, config):
if dummy_connection: if dummy_connection:
RFXOBJECT =\ RFXOBJECT =\
rfxtrxmod.Core(device, handle_receive, debug=debug, rfxtrxmod.Connect(device, handle_receive, debug=debug,
transport_protocol=rfxtrxmod.DummyTransport2) transport_protocol=rfxtrxmod.DummyTransport2)
else: else:
RFXOBJECT = rfxtrxmod.Core(device, handle_receive, debug=debug) RFXOBJECT = rfxtrxmod.Connect(device, handle_receive, debug=debug)
def _shutdown_rfxtrx(event): def _shutdown_rfxtrx(event):
"""Close connection with RFXtrx."""
RFXOBJECT.close_connection() RFXOBJECT.close_connection()
hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, _shutdown_rfxtrx) hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, _shutdown_rfxtrx)
return True return True

View File

@ -1,7 +1,7 @@
"""Th tests for the Rfxtrx component.""" """Th tests for the Rfxtrx component."""
# pylint: disable=too-many-public-methods,protected-access # pylint: disable=too-many-public-methods,protected-access
import unittest import unittest
import time from unittest.mock import patch
from homeassistant.bootstrap import _setup_component from homeassistant.bootstrap import _setup_component
from homeassistant.components import rfxtrx as rfxtrx from homeassistant.components import rfxtrx as rfxtrx
@ -13,17 +13,16 @@ class TestRFXTRX(unittest.TestCase):
def setUp(self): def setUp(self):
"""Setup things to be run when tests are started.""" """Setup things to be run when tests are started."""
self.hass = get_test_home_assistant(0) self.hass = get_test_home_assistant()
def tearDown(self): def tearDown(self):
"""Stop everything that was started.""" """Stop everything that was started."""
rfxtrx.RECEIVED_EVT_SUBSCRIBERS = [] rfxtrx.RECEIVED_EVT_SUBSCRIBERS = []
rfxtrx.RFX_DEVICES = {} rfxtrx.RFX_DEVICES = {}
if rfxtrx.RFXOBJECT:
rfxtrx.RFXOBJECT.close_connection()
self.hass.stop() self.hass.stop()
def test_default_config(self): @patch('RFXtrx.sleep')
def test_default_config(self, mock_sleep):
"""Test configuration.""" """Test configuration."""
self.assertTrue(_setup_component(self.hass, 'rfxtrx', { self.assertTrue(_setup_component(self.hass, 'rfxtrx', {
'rfxtrx': { 'rfxtrx': {
@ -37,12 +36,10 @@ class TestRFXTRX(unittest.TestCase):
'automatic_add': True, 'automatic_add': True,
'devices': {}}})) 'devices': {}}}))
while len(rfxtrx.RFX_DEVICES) < 2:
time.sleep(0.1)
self.assertEqual(len(rfxtrx.RFXOBJECT.sensors()), 2) self.assertEqual(len(rfxtrx.RFXOBJECT.sensors()), 2)
def test_valid_config(self): @patch('RFXtrx.sleep')
def test_valid_config(self, mock_sleep):
"""Test configuration.""" """Test configuration."""
self.assertTrue(_setup_component(self.hass, 'rfxtrx', { self.assertTrue(_setup_component(self.hass, 'rfxtrx', {
'rfxtrx': { 'rfxtrx': {
@ -50,6 +47,8 @@ class TestRFXTRX(unittest.TestCase):
'-RFXCOM_RFXtrx433_A1Y0NJGR-if00-port0', '-RFXCOM_RFXtrx433_A1Y0NJGR-if00-port0',
'dummy': True}})) 'dummy': True}}))
self.hass.config.components.remove('rfxtrx')
self.assertTrue(_setup_component(self.hass, 'rfxtrx', { self.assertTrue(_setup_component(self.hass, 'rfxtrx', {
'rfxtrx': { 'rfxtrx': {
'device': '/dev/serial/by-id/usb' + 'device': '/dev/serial/by-id/usb' +
@ -69,9 +68,9 @@ class TestRFXTRX(unittest.TestCase):
'-RFXCOM_RFXtrx433_A1Y0NJGR-if00-port0', '-RFXCOM_RFXtrx433_A1Y0NJGR-if00-port0',
'invalid_key': True}})) 'invalid_key': True}}))
def test_fire_event(self): @patch('RFXtrx.sleep')
def test_fire_event(self, mock_sleep):
"""Test fire event.""" """Test fire event."""
self.assertTrue(_setup_component(self.hass, 'rfxtrx', { self.assertTrue(_setup_component(self.hass, 'rfxtrx', {
'rfxtrx': { 'rfxtrx': {
'device': '/dev/serial/by-id/usb' + 'device': '/dev/serial/by-id/usb' +
@ -110,12 +109,12 @@ class TestRFXTRX(unittest.TestCase):
self.assertEqual(event.values['Command'], "On") self.assertEqual(event.values['Command'], "On")
self.assertEqual('on', entity.state) self.assertEqual('on', entity.state)
self.assertEqual(self.hass.states.get('switch.test').state, 'on') self.assertEqual(self.hass.states.get('switch.test').state, 'on')
self.assertEqual(1, len(rfxtrx.RFX_DEVICES))
self.assertEqual(1, len(calls)) self.assertEqual(1, len(calls))
self.assertEqual(calls[0].data, self.assertEqual(calls[0].data,
{'entity_id': 'switch.test', 'state': 'on'}) {'entity_id': 'switch.test', 'state': 'on'})
def test_fire_event_sensor(self): @patch('RFXtrx.sleep')
def test_fire_event_sensor(self, mock_sleep):
"""Test fire event.""" """Test fire event."""
self.assertTrue(_setup_component(self.hass, 'rfxtrx', { self.assertTrue(_setup_component(self.hass, 'rfxtrx', {
'rfxtrx': { 'rfxtrx': {
@ -145,7 +144,6 @@ class TestRFXTRX(unittest.TestCase):
rfxtrx.RECEIVED_EVT_SUBSCRIBERS[0](event) rfxtrx.RECEIVED_EVT_SUBSCRIBERS[0](event)
self.hass.block_till_done() self.hass.block_till_done()
self.assertEqual(1, len(rfxtrx.RFX_DEVICES))
self.assertEqual(1, len(calls)) self.assertEqual(1, len(calls))
self.assertEqual(calls[0].data, self.assertEqual(calls[0].data,
{'entity_id': 'sensor.test'}) {'entity_id': 'sensor.test'})

View File

@ -11,7 +11,7 @@ setenv =
LANG=en_US.UTF-8 LANG=en_US.UTF-8
PYTHONPATH = {toxinidir}:{toxinidir}/homeassistant PYTHONPATH = {toxinidir}:{toxinidir}/homeassistant
commands = commands =
py.test -v --timeout=30 --cov --cov-report= {posargs} py.test -v --timeout=30 --duration=10 --cov --cov-report= {posargs}
deps = deps =
-r{toxinidir}/requirements_all.txt -r{toxinidir}/requirements_all.txt
-r{toxinidir}/requirements_test.txt -r{toxinidir}/requirements_test.txt