From 3c29ccdb6f7aba9f76826990f5fb14a832f70c71 Mon Sep 17 00:00:00 2001 From: Guido Schmitz Date: Sat, 28 Nov 2020 12:17:14 +0100 Subject: [PATCH 1/2] Optimize tests --- tests/fixtures/__init__.py | 0 tests/fixtures/device.py | 9 ++++++--- tests/mocks/__init__.py | 0 tests/mocks/mock_zeroconf.py | 2 +- tests/stubs/__init__.py | 0 tests/test_device.py | 16 ++++++---------- 6 files changed, 13 insertions(+), 14 deletions(-) create mode 100644 tests/fixtures/__init__.py create mode 100644 tests/mocks/__init__.py create mode 100644 tests/stubs/__init__.py diff --git a/tests/fixtures/__init__.py b/tests/fixtures/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/fixtures/device.py b/tests/fixtures/device.py index 9e0c4dd..83a1d75 100644 --- a/tests/fixtures/device.py +++ b/tests/fixtures/device.py @@ -1,9 +1,11 @@ from copy import deepcopy import pytest +from zeroconf import Zeroconf from devolo_plc_api.device import Device -from ..mocks.mock_zeroconf import Zeroconf + +from ..mocks.mock_zeroconf import MockZeroconf @pytest.fixture() @@ -12,7 +14,7 @@ def mock_device(request): device._info = deepcopy(request.cls.device_info) device._session = None device._zeroconf = None - return device + yield device @pytest.fixture() @@ -23,4 +25,5 @@ def mock_service_browser(mocker): @pytest.fixture() def mock_zeroconf(mocker): - mocker.patch("zeroconf.Zeroconf.get_service_info", Zeroconf.get_service_info) + mocker.patch("zeroconf.Zeroconf.get_service_info", MockZeroconf.get_service_info) + yield Zeroconf() diff --git a/tests/mocks/__init__.py b/tests/mocks/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/mocks/mock_zeroconf.py b/tests/mocks/mock_zeroconf.py index a1621ce..34857eb 100644 --- a/tests/mocks/mock_zeroconf.py +++ b/tests/mocks/mock_zeroconf.py @@ -9,7 +9,7 @@ test_data = json.load(fh) -class Zeroconf: +class MockZeroconf: def get_service_info(self, service_type, name): service_info = ServiceInfo(service_type, name) diff --git a/tests/stubs/__init__.py b/tests/stubs/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_device.py b/tests/test_device.py index 0782851..1ea5b0d 100644 --- a/tests/test_device.py +++ b/tests/test_device.py @@ -3,7 +3,7 @@ from unittest.mock import patch import pytest -import zeroconf +from zeroconf import ServiceBrowser, ServiceStateChange try: from unittest.mock import AsyncMock @@ -59,7 +59,7 @@ async def test__get_plcnet_info_timeout(self, mock_device): @pytest.mark.asyncio @pytest.mark.usefixtures("mock_service_browser") async def test__get_zeroconf_info(self, mocker, mock_device): - spy_cancel = mocker.spy(zeroconf.ServiceBrowser, "cancel") + spy_cancel = mocker.spy(ServiceBrowser, "cancel") spy_sleep = mocker.spy(asyncio, "sleep") await mock_device._get_zeroconf_info("_dvl-plcnetapi._tcp.local.") assert spy_cancel.call_count == 1 @@ -85,17 +85,13 @@ async def test__setup_device_not_found(self, mock_device): await mock_device._setup_device() @pytest.mark.asyncio - @pytest.mark.usefixtures("mock_zeroconf") - def test__state_change_added(self, mock_device): - zc = zeroconf.Zeroconf() + def test__state_change_added(self, mock_device, mock_zeroconf): service_type = "_dvl-plcnetapi._tcp.local." - mock_device._state_change(zc, service_type, service_type, zeroconf.ServiceStateChange.Added) + mock_device._state_change(mock_zeroconf, service_type, service_type, ServiceStateChange.Added) assert mock_device._info[service_type]['new'] == "value" @pytest.mark.asyncio - @pytest.mark.usefixtures("mock_zeroconf") - def test__state_change_removed(self, mock_device): - zc = zeroconf.Zeroconf() + def test__state_change_removed(self, mock_device, mock_zeroconf): service_type = "_dvl-plcnetapi._tcp.local." - mock_device._state_change(zc, service_type, service_type, zeroconf.ServiceStateChange.Removed) + mock_device._state_change(mock_zeroconf, service_type, service_type, ServiceStateChange.Removed) assert "new" not in mock_device._info[service_type] From b80bfd6dd843ef3f6ddaa27d52e5b47342bca64e Mon Sep 17 00:00:00 2001 From: Guido Schmitz Date: Sat, 28 Nov 2020 14:49:47 +0100 Subject: [PATCH 2/2] Unify usage of return and yield --- tests/fixtures/device.py | 4 ++-- tests/fixtures/protobuf.py | 3 +-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/tests/fixtures/device.py b/tests/fixtures/device.py index 83a1d75..c7f9ab8 100644 --- a/tests/fixtures/device.py +++ b/tests/fixtures/device.py @@ -14,7 +14,7 @@ def mock_device(request): device._info = deepcopy(request.cls.device_info) device._session = None device._zeroconf = None - yield device + return device @pytest.fixture() @@ -26,4 +26,4 @@ def mock_service_browser(mocker): @pytest.fixture() def mock_zeroconf(mocker): mocker.patch("zeroconf.Zeroconf.get_service_info", MockZeroconf.get_service_info) - yield Zeroconf() + return Zeroconf() diff --git a/tests/fixtures/protobuf.py b/tests/fixtures/protobuf.py index 7da8c6a..3b19622 100644 --- a/tests/fixtures/protobuf.py +++ b/tests/fixtures/protobuf.py @@ -10,8 +10,7 @@ @pytest.fixture() def mock_protobuf(): - protobuf = StubProtobuf() - yield protobuf + return StubProtobuf() @pytest.fixture()