Как игнорировать предупреждения в модульных тестах Python (pytest / tox)

Проблема:

У вас есть тестовый случай, такой как этот из UliEngineering

tests/Math/TestDecibel.py
def test_ratio_to_dB_infinite(self):
    self.assertEqual(-np.inf, ratio_to_dB(0))
    self.assertEqual(-np.inf, ratio_to_dB(0, factor=dBFactor.Power))
    self.assertEqual(-np.inf, ratio_to_dB(-5))
    self.assertEqual(-np.inf, ratio_to_dB(-5, factor=dBFactor.Power))

который выводит предупреждение:

output.txt
tests/Math/TestDecibel.py::TestDecibel::test_ratio_to_dB_infinite
  /home/uli/UliEngineering/UliEngineering/Math/Decibel.py:29: RuntimeWarning: divide by zero encountered in log10
    return np.log10(v)

Решение

Вы можете игнорировать это конкретное предупреждение для этого конкретного тестового случая, используя аннотацию pytest:

tests/Math/TestDecibel.py
import pytest

@pytest.mark.filterwarnings("ignore:divide by zero encountered in log10")
def test_ratio_to_dB_infinite(self):
    self.assertEqual(-np.inf, ratio_to_dB(0))
    self.assertEqual(-np.inf, ratio_to_dB(0, factor=dBFactor.Power))
    self.assertEqual(-np.inf, ratio_to_dB(-5))
    self.assertEqual(-np.inf, ratio_to_dB(-5, factor=dBFactor.Power))

Другие предупреждения всё равно будут выводиться.


Check out similar posts by category: Python