Skip to content

Commit 9e72978

Browse files
author
benoit-cty
committed
Better test
1 parent 31c3f4c commit 9e72978

1 file changed

Lines changed: 20 additions & 11 deletions

File tree

tests/test_cpu.py

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,26 @@
2020

2121

2222
class TestCPU(unittest.TestCase):
23-
def test_is_psutil_available(self):
24-
@mock.patch("psutil.cpu_times")
25-
def test_is_psutil_available(self, mock_cpu_times):
26-
# Test when psutil is available
27-
mock_cpu_times.return_value = mock.Mock()
28-
self.assertTrue(is_psutil_available()) # noqa: E712
29-
30-
@mock.patch("psutil.cpu_times", side_effect=AttributeError)
31-
def test_is_psutil_not_available(self, mock_cpu_times):
32-
# Test when psutil is not available
33-
self.assertFalse(is_psutil_available()) # noqa: E712
23+
@mock.patch("psutil.cpu_times")
24+
def test_is_psutil_available_with_nice(self, mock_cpu_times):
25+
# Create a mock with 'nice' attribute
26+
mock_times = mock.Mock()
27+
mock_times.nice = 0.1
28+
mock_cpu_times.return_value = mock_times
29+
self.assertTrue(is_psutil_available())
30+
31+
@mock.patch("psutil.cpu_times")
32+
def test_is_psutil_available_without_nice(self, mock_cpu_times):
33+
# Create a mock without 'nice' attribute (like Windows)
34+
mock_times = mock.Mock(spec=[]) # Empty spec = no attributes
35+
mock_cpu_times.return_value = mock_times
36+
with mock.patch("psutil.cpu_percent") as mock_cpu_percent:
37+
self.assertTrue(is_psutil_available())
38+
mock_cpu_percent.assert_called_once_with(interval=0.0, percpu=False)
39+
40+
@mock.patch("psutil.cpu_times", side_effect=Exception("Test error"))
41+
def test_is_psutil_not_available_on_exception(self, mock_cpu_times):
42+
self.assertFalse(is_psutil_available())
3443

3544

3645
class TestIntelPowerGadget(unittest.TestCase):

0 commit comments

Comments
 (0)