diff --git a/tests/batcontrol/inverter/test_inverter_factory.py b/tests/batcontrol/inverter/test_inverter_factory.py index fdaf809f..9e9cded7 100644 --- a/tests/batcontrol/inverter/test_inverter_factory.py +++ b/tests/batcontrol/inverter/test_inverter_factory.py @@ -4,6 +4,16 @@ from batcontrol.inverter.mqtt_inverter import MqttInverter +@pytest.fixture(autouse=True) +def reset_inverter_counter(): + original_value = Inverter.num_inverters + Inverter.num_inverters = 0 + + yield + + Inverter.num_inverters = original_value + + def test_factory_creates_mqtt_inverter(): """Factory should create an MQTT inverter for type mqtt.""" config = { @@ -40,7 +50,7 @@ def test_factory_rejects_unknown_type(): "max_grid_charge_rate": 5000, } - with pytest.raises(RuntimeError, match="Unkown inverter type"): + with pytest.raises(RuntimeError, match="inverter type"): Inverter.create_inverter(config) diff --git a/tests/batcontrol/test_production_offset.py b/tests/batcontrol/test_production_offset.py index a5d42a33..30eb1e03 100644 --- a/tests/batcontrol/test_production_offset.py +++ b/tests/batcontrol/test_production_offset.py @@ -67,7 +67,7 @@ def mock_config(self): } @pytest.fixture - def batcontrol_with_patched_factories(self, mock_config, mocker): + def make_batcontrol(self, mocker): core_module = 'batcontrol.core' mocker.patch(f'{core_module}.tariff_factory') @@ -75,22 +75,29 @@ def batcontrol_with_patched_factories(self, mock_config, mocker): mocker.patch(f'{core_module}.solar_factory') mocker.patch(f'{core_module}.consumption_factory') - bc = Batcontrol(mock_config) + created_instances = [] - yield bc + def _make(config): + bc = Batcontrol(config) + created_instances.append(bc) + return bc - bc.shutdown() + yield _make - def test_production_offset_initialization_default( - self, mock_config, batcontrol_with_patched_factories - ): + for bc in created_instances: + bc.shutdown() + + @pytest.fixture + def batcontrol_with_patched_factories(self, mock_config, make_batcontrol): + yield make_batcontrol(mock_config) + + def test_production_offset_initialization_default(self, mock_config, make_batcontrol): """Test that production offset initializes with default value when not configured""" del mock_config['battery_control_expert']['production_offset_percent'] - batcontrol = Batcontrol(mock_config) + batcontrol = make_batcontrol(mock_config) assert batcontrol.production_offset_percent == 1.0 - batcontrol.shutdown() def test_production_offset_initialization_from_config( self, batcontrol_with_patched_factories