From fe7c8dd80a349437e5d68ccf3a85b175404ad80d Mon Sep 17 00:00:00 2001 From: Adam Cooper <13488006+GenericStudent@users.noreply.github.com> Date: Mon, 19 Oct 2020 11:08:55 +0100 Subject: [PATCH] Migrated coinmarketcap tests to utilise pytests instead of unittests (#42013) --- tests/components/coinmarketcap/test_sensor.py | 59 +++++++++---------- 1 file changed, 29 insertions(+), 30 deletions(-) diff --git a/tests/components/coinmarketcap/test_sensor.py b/tests/components/coinmarketcap/test_sensor.py index 4d1fb31da95..30fc0f0e1f7 100644 --- a/tests/components/coinmarketcap/test_sensor.py +++ b/tests/components/coinmarketcap/test_sensor.py @@ -1,43 +1,42 @@ """Tests for the CoinMarketCap sensor platform.""" import json -import unittest -import homeassistant.components.sensor as sensor -from homeassistant.setup import setup_component +import pytest + +from homeassistant.components.sensor import DOMAIN +from homeassistant.setup import async_setup_component from tests.async_mock import patch -from tests.common import assert_setup_component, get_test_home_assistant, load_fixture +from tests.common import assert_setup_component, load_fixture VALID_CONFIG = { - "platform": "coinmarketcap", - "currency_id": 1027, - "display_currency": "EUR", - "display_currency_decimals": 3, + DOMAIN: { + "platform": "coinmarketcap", + "currency_id": 1027, + "display_currency": "EUR", + "display_currency_decimals": 3, + } } -class TestCoinMarketCapSensor(unittest.TestCase): - """Test the CoinMarketCap sensor.""" +@pytest.fixture +async def setup_sensor(hass): + """Set up demo sensor component.""" + with assert_setup_component(1, DOMAIN): + with patch( + "coinmarketcap.Market.ticker", + return_value=json.loads(load_fixture("coinmarketcap.json")), + ): + await async_setup_component(hass, DOMAIN, VALID_CONFIG) + await hass.async_block_till_done() - def setUp(self): - """Set up things to be run when tests are started.""" - self.hass = get_test_home_assistant() - self.config = VALID_CONFIG - self.addCleanup(self.hass.stop) - @patch( - "coinmarketcap.Market.ticker", - return_value=json.loads(load_fixture("coinmarketcap.json")), - ) - def test_setup(self, mock_request): - """Test the setup with custom settings.""" - with assert_setup_component(1, sensor.DOMAIN): - assert setup_component(self.hass, sensor.DOMAIN, {"sensor": VALID_CONFIG}) - self.hass.block_till_done() +async def test_setup(hass, setup_sensor): + """Test the setup with custom settings.""" + state = hass.states.get("sensor.ethereum") + assert state is not None - state = self.hass.states.get("sensor.ethereum") - assert state is not None - - assert state.state == "493.455" - assert state.attributes.get("symbol") == "ETH" - assert state.attributes.get("unit_of_measurement") == "EUR" + assert state.name == "Ethereum" + assert state.state == "493.455" + assert state.attributes.get("symbol") == "ETH" + assert state.attributes.get("unit_of_measurement") == "EUR"