Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use @pytest.mark.skipif decorator instead of @unittest.skipIf #2459

Merged
merged 5 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion psutil/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -975,6 +975,12 @@ def warns(warning, match=None):
return unittest.TestCase().assertWarns(warning)

class mark:

@staticmethod
def skipif(condition, reason=""):
"""Mimics `@pytest.mark.skipif` decorator."""
return unittest.skipIf(condition, reason)

class xdist_group:
"""Mimics `@pytest.mark.xdist_group` decorator (no-op)."""

Expand Down Expand Up @@ -1150,7 +1156,7 @@ def assertProcessZombie(self, proc):
# self.assertEqual(proc.ppid(), os.getpid())


@unittest.skipIf(PYPY, "unreliable on PYPY")
@pytest.mark.skipif(PYPY, reason="unreliable on PYPY")
class TestMemoryLeak(PsutilTestCase):
"""Test framework class for detecting function memory leaks,
typically functions implemented in C which forgot to free() memory
Expand Down
4 changes: 2 additions & 2 deletions psutil/tests/test_aix.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@
"""AIX specific tests."""

import re
import unittest

import psutil
from psutil import AIX
from psutil.tests import PsutilTestCase
from psutil.tests import pytest
from psutil.tests import sh


@unittest.skipIf(not AIX, "AIX only")
@pytest.mark.skipif(not AIX, reason="AIX only")
class AIXSpecificTestCase(PsutilTestCase):
def test_virtual_memory(self):
out = sh('/usr/bin/svmon -O unit=KB')
Expand Down
44 changes: 24 additions & 20 deletions psutil/tests/test_bsd.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def muse(field):
# =====================================================================


@unittest.skipIf(not BSD, "BSD only")
@pytest.mark.skipif(not BSD, reason="BSD only")
class BSDTestCase(PsutilTestCase):
"""Generic tests common to all BSD variants."""

Expand All @@ -86,7 +86,7 @@ def setUpClass(cls):
def tearDownClass(cls):
terminate(cls.pid)

@unittest.skipIf(NETBSD, "-o lstart doesn't work on NETBSD")
@pytest.mark.skipif(NETBSD, reason="-o lstart doesn't work on NETBSD")
def test_process_create_time(self):
output = sh("ps -o lstart -p %s" % self.pid)
start_ps = output.replace('STARTED', '').strip()
Expand Down Expand Up @@ -123,18 +123,22 @@ def df(path):
if abs(usage.used - used) > 10 * 1024 * 1024:
raise self.fail("psutil=%s, df=%s" % (usage.used, used))

@unittest.skipIf(not which('sysctl'), "sysctl cmd not available")
@pytest.mark.skipif(not which('sysctl'), reason="sysctl cmd not available")
def test_cpu_count_logical(self):
syst = sysctl("hw.ncpu")
assert psutil.cpu_count(logical=True) == syst

@unittest.skipIf(not which('sysctl'), "sysctl cmd not available")
@unittest.skipIf(NETBSD, "skipped on NETBSD") # we check /proc/meminfo
@pytest.mark.skipif(not which('sysctl'), reason="sysctl cmd not available")
@pytest.mark.skipif(
NETBSD, reason="skipped on NETBSD" # we check /proc/meminfo
)
def test_virtual_memory_total(self):
num = sysctl('hw.physmem')
assert num == psutil.virtual_memory().total

@unittest.skipIf(not which('ifconfig'), "ifconfig cmd not available")
@pytest.mark.skipif(
not which('ifconfig'), reason="ifconfig cmd not available"
)
def test_net_if_stats(self):
for name, stats in psutil.net_if_stats().items():
try:
Expand All @@ -152,7 +156,7 @@ def test_net_if_stats(self):
# =====================================================================


@unittest.skipIf(not FREEBSD, "FREEBSD only")
@pytest.mark.skipif(not FREEBSD, reason="FREEBSD only")
class FreeBSDPsutilTestCase(PsutilTestCase):
@classmethod
def setUpClass(cls):
Expand Down Expand Up @@ -241,7 +245,7 @@ def test_cpu_times(self):
raise RuntimeError("couldn't find lines match in procstat out")


@unittest.skipIf(not FREEBSD, "FREEBSD only")
@pytest.mark.skipif(not FREEBSD, reason="FREEBSD only")
class FreeBSDSystemTestCase(PsutilTestCase):
@staticmethod
def parse_swapinfo():
Expand Down Expand Up @@ -310,42 +314,42 @@ def test_vmem_buffers(self):

# --- virtual_memory(); tests against muse

@unittest.skipIf(not MUSE_AVAILABLE, "muse not installed")
@pytest.mark.skipif(not MUSE_AVAILABLE, reason="muse not installed")
def test_muse_vmem_total(self):
num = muse('Total')
assert psutil.virtual_memory().total == num

@unittest.skipIf(not MUSE_AVAILABLE, "muse not installed")
@pytest.mark.skipif(not MUSE_AVAILABLE, reason="muse not installed")
@retry_on_failure()
def test_muse_vmem_active(self):
num = muse('Active')
assert abs(psutil.virtual_memory().active - num) < TOLERANCE_SYS_MEM

@unittest.skipIf(not MUSE_AVAILABLE, "muse not installed")
@pytest.mark.skipif(not MUSE_AVAILABLE, reason="muse not installed")
@retry_on_failure()
def test_muse_vmem_inactive(self):
num = muse('Inactive')
assert abs(psutil.virtual_memory().inactive - num) < TOLERANCE_SYS_MEM

@unittest.skipIf(not MUSE_AVAILABLE, "muse not installed")
@pytest.mark.skipif(not MUSE_AVAILABLE, reason="muse not installed")
@retry_on_failure()
def test_muse_vmem_wired(self):
num = muse('Wired')
assert abs(psutil.virtual_memory().wired - num) < TOLERANCE_SYS_MEM

@unittest.skipIf(not MUSE_AVAILABLE, "muse not installed")
@pytest.mark.skipif(not MUSE_AVAILABLE, reason="muse not installed")
@retry_on_failure()
def test_muse_vmem_cached(self):
num = muse('Cache')
assert abs(psutil.virtual_memory().cached - num) < TOLERANCE_SYS_MEM

@unittest.skipIf(not MUSE_AVAILABLE, "muse not installed")
@pytest.mark.skipif(not MUSE_AVAILABLE, reason="muse not installed")
@retry_on_failure()
def test_muse_vmem_free(self):
num = muse('Free')
assert abs(psutil.virtual_memory().free - num) < TOLERANCE_SYS_MEM

@unittest.skipIf(not MUSE_AVAILABLE, "muse not installed")
@pytest.mark.skipif(not MUSE_AVAILABLE, reason="muse not installed")
@retry_on_failure()
def test_muse_vmem_buffers(self):
num = muse('Buffer')
Expand Down Expand Up @@ -412,7 +416,7 @@ def test_boot_time(self):

# --- sensors_battery

@unittest.skipIf(not HAS_BATTERY, "no battery")
@pytest.mark.skipif(not HAS_BATTERY, reason="no battery")
def test_sensors_battery(self):
def secs2hours(secs):
m, _s = divmod(secs, 60)
Expand All @@ -432,7 +436,7 @@ def secs2hours(secs):
else:
assert secs2hours(metrics.secsleft) == remaining_time

@unittest.skipIf(not HAS_BATTERY, "no battery")
@pytest.mark.skipif(not HAS_BATTERY, reason="no battery")
def test_sensors_battery_against_sysctl(self):
assert psutil.sensors_battery().percent == sysctl(
"hw.acpi.battery.life"
Expand All @@ -446,7 +450,7 @@ def test_sensors_battery_against_sysctl(self):
else:
assert secsleft == sysctl("hw.acpi.battery.time") * 60

@unittest.skipIf(HAS_BATTERY, "has battery")
@pytest.mark.skipif(HAS_BATTERY, reason="has battery")
def test_sensors_battery_no_battery(self):
# If no battery is present one of these calls is supposed
# to fail, see:
Expand Down Expand Up @@ -489,7 +493,7 @@ def test_sensors_temperatures_against_sysctl(self):
# =====================================================================


@unittest.skipIf(not OPENBSD, "OPENBSD only")
@pytest.mark.skipif(not OPENBSD, reason="OPENBSD only")
class OpenBSDTestCase(PsutilTestCase):
def test_boot_time(self):
s = sysctl('kern.boottime')
Expand All @@ -503,7 +507,7 @@ def test_boot_time(self):
# =====================================================================


@unittest.skipIf(not NETBSD, "NETBSD only")
@pytest.mark.skipif(not NETBSD, reason="NETBSD only")
class NetBSDTestCase(PsutilTestCase):
@staticmethod
def parse_meminfo(look_for):
Expand Down
17 changes: 8 additions & 9 deletions psutil/tests/test_connections.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import os
import socket
import textwrap
import unittest
from contextlib import closing
from socket import AF_INET
from socket import AF_INET6
Expand Down Expand Up @@ -86,7 +85,7 @@ def compare_procsys_connections(self, pid, proc_cons, kind='all'):


class TestBasicOperations(ConnectionTestCase):
@unittest.skipIf(SKIP_SYSCONS, "requires root")
@pytest.mark.skipif(SKIP_SYSCONS, reason="requires root")
def test_system(self):
with create_sockets():
for conn in psutil.net_connections(kind='all'):
Expand Down Expand Up @@ -158,7 +157,7 @@ def test_tcp_v4(self):
assert conn.raddr == ()
assert conn.status == psutil.CONN_LISTEN

@unittest.skipIf(not supports_ipv6(), "IPv6 not supported")
@pytest.mark.skipif(not supports_ipv6(), reason="IPv6 not supported")
def test_tcp_v6(self):
addr = ("::1", 0)
with closing(bind_socket(AF_INET6, SOCK_STREAM, addr=addr)) as sock:
Expand All @@ -173,23 +172,23 @@ def test_udp_v4(self):
assert conn.raddr == ()
assert conn.status == psutil.CONN_NONE

@unittest.skipIf(not supports_ipv6(), "IPv6 not supported")
@pytest.mark.skipif(not supports_ipv6(), reason="IPv6 not supported")
def test_udp_v6(self):
addr = ("::1", 0)
with closing(bind_socket(AF_INET6, SOCK_DGRAM, addr=addr)) as sock:
conn = self.check_socket(sock)
assert conn.raddr == ()
assert conn.status == psutil.CONN_NONE

@unittest.skipIf(not POSIX, 'POSIX only')
@pytest.mark.skipif(not POSIX, reason="POSIX only")
def test_unix_tcp(self):
testfn = self.get_testfn()
with closing(bind_unix_socket(testfn, type=SOCK_STREAM)) as sock:
conn = self.check_socket(sock)
assert conn.raddr == "" # noqa
assert conn.status == psutil.CONN_NONE

@unittest.skipIf(not POSIX, 'POSIX only')
@pytest.mark.skipif(not POSIX, reason="POSIX only")
def test_unix_udp(self):
testfn = self.get_testfn()
with closing(bind_unix_socket(testfn, type=SOCK_STREAM)) as sock:
Expand All @@ -206,7 +205,7 @@ class TestConnectedSocket(ConnectionTestCase):

# On SunOS, even after we close() it, the server socket stays around
# in TIME_WAIT state.
@unittest.skipIf(SUNOS, "unreliable on SUONS")
@pytest.mark.skipif(SUNOS, reason="unreliable on SUONS")
def test_tcp(self):
addr = ("127.0.0.1", 0)
assert this_proc_net_connections(kind='tcp4') == []
Expand All @@ -226,7 +225,7 @@ def test_tcp(self):
server.close()
client.close()

@unittest.skipIf(not POSIX, 'POSIX only')
@pytest.mark.skipif(not POSIX, reason="POSIX only")
def test_unix(self):
testfn = self.get_testfn()
server, client = unix_socketpair(testfn)
Expand Down Expand Up @@ -484,7 +483,7 @@ def test_count(self):
assert conn.type in (SOCK_STREAM, SOCK_DGRAM)


@unittest.skipIf(SKIP_SYSCONS, "requires root")
@pytest.mark.skipif(SKIP_SYSCONS, reason="requires root")
class TestSystemWideConnections(ConnectionTestCase):
"""Tests for net_connections()."""

Expand Down
29 changes: 16 additions & 13 deletions psutil/tests/test_contracts.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
from psutil.tests import enum
from psutil.tests import is_namedtuple
from psutil.tests import kernel_version
from psutil.tests import pytest


# ===================================================================
Expand Down Expand Up @@ -74,8 +75,9 @@ def test_linux_ioprio_windows(self):
ae(hasattr(psutil, "IOPRIO_LOW"), WINDOWS)
ae(hasattr(psutil, "IOPRIO_VERYLOW"), WINDOWS)

@unittest.skipIf(
GITHUB_ACTIONS and LINUX, "unsupported on GITHUB_ACTIONS + LINUX"
@pytest.mark.skipif(
GITHUB_ACTIONS and LINUX,
reason="unsupported on GITHUB_ACTIONS + LINUX",
)
def test_rlimit(self):
ae = self.assertEqual
Expand Down Expand Up @@ -158,8 +160,9 @@ def test_terminal(self):
def test_ionice(self):
assert hasattr(psutil.Process, "ionice") == (LINUX or WINDOWS)

@unittest.skipIf(
GITHUB_ACTIONS and LINUX, "unsupported on GITHUB_ACTIONS + LINUX"
@pytest.mark.skipif(
GITHUB_ACTIONS and LINUX,
reason="unsupported on GITHUB_ACTIONS + LINUX",
)
def test_rlimit(self):
assert hasattr(psutil.Process, "rlimit") == (LINUX or FREEBSD)
Expand Down Expand Up @@ -228,10 +231,10 @@ def test_cpu_count(self):
assert isinstance(psutil.cpu_count(), int)

# TODO: remove this once 1892 is fixed
@unittest.skipIf(
MACOS and platform.machine() == 'arm64', "skipped due to #1892"
@pytest.mark.skipif(
MACOS and platform.machine() == 'arm64', reason="skipped due to #1892"
)
@unittest.skipIf(not HAS_CPU_FREQ, "not supported")
@pytest.mark.skipif(not HAS_CPU_FREQ, reason="not supported")
def test_cpu_freq(self):
if psutil.cpu_freq() is None:
raise unittest.SkipTest("cpu_freq() returns None")
Expand All @@ -251,7 +254,7 @@ def test_disk_partitions(self):
assert isinstance(disk.fstype, str)
assert isinstance(disk.opts, str)

@unittest.skipIf(SKIP_SYSCONS, "requires root")
@pytest.mark.skipif(SKIP_SYSCONS, reason="requires root")
def test_net_connections(self):
with create_sockets():
ret = psutil.net_connections('all')
Expand All @@ -272,7 +275,7 @@ def test_net_if_addrs(self):
assert isinstance(addr.netmask, (str, type(None)))
assert isinstance(addr.broadcast, (str, type(None)))

@unittest.skipIf(QEMU_USER, 'QEMU user not supported')
@pytest.mark.skipif(QEMU_USER, reason="QEMU user not supported")
def test_net_if_stats(self):
# Duplicate of test_system.py. Keep it anyway.
for ifname, info in psutil.net_if_stats().items():
Expand All @@ -285,13 +288,13 @@ def test_net_if_stats(self):
assert isinstance(info.speed, int)
assert isinstance(info.mtu, int)

@unittest.skipIf(not HAS_NET_IO_COUNTERS, 'not supported')
@pytest.mark.skipif(not HAS_NET_IO_COUNTERS, reason="not supported")
def test_net_io_counters(self):
# Duplicate of test_system.py. Keep it anyway.
for ifname in psutil.net_io_counters(pernic=True):
assert isinstance(ifname, str)

@unittest.skipIf(not HAS_SENSORS_FANS, "not supported")
@pytest.mark.skipif(not HAS_SENSORS_FANS, reason="not supported")
def test_sensors_fans(self):
# Duplicate of test_system.py. Keep it anyway.
for name, units in psutil.sensors_fans().items():
Expand All @@ -300,7 +303,7 @@ def test_sensors_fans(self):
assert isinstance(unit.label, str)
assert isinstance(unit.current, (float, int, type(None)))

@unittest.skipIf(not HAS_SENSORS_TEMPERATURES, "not supported")
@pytest.mark.skipif(not HAS_SENSORS_TEMPERATURES, reason="not supported")
def test_sensors_temperatures(self):
# Duplicate of test_system.py. Keep it anyway.
for name, units in psutil.sensors_temperatures().items():
Expand All @@ -325,7 +328,7 @@ def test_users(self):


class TestProcessWaitType(PsutilTestCase):
@unittest.skipIf(not POSIX, "not POSIX")
@pytest.mark.skipif(not POSIX, reason="not POSIX")
def test_negative_signal(self):
p = psutil.Process(self.spawn_testproc().pid)
p.terminate()
Expand Down
Loading
Loading