Rewrite mochad unittest to pytest style (#42164)

This commit is contained in:
Tim Werner authored and GitHub committed 2020-10-22 14:58:54 +02:00
1 parent 9a54c31c34
commit fcc895f294
2 files changed
+70 -163

No files matched your search

+29 -49
View File
@@ -1,14 +1,11 @@
"""The tests for the mochad switch platform."""
import unittest
import pytest
from homeassistant.components import switch
from homeassistant.components.mochad import switch as mochad
from homeassistant.setup import setup_component
from homeassistant.setup import async_setup_component
import tests.async_mock as mock
from tests.common import get_test_home_assistant
@pytest.fixture(autouse=True)
@@ -20,55 +17,38 @@ def pymochad_mock():
yield
class TestMochadSwitchSetup(unittest.TestCase):
"""Test the mochad switch."""
PLATFORM = mochad
COMPONENT = switch
THING = "switch"
def setUp(self):
"""Set up things to be run when tests are started."""
self.hass = get_test_home_assistant()
self.addCleanup(self.hass.stop)
@mock.patch("homeassistant.components.mochad.switch.MochadSwitch")
def test_setup_adds_proper_devices(self, mock_switch):
"""Test if setup adds devices."""
good_config = {
"mochad": {},
"switch": {
"platform": "mochad",
"devices": [{"name": "Switch1", "address": "a1"}],
},
}
assert setup_component(self.hass, switch.DOMAIN, good_config)
@pytest.fixture
def switch_mock(hass):
"""Mock switch."""
controller_mock = mock.MagicMock()
dev_dict = {"address": "a1", "name": "fake_switch"}
return mochad.MochadSwitch(hass, controller_mock, dev_dict)
class TestMochadSwitch(unittest.TestCase):
"""Test for mochad switch platform."""
async def test_setup_adds_proper_devices(hass):
"""Test if setup adds devices."""
good_config = {
"mochad": {},
"switch": {
"platform": "mochad",
"devices": [{"name": "Switch1", "address": "a1"}],
},
}
assert await async_setup_component(hass, switch.DOMAIN, good_config)
def setUp(self):
"""Set up things to be run when tests are started."""
self.hass = get_test_home_assistant()
controller_mock = mock.MagicMock()
dev_dict = {"address": "a1", "name": "fake_switch"}
self.switch = mochad.MochadSwitch(self.hass, controller_mock, dev_dict)
def teardown_method(self, method):
"""Stop everything that was started."""
self.hass.stop()
async def test_name(switch_mock):
"""Test the name."""
assert "fake_switch" == switch_mock.name
def test_name(self):
"""Test the name."""
assert "fake_switch" == self.switch.name
def test_turn_on(self):
"""Test turn_on."""
self.switch.turn_on()
self.switch.switch.send_cmd.assert_called_once_with("on")
async def test_turn_on(switch_mock):
"""Test turn_on."""
switch_mock.turn_on()
switch_mock.switch.send_cmd.assert_called_once_with("on")
def test_turn_off(self):
"""Test turn_off."""
self.switch.turn_off()
self.switch.switch.send_cmd.assert_called_once_with("off")
async def test_turn_off(switch_mock):
"""Test turn_off."""
switch_mock.turn_off()
switch_mock.switch.send_cmd.assert_called_once_with("off")