|
1 | | -# tests/test_weather.py |
2 | | - |
3 | 1 | import sys |
| 2 | +from typing import Any |
4 | 3 |
|
5 | 4 | import pytest |
6 | | -from weather import WeatherService |
7 | 5 |
|
8 | | -# from hypothesis import given |
9 | | -# from hypothesis.strategies import floats |
| 6 | +from weather import WeatherService |
10 | 7 |
|
11 | 8 |
|
12 | | -# ✅ Parametrization |
13 | 9 | @pytest.mark.parametrize( |
14 | | - "city,expected_temp", [("London", 15), ("Berlin", 20), ("Paris", 17)] |
| 10 | + "city,expected_temp", |
| 11 | + [ |
| 12 | + ("London", 15), |
| 13 | + ("Berlin", 20), |
| 14 | + ("Rome", 18), |
| 15 | + ], |
15 | 16 | ) |
16 | | -def test_get_temperature_multiple_cities(monkeypatch, city, expected_temp): |
17 | | - def fake_get(url, params): |
| 17 | +def test_parametrized_temperatures( |
| 18 | + monkeypatch: pytest.MonkeyPatch, city: str, expected_temp: float |
| 19 | +) -> None: |
| 20 | + def fake_get(url: str, params: dict[str, Any]) -> Any: |
18 | 21 | class FakeResponse: |
19 | | - def raise_for_status(self): |
| 22 | + def raise_for_status(self) -> None: |
20 | 23 | pass |
21 | 24 |
|
22 | | - def json(self): |
| 25 | + def json(self) -> dict[str, Any]: |
23 | 26 | return {"current": {"temp_c": expected_temp}} |
24 | 27 |
|
25 | 28 | return FakeResponse() |
26 | 29 |
|
27 | | - monkeypatch.setattr("weather.httpx.get", fake_get) |
28 | | - |
| 30 | + monkeypatch.setattr("httpx.get", fake_get) |
29 | 31 | service = WeatherService(api_key="fake-key") |
30 | | - temp = service.get_temperature(city) |
| 32 | + assert service.get_temperature(city) == expected_temp |
31 | 33 |
|
32 | | - assert temp == expected_temp |
33 | 34 |
|
34 | | - |
35 | | -# ✅ pytest.raises |
36 | | -def test_get_temperature_raises_http_error(monkeypatch): |
37 | | - def fake_get(url, params): |
| 35 | +def test_temperature_raises_error(monkeypatch: pytest.MonkeyPatch) -> None: |
| 36 | + def fake_get(url: str, params: dict[str, Any]) -> Any: |
38 | 37 | class FakeResponse: |
39 | | - def raise_for_status(self): |
| 38 | + def raise_for_status(self) -> None: |
40 | 39 | raise Exception("API error") |
41 | 40 |
|
42 | 41 | return FakeResponse() |
43 | 42 |
|
44 | | - monkeypatch.setattr("weather.httpx.get", fake_get) |
45 | | - |
| 43 | + monkeypatch.setattr("httpx.get", fake_get) |
46 | 44 | service = WeatherService(api_key="fake-key") |
47 | 45 |
|
48 | 46 | with pytest.raises(Exception): |
49 | | - service.get_temperature("Tokyo") |
| 47 | + service.get_temperature("Oslo") |
50 | 48 |
|
51 | 49 |
|
52 | | -# ✅ pytest.mark.skip |
53 | | -@pytest.mark.skip(reason="Skipping this test for demonstration purposes.") |
54 | | -def test_skipped_example(): |
| 50 | +@pytest.mark.skip(reason="Temporarily skipping for demo purposes") |
| 51 | +def test_skipped() -> None: |
55 | 52 | assert False |
56 | 53 |
|
57 | 54 |
|
58 | | -# ✅ pytest.mark.skipif |
59 | | -@pytest.mark.skipif(sys.platform == "win32", reason="Does not run on Windows") |
60 | | -def test_only_runs_on_non_windows(): |
| 55 | +@pytest.mark.skipif(sys.platform == "win32", reason="Fails on Windows") |
| 56 | +def test_non_windows_behavior() -> None: |
61 | 57 | assert True |
62 | 58 |
|
63 | 59 |
|
64 | | -# ✅ pytest.mark.xfail |
65 | | -@pytest.mark.xfail(reason="Known bug: API sometimes returns wrong temperature") |
66 | | -def test_expected_failure_example(): |
67 | | - assert 2 + 2 == 5 |
68 | | - |
69 | | - |
70 | | -# ✅ Hypothesis property-based test |
71 | | -# @given(floats(min_value=-50, max_value=50)) |
72 | | -# def test_temperature_range_property(temp): |
73 | | -# assert -50 <= temp <= 50 |
| 60 | +@pytest.mark.xfail(reason="Intentional failure due to API bug") |
| 61 | +def test_expected_failure() -> None: |
| 62 | + assert 1 + 1 == 3 |
0 commit comments