Rewrite random unittest tests to pytest style test functions (#40920)

This commit is contained in:
Eliot Wong 2020-10-01 03:05:44 -04:00 committed by GitHub
parent c5041b41c8
commit ad8ab183cb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 48 additions and 60 deletions

View file

@ -1,45 +1,44 @@
"""The test for the Random binary sensor platform.""" """The test for the Random binary sensor platform."""
import unittest from homeassistant.setup import async_setup_component
from homeassistant.setup import setup_component
from tests.async_mock import patch from tests.async_mock import patch
from tests.common import get_test_home_assistant
class TestRandomSensor(unittest.TestCase): async def test_random_binary_sensor_on(hass):
"""Test the Random binary sensor.""" """Test the Random binary sensor."""
config = {"binary_sensor": {"platform": "random", "name": "test"}}
def setup_method(self, method): with patch(
"""Set up things to be run when tests are started.""" "homeassistant.components.random.binary_sensor.getrandbits",
self.hass = get_test_home_assistant() return_value=1,
):
assert await async_setup_component(
hass,
"binary_sensor",
config,
)
await hass.async_block_till_done()
def teardown_method(self, method): state = hass.states.get("binary_sensor.test")
"""Stop everything that was started."""
self.hass.stop()
@patch("homeassistant.components.random.binary_sensor.getrandbits", return_value=1) assert state.state == "on"
def test_random_binary_sensor_on(self, mocked):
"""Test the Random binary sensor."""
config = {"binary_sensor": {"platform": "random", "name": "test"}}
assert setup_component(self.hass, "binary_sensor", config)
self.hass.block_till_done()
state = self.hass.states.get("binary_sensor.test") async def test_random_binary_sensor_off(hass):
"""Test the Random binary sensor."""
config = {"binary_sensor": {"platform": "random", "name": "test"}}
assert state.state == "on" with patch(
"homeassistant.components.random.binary_sensor.getrandbits",
return_value=False,
):
assert await async_setup_component(
hass,
"binary_sensor",
config,
)
await hass.async_block_till_done()
@patch( state = hass.states.get("binary_sensor.test")
"homeassistant.components.random.binary_sensor.getrandbits", return_value=False
)
def test_random_binary_sensor_off(self, mocked):
"""Test the Random binary sensor."""
config = {"binary_sensor": {"platform": "random", "name": "test"}}
assert setup_component(self.hass, "binary_sensor", config) assert state.state == "off"
self.hass.block_till_done()
state = self.hass.states.get("binary_sensor.test")
assert state.state == "off"

View file

@ -1,37 +1,26 @@
"""The test for the random number sensor platform.""" """The test for the random number sensor platform."""
import unittest from homeassistant.setup import async_setup_component
from homeassistant.setup import setup_component
from tests.common import get_test_home_assistant
class TestRandomSensor(unittest.TestCase): async def test_random_sensor(hass):
"""Test the Random number sensor.""" """Test the Random number sensor."""
config = {
def setup_method(self, method): "sensor": {
"""Set up things to be run when tests are started.""" "platform": "random",
self.hass = get_test_home_assistant() "name": "test",
"minimum": 10,
def teardown_method(self, method): "maximum": 20,
"""Stop everything that was started."""
self.hass.stop()
def test_random_sensor(self):
"""Test the Random number sensor."""
config = {
"sensor": {
"platform": "random",
"name": "test",
"minimum": 10,
"maximum": 20,
}
} }
}
assert setup_component(self.hass, "sensor", config) assert await async_setup_component(
self.hass.block_till_done() hass,
"sensor",
config,
)
await hass.async_block_till_done()
state = self.hass.states.get("sensor.test") state = hass.states.get("sensor.test")
assert int(state.state) <= config["sensor"]["maximum"] assert int(state.state) <= config["sensor"]["maximum"]
assert int(state.state) >= config["sensor"]["minimum"] assert int(state.state) >= config["sensor"]["minimum"]