From bf5f7c53d8ac56e57e81e5cddf86640df0847c1d Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Mon, 6 Mar 2023 11:31:50 +0100 Subject: [PATCH] Move mock_setup_entry to conftest (#88484) --- .../templates/config_flow/tests/conftest.py | 14 ++++++++++++++ .../config_flow/tests/test_config_flow.py | 10 +--------- .../templates/config_flow_helper/tests/conftest.py | 14 ++++++++++++++ .../config_flow_helper/tests/test_config_flow.py | 12 ++---------- tests/components/nibe_heatpump/conftest.py | 11 ++++++++++- tests/components/nibe_heatpump/test_config_flow.py | 10 ++-------- tests/components/onewire/conftest.py | 12 +++++++++++- tests/components/onewire/test_config_flow.py | 10 +--------- tests/components/philips_js/conftest.py | 14 +++++++++++++- tests/components/philips_js/test_config_flow.py | 13 ++----------- tests/components/renault/conftest.py | 12 +++++++++++- tests/components/renault/test_config_flow.py | 9 +-------- tests/components/samsungtv/conftest.py | 11 ++++++++++- tests/components/samsungtv/test_config_flow.py | 10 +--------- tests/components/sfr_box/conftest.py | 11 ++++++++++- tests/components/sfr_box/test_config_flow.py | 10 +--------- tests/components/sleepiq/conftest.py | 11 ++++++++++- tests/components/sleepiq/test_config_flow.py | 10 +--------- 18 files changed, 115 insertions(+), 89 deletions(-) create mode 100644 script/scaffold/templates/config_flow/tests/conftest.py create mode 100644 script/scaffold/templates/config_flow_helper/tests/conftest.py diff --git a/script/scaffold/templates/config_flow/tests/conftest.py b/script/scaffold/templates/config_flow/tests/conftest.py new file mode 100644 index 00000000000..dab3d971a3b --- /dev/null +++ b/script/scaffold/templates/config_flow/tests/conftest.py @@ -0,0 +1,14 @@ +"""Test the NEW_NAME config flow.""" +from collections.abc import Generator +from unittest.mock import AsyncMock, patch + +import pytest + + +@pytest.fixture +def mock_setup_entry() -> Generator[AsyncMock, None, None]: + """Override async_setup_entry.""" + with patch( + "homeassistant.components.NEW_DOMAIN.async_setup_entry", return_value=True + ) as mock_setup_entry: + yield mock_setup_entry diff --git a/script/scaffold/templates/config_flow/tests/test_config_flow.py b/script/scaffold/templates/config_flow/tests/test_config_flow.py index 6af4bf1e667..cbc1449378c 100644 --- a/script/scaffold/templates/config_flow/tests/test_config_flow.py +++ b/script/scaffold/templates/config_flow/tests/test_config_flow.py @@ -1,5 +1,4 @@ """Test the NEW_NAME config flow.""" -from collections.abc import Generator from unittest.mock import AsyncMock, patch import pytest @@ -10,14 +9,7 @@ from homeassistant.components.NEW_DOMAIN.const import DOMAIN from homeassistant.core import HomeAssistant from homeassistant.data_entry_flow import FlowResultType - -@pytest.fixture(autouse=True, name="mock_setup_entry") -def override_async_setup_entry() -> Generator[AsyncMock, None, None]: - """Override async_setup_entry.""" - with patch( - "homeassistant.components.NEW_DOMAIN.async_setup_entry", return_value=True - ) as mock_setup_entry: - yield mock_setup_entry +pytestmark = pytest.mark.usefixtures("mock_setup_entry") async def test_form(hass: HomeAssistant, mock_setup_entry: AsyncMock) -> None: diff --git a/script/scaffold/templates/config_flow_helper/tests/conftest.py b/script/scaffold/templates/config_flow_helper/tests/conftest.py new file mode 100644 index 00000000000..dab3d971a3b --- /dev/null +++ b/script/scaffold/templates/config_flow_helper/tests/conftest.py @@ -0,0 +1,14 @@ +"""Test the NEW_NAME config flow.""" +from collections.abc import Generator +from unittest.mock import AsyncMock, patch + +import pytest + + +@pytest.fixture +def mock_setup_entry() -> Generator[AsyncMock, None, None]: + """Override async_setup_entry.""" + with patch( + "homeassistant.components.NEW_DOMAIN.async_setup_entry", return_value=True + ) as mock_setup_entry: + yield mock_setup_entry diff --git a/script/scaffold/templates/config_flow_helper/tests/test_config_flow.py b/script/scaffold/templates/config_flow_helper/tests/test_config_flow.py index ba7efff5b6c..d21c66797d8 100644 --- a/script/scaffold/templates/config_flow_helper/tests/test_config_flow.py +++ b/script/scaffold/templates/config_flow_helper/tests/test_config_flow.py @@ -1,6 +1,5 @@ """Test the NEW_NAME config flow.""" -from collections.abc import Generator -from unittest.mock import AsyncMock, patch +from unittest.mock import AsyncMock import pytest @@ -11,14 +10,7 @@ from homeassistant.data_entry_flow import FlowResultType from tests.common import MockConfigEntry - -@pytest.fixture(autouse=True, name="mock_setup_entry") -def override_async_setup_entry() -> Generator[AsyncMock, None, None]: - """Override async_setup_entry.""" - with patch( - "homeassistant.components.NEW_DOMAIN.async_setup_entry", return_value=True - ) as mock_setup_entry: - yield mock_setup_entry +pytestmark = pytest.mark.usefixtures("mock_setup_entry") @pytest.mark.parametrize("platform", ("sensor",)) diff --git a/tests/components/nibe_heatpump/conftest.py b/tests/components/nibe_heatpump/conftest.py index b75c49b2b79..2a4e2f80ff5 100644 --- a/tests/components/nibe_heatpump/conftest.py +++ b/tests/components/nibe_heatpump/conftest.py @@ -1,5 +1,5 @@ """Test configuration for Nibe Heat Pump.""" -from collections.abc import AsyncIterator, Iterable +from collections.abc import AsyncIterator, Generator, Iterable from contextlib import ExitStack from typing import Any from unittest.mock import AsyncMock, Mock, patch @@ -10,6 +10,15 @@ from nibe.exceptions import ReadException import pytest +@pytest.fixture +def mock_setup_entry() -> Generator[AsyncMock, None, None]: + """Make sure we never actually run setup.""" + with patch( + "homeassistant.components.nibe_heatpump.async_setup_entry", return_value=True + ) as mock_setup_entry: + yield mock_setup_entry + + @pytest.fixture(autouse=True, name="mock_connection_constructor") async def fixture_mock_connection_constructor(): """Make sure we have a dummy connection.""" diff --git a/tests/components/nibe_heatpump/test_config_flow.py b/tests/components/nibe_heatpump/test_config_flow.py index 3360c82577f..22dca1fa2f3 100644 --- a/tests/components/nibe_heatpump/test_config_flow.py +++ b/tests/components/nibe_heatpump/test_config_flow.py @@ -1,5 +1,5 @@ """Test the Nibe Heat Pump config flow.""" -from unittest.mock import Mock, patch +from unittest.mock import Mock from nibe.coil import Coil from nibe.exceptions import ( @@ -32,13 +32,7 @@ MOCK_FLOW_MODBUS_USERDATA = { } -@pytest.fixture(autouse=True, name="mock_setup_entry") -async def fixture_mock_setup(): - """Make sure we never actually run setup.""" - with patch( - "homeassistant.components.nibe_heatpump.async_setup_entry", return_value=True - ) as mock_setup_entry: - yield mock_setup_entry +pytestmark = pytest.mark.usefixtures("mock_setup_entry") async def _get_connection_form( diff --git a/tests/components/onewire/conftest.py b/tests/components/onewire/conftest.py index 9b8b53859ea..031b29d47a7 100644 --- a/tests/components/onewire/conftest.py +++ b/tests/components/onewire/conftest.py @@ -1,5 +1,6 @@ """Provide common 1-Wire fixtures.""" -from unittest.mock import MagicMock, patch +from collections.abc import Generator +from unittest.mock import AsyncMock, MagicMock, patch from pyownet.protocol import ConnError import pytest @@ -14,6 +15,15 @@ from .const import MOCK_OWPROXY_DEVICES from tests.common import MockConfigEntry +@pytest.fixture +def mock_setup_entry() -> Generator[AsyncMock, None, None]: + """Override async_setup_entry.""" + with patch( + "homeassistant.components.onewire.async_setup_entry", return_value=True + ) as mock_setup_entry: + yield mock_setup_entry + + @pytest.fixture(name="device_id", params=MOCK_OWPROXY_DEVICES.keys()) def get_device_id(request: pytest.FixtureRequest) -> str: """Parametrize device id.""" diff --git a/tests/components/onewire/test_config_flow.py b/tests/components/onewire/test_config_flow.py index eab44bc5d48..63e53627e0e 100644 --- a/tests/components/onewire/test_config_flow.py +++ b/tests/components/onewire/test_config_flow.py @@ -1,5 +1,4 @@ """Tests for 1-Wire config flow.""" -from collections.abc import Generator from unittest.mock import AsyncMock, patch from pyownet import protocol @@ -19,14 +18,7 @@ from homeassistant.helpers.config_validation import ensure_list from .const import MOCK_OWPROXY_DEVICES - -@pytest.fixture(autouse=True, name="mock_setup_entry") -def override_async_setup_entry() -> Generator[AsyncMock, None, None]: - """Override async_setup_entry.""" - with patch( - "homeassistant.components.onewire.async_setup_entry", return_value=True - ) as mock_setup_entry: - yield mock_setup_entry +pytestmark = pytest.mark.usefixtures("mock_setup_entry") @pytest.fixture diff --git a/tests/components/philips_js/conftest.py b/tests/components/philips_js/conftest.py index 0aaa76001c1..bc94d721cc9 100644 --- a/tests/components/philips_js/conftest.py +++ b/tests/components/philips_js/conftest.py @@ -1,5 +1,6 @@ """Standard setup for tests.""" -from unittest.mock import create_autospec, patch +from collections.abc import Generator +from unittest.mock import AsyncMock, create_autospec, patch from haphilipsjs import PhilipsTV import pytest @@ -11,6 +12,17 @@ from . import MOCK_CONFIG, MOCK_ENTITY_ID, MOCK_NAME, MOCK_SERIAL_NO, MOCK_SYSTE from tests.common import MockConfigEntry, mock_device_registry +@pytest.fixture +def mock_setup_entry() -> Generator[AsyncMock, None, None]: + """Disable component setup.""" + with patch( + "homeassistant.components.philips_js.async_setup_entry", return_value=True + ) as mock_setup_entry, patch( + "homeassistant.components.philips_js.async_unload_entry", return_value=True + ): + yield mock_setup_entry + + @pytest.fixture(autouse=True) async def setup_notification(hass): """Configure notification system.""" diff --git a/tests/components/philips_js/test_config_flow.py b/tests/components/philips_js/test_config_flow.py index d4a08aa6886..1662a2a3fc2 100644 --- a/tests/components/philips_js/test_config_flow.py +++ b/tests/components/philips_js/test_config_flow.py @@ -1,5 +1,5 @@ """Test the Philips TV config flow.""" -from unittest.mock import ANY, patch +from unittest.mock import ANY from haphilipsjs import PairingFailure import pytest @@ -19,16 +19,7 @@ from . import ( from tests.common import MockConfigEntry - -@pytest.fixture(autouse=True, name="mock_setup_entry") -def mock_setup_entry_fixture(): - """Disable component setup.""" - with patch( - "homeassistant.components.philips_js.async_setup_entry", return_value=True - ) as mock_setup_entry, patch( - "homeassistant.components.philips_js.async_unload_entry", return_value=True - ): - yield mock_setup_entry +pytestmark = pytest.mark.usefixtures("mock_setup_entry") @pytest.fixture diff --git a/tests/components/renault/conftest.py b/tests/components/renault/conftest.py index 6c62e5d22e2..312ddbf6092 100644 --- a/tests/components/renault/conftest.py +++ b/tests/components/renault/conftest.py @@ -1,8 +1,9 @@ """Provide common Renault fixtures.""" +from collections.abc import Generator import contextlib from types import MappingProxyType from typing import Any -from unittest.mock import patch +from unittest.mock import AsyncMock, patch import pytest from renault_api.kamereon import exceptions, schemas @@ -18,6 +19,15 @@ from .const import MOCK_ACCOUNT_ID, MOCK_CONFIG, MOCK_VEHICLES from tests.common import MockConfigEntry, load_fixture +@pytest.fixture +def mock_setup_entry() -> Generator[AsyncMock, None, None]: + """Override async_setup_entry.""" + with patch( + "homeassistant.components.renault.async_setup_entry", return_value=True + ) as mock_setup_entry: + yield mock_setup_entry + + @pytest.fixture(name="vehicle_type", params=MOCK_VEHICLES.keys()) def get_vehicle_type(request: pytest.FixtureRequest) -> str: """Parametrize vehicle type.""" diff --git a/tests/components/renault/test_config_flow.py b/tests/components/renault/test_config_flow.py index 39d4add062f..5d933c03c65 100644 --- a/tests/components/renault/test_config_flow.py +++ b/tests/components/renault/test_config_flow.py @@ -21,14 +21,7 @@ from .const import MOCK_CONFIG from tests.common import load_fixture - -@pytest.fixture(autouse=True, name="mock_setup_entry") -def override_async_setup_entry() -> AsyncMock: - """Override async_setup_entry.""" - with patch( - "homeassistant.components.renault.async_setup_entry", return_value=True - ) as mock_setup_entry: - yield mock_setup_entry +pytestmark = pytest.mark.usefixtures("mock_setup_entry") async def test_config_flow_single_account( diff --git a/tests/components/samsungtv/conftest.py b/tests/components/samsungtv/conftest.py index 73ad642f7e7..0e95dfa28a9 100644 --- a/tests/components/samsungtv/conftest.py +++ b/tests/components/samsungtv/conftest.py @@ -1,7 +1,7 @@ """Fixtures for Samsung TV.""" from __future__ import annotations -from collections.abc import Awaitable, Callable +from collections.abc import Awaitable, Callable, Generator from datetime import datetime from socket import AddressFamily from typing import Any @@ -25,6 +25,15 @@ import homeassistant.util.dt as dt_util from .const import SAMPLE_DEVICE_INFO_UE48JU6400, SAMPLE_DEVICE_INFO_WIFI +@pytest.fixture +def mock_setup_entry() -> Generator[AsyncMock, None, None]: + """Override async_setup_entry.""" + with patch( + "homeassistant.components.samsungtv.async_setup_entry", return_value=True + ) as mock_setup_entry: + yield mock_setup_entry + + @pytest.fixture(autouse=True) async def silent_ssdp_scanner(hass): """Start SSDP component and get Scanner, prevent actual SSDP traffic.""" diff --git a/tests/components/samsungtv/test_config_flow.py b/tests/components/samsungtv/test_config_flow.py index e84b3d30cb1..ac0072c88ce 100644 --- a/tests/components/samsungtv/test_config_flow.py +++ b/tests/components/samsungtv/test_config_flow.py @@ -1,5 +1,4 @@ """Tests for Samsung TV config flow.""" -from collections.abc import Generator import socket from unittest.mock import ANY, AsyncMock, Mock, call, patch @@ -216,14 +215,7 @@ DEVICEINFO_WEBSOCKET_NO_SSL = { "timeout": TIMEOUT_WEBSOCKET, } - -@pytest.fixture(autouse=True, name="mock_setup_entry") -def override_async_setup_entry() -> Generator[AsyncMock, None, None]: - """Override async_setup_entry.""" - with patch( - "homeassistant.components.samsungtv.async_setup_entry", return_value=True - ) as mock_setup_entry: - yield mock_setup_entry +pytestmark = pytest.mark.usefixtures("mock_setup_entry") @pytest.mark.usefixtures("remote", "rest_api_failing") diff --git a/tests/components/sfr_box/conftest.py b/tests/components/sfr_box/conftest.py index 207c2939f1d..1857ffeec30 100644 --- a/tests/components/sfr_box/conftest.py +++ b/tests/components/sfr_box/conftest.py @@ -1,7 +1,7 @@ """Provide common SFR Box fixtures.""" from collections.abc import Generator import json -from unittest.mock import patch +from unittest.mock import AsyncMock, patch import pytest from sfrbox_api.models import DslInfo, SystemInfo @@ -14,6 +14,15 @@ from homeassistant.core import HomeAssistant from tests.common import MockConfigEntry, load_fixture +@pytest.fixture +def mock_setup_entry() -> Generator[AsyncMock, None, None]: + """Override async_setup_entry.""" + with patch( + "homeassistant.components.sfr_box.async_setup_entry", return_value=True + ) as mock_setup_entry: + yield mock_setup_entry + + @pytest.fixture(name="config_entry") def get_config_entry(hass: HomeAssistant) -> ConfigEntry: """Create and register mock config entry.""" diff --git a/tests/components/sfr_box/test_config_flow.py b/tests/components/sfr_box/test_config_flow.py index e75e5a42c7d..c8130d5d617 100644 --- a/tests/components/sfr_box/test_config_flow.py +++ b/tests/components/sfr_box/test_config_flow.py @@ -1,5 +1,4 @@ """Test the SFR Box config flow.""" -from collections.abc import Generator import json from unittest.mock import AsyncMock, patch @@ -15,14 +14,7 @@ from homeassistant.core import HomeAssistant from tests.common import load_fixture - -@pytest.fixture(autouse=True, name="mock_setup_entry") -def override_async_setup_entry() -> Generator[AsyncMock, None, None]: - """Override async_setup_entry.""" - with patch( - "homeassistant.components.sfr_box.async_setup_entry", return_value=True - ) as mock_setup_entry: - yield mock_setup_entry +pytestmark = pytest.mark.usefixtures("mock_setup_entry") async def test_config_flow_skip_auth( diff --git a/tests/components/sleepiq/conftest.py b/tests/components/sleepiq/conftest.py index 23c42ee7f66..9932b75ebdb 100644 --- a/tests/components/sleepiq/conftest.py +++ b/tests/components/sleepiq/conftest.py @@ -2,7 +2,7 @@ from __future__ import annotations from collections.abc import Generator -from unittest.mock import MagicMock, create_autospec, patch +from unittest.mock import AsyncMock, MagicMock, create_autospec, patch from asyncsleepiq import ( Side, @@ -40,6 +40,15 @@ SLEEPIQ_CONFIG = { } +@pytest.fixture +def mock_setup_entry() -> Generator[AsyncMock, None, None]: + """Override async_setup_entry.""" + with patch( + "homeassistant.components.sleepiq.async_setup_entry", return_value=True + ) as mock_setup_entry: + yield mock_setup_entry + + @pytest.fixture def mock_bed() -> MagicMock: """Mock a SleepIQBed object with sleepers and lights.""" diff --git a/tests/components/sleepiq/test_config_flow.py b/tests/components/sleepiq/test_config_flow.py index a2191b56583..0f251675892 100644 --- a/tests/components/sleepiq/test_config_flow.py +++ b/tests/components/sleepiq/test_config_flow.py @@ -1,5 +1,4 @@ """Tests for the SleepIQ config flow.""" -from collections.abc import Generator from unittest.mock import AsyncMock, patch from asyncsleepiq import SleepIQLoginException, SleepIQTimeoutException @@ -12,14 +11,7 @@ from homeassistant.core import HomeAssistant from .conftest import SLEEPIQ_CONFIG, setup_platform - -@pytest.fixture(autouse=True, name="mock_setup_entry") -def override_async_setup_entry() -> Generator[AsyncMock, None, None]: - """Override async_setup_entry.""" - with patch( - "homeassistant.components.sleepiq.async_setup_entry", return_value=True - ) as mock_setup_entry: - yield mock_setup_entry +pytestmark = pytest.mark.usefixtures("mock_setup_entry") async def test_import(hass: HomeAssistant) -> None: