Warnungen in Python-Unit-Tests (pytest / tox) ignorieren
Problem:
Du hast einen Unit-Test wie diesen aus 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))der eine Warnung ausgibt:
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)Lösung
Du kannst diese spezifische Warnung für diesen spezifischen Testfall ignorieren, indem du eine pytest-Annotation verwendest:
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))Andere Warnungen werden weiterhin ausgegeben.
Check out similar posts by category:
Python
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow