tests: clean up pytest configuration and setup#321
Conversation
There was a problem hiding this comment.
Pull request overview
This PR consolidates pytest configuration into pyproject.toml (prepping for a future uv follow-up), removes redundant pytest/sys.path bootstrapping from tests, and refactors one test module to use a shared factory-patching fixture.
Changes:
- Move pytest configuration from
pytest.iniintopyproject.toml([tool.pytest.ini_options]) and deletepytest.ini. - Remove manual
sys.pathmanipulation from tests now that pytest addssrcviapythonpath. - Refactor
tests/batcontrol/test_production_offset.pyto use apytest-mockfixture for repeated factory patch setup.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| tests/conftest.py | Removed now-unneeded global sys.path insertion (file deleted). |
| tests/batcontrol/test_production_offset.py | Introduced a fixture to patch core factories and reuse a Batcontrol instance in tests. |
| tests/batcontrol/inverter/test_mqtt_inverter.py | Removed per-file sys.path insertion. |
| tests/batcontrol/inverter/test_dummy.py | Removed per-file sys.path insertion. |
| pytest.ini | Removed redundant pytest configuration file. |
| pyproject.toml | Added pytest ini options under [tool.pytest.ini_options] (incl. pythonpath = ["src"]). |
| def test_production_offset_initialization_default( | ||
| self, mock_config, batcontrol_with_patched_factories | ||
| ): | ||
| """Test that production offset initializes with default value when not configured""" | ||
| # Remove production_offset_percent from config | ||
| del mock_config['battery_control_expert']['production_offset_percent'] | ||
|
|
||
| with patch('batcontrol.core.tariff_factory'), \ | ||
| patch('batcontrol.core.inverter_factory'), \ | ||
| patch('batcontrol.core.solar_factory'), \ | ||
| patch('batcontrol.core.consumption_factory'): | ||
| batcontrol = Batcontrol(mock_config) | ||
|
|
||
| batcontrol = Batcontrol(mock_config) | ||
| assert batcontrol.production_offset_percent == 1.0 | ||
| batcontrol.shutdown() |
There was a problem hiding this comment.
test_production_offset_initialization_default requests batcontrol_with_patched_factories but then instantiates a new Batcontrol(mock_config) without any factory patching. This bypasses the intended isolation (real provider factories + scheduler thread + refresh_data calls can run) and also spins up an unused extra Batcontrol instance via the fixture. Use the fixture instance for the assertion, or restructure so factory patching happens for the Batcontrol instance created after removing production_offset_percent (e.g., patch factories first, then construct Batcontrol once).
This cleans up the current pytest setup before a separate
uvfollow-up.It moves the active pytest configuration into
pyproject.toml, removes the now-redundantpytest.ini, and drops old manualsys.pathmangling from tests that no longer need it once pytest usespythonpath = src.While touching test setup, it also does a small cleanup in
tests/batcontrol/test_production_offset.pyby switching the repeated factory patch setup there to a fixture usingpytest-mock.