| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
import os |
| 6 |
import sys |
| 7 |
import pytest |
| 8 |
from unittest.mock import patch, MagicMock |
| 9 |
|
| 10 |
|
| 11 |
from scalene.time_info import get_times |
| 12 |
|
| 13 |
|
| 14 |
class MockResourceUsage: |
| 15 |
def __init__(self, stime, utime): |
| 16 |
self.ru_stime = stime |
| 17 |
self.ru_utime = utime |
| 18 |
|
| 19 |
|
| 20 |
@pytest.fixture |
| 21 |
def mock_resource_module(): |
| 22 |
with patch( |
| 23 |
"resource.getrusage", return_value=MockResourceUsage(1.23, 4.56) |
| 24 |
) as mock_resource: |
| 25 |
yield mock_resource |
| 26 |
|
| 27 |
|
| 28 |
def test_get_times_linux_mac(mock_resource_module): |
| 29 |
if sys.platform == "win32": |
| 30 |
pytest.skip("This test is for Linux/Mac platforms only.") |
| 31 |
now_sys, now_user = get_times() |
| 32 |
assert now_sys == 1.23 |
| 33 |
assert now_user == 4.56 |
| 34 |
|
| 35 |
|
| 36 |
@pytest.fixture |
| 37 |
def mock_os_times(): |
| 38 |
with patch( |
| 39 |
"os.times", return_value=MagicMock(system=2.34, user=5.67) |
| 40 |
) as mock_times: |
| 41 |
yield mock_times |
| 42 |
|
| 43 |
|
| 44 |
def test_get_times_win32(mock_os_times): |
| 45 |
with patch("sys.platform", "win32"): |
| 46 |
now_sys, now_user = get_times() |
| 47 |
assert now_sys == 2.34 |
| 48 |
assert now_user == 5.67 |