Add flake8-pytest-style (PT) for ruff (#1105)

This will catch common issues with pytest code.

* Use `match` when using `pytest.raises()` for base exception types like
`TypeError` or `ValueError`
* Use tuples for `parametrize()`
* Enforces `pytest.raises()` to contain simple statements, using `noqa`
to skip this on some cases for now.
* Fixes incorrect exception type (valueerror instead of typeerror) for
iotdimmer.
* Adds check valid types for `iotbulb.set_hsv` and `color` smart module.
* Consolidate exception messages for common interface modules.
This commit is contained in:
Teemu R.
2024-08-30 17:30:07 +02:00
committed by GitHub
parent 3e43781bb2
commit 6a86ffbbba
36 changed files with 248 additions and 150 deletions

View File

@@ -1,4 +1,5 @@
import logging
import re
import pytest
@@ -15,7 +16,7 @@ temperature = parametrize(
@thermostats_smart
@pytest.mark.parametrize(
"feature, type",
("feature", "type"),
[
("target_temperature", float),
("temperature_offset", int),
@@ -59,10 +60,14 @@ async def test_set_temperature_invalid_values(dev):
"""Test that out-of-bounds temperature values raise errors."""
temp_module: TemperatureControl = dev.modules["TemperatureControl"]
with pytest.raises(ValueError):
with pytest.raises(
ValueError, match="Invalid target temperature -1, must be in range"
):
await temp_module.set_target_temperature(-1)
with pytest.raises(ValueError):
with pytest.raises(
ValueError, match="Invalid target temperature 100, must be in range"
):
await temp_module.set_target_temperature(100)
@@ -70,10 +75,14 @@ async def test_set_temperature_invalid_values(dev):
async def test_temperature_offset(dev):
"""Test the temperature offset API."""
temp_module: TemperatureControl = dev.modules["TemperatureControl"]
with pytest.raises(ValueError):
with pytest.raises(
ValueError, match=re.escape("Temperature offset must be [-10, 10]")
):
await temp_module.set_temperature_offset(100)
with pytest.raises(ValueError):
with pytest.raises(
ValueError, match=re.escape("Temperature offset must be [-10, 10]")
):
await temp_module.set_temperature_offset(-100)
await temp_module.set_temperature_offset(5)
@@ -83,7 +92,7 @@ async def test_temperature_offset(dev):
@thermostats_smart
@pytest.mark.parametrize(
"mode, states, frost_protection",
("mode", "states", "frost_protection"),
[
pytest.param(ThermostatState.Idle, [], False, id="idle has empty"),
pytest.param(
@@ -114,7 +123,7 @@ async def test_thermostat_mode(dev, mode, states, frost_protection):
@thermostats_smart
@pytest.mark.parametrize(
"mode, states, msg",
("mode", "states", "msg"),
[
pytest.param(
ThermostatState.Heating,